View Javadoc

1   /*
2       Jiffie Plugin for Jameleon - An Internet Explorer plug-in for Jameleon
3       Copyright (C) 2004 Christian W. Hargraves (engrean@hotmail.com) and
4                          Matthias Marschall (matthias@marschalls.de)
5   
6       This program is free software; you can redistribute it and/or modify
7       it under the terms of the GNU General Public License as published by
8       the Free Software Foundation; either version 2 of the License, or
9       (at your option) any later version.
10  
11      This program is distributed in the hope that it will be useful,
12      but WITHOUT ANY WARRANTY; without even the implied warranty of
13      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14      GNU General Public License for more details.
15  
16      You should have received a copy of the GNU General Public License
17      along with this program; if not, write to the Free Software
18      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20  package net.sf.jameleon.plugin.jiffie.util;
21  
22  import net.sf.jiffie.InternetExplorer;
23  import net.sf.jiffie.NavigationListener;
24  import net.sf.jiffie.NewWindowEventHandler;
25  import net.sf.jiffie.WindowListener;
26  
27  import java.util.Iterator;
28  
29  import com.jacob.com.Variant;
30  
31  
32  /***
33   * This class implements an event handler which receives only the events
34   * the jiffie plug-in cares about. This class extends the new window event
35   * handler, which only recieves new window events and makes all other
36   * methods thread-safe.
37   * 
38   * Note that the non-standard capitalisation of the method names is necessary 
39   * to ensure that they match exactly the names used by Internet Explorer. 
40   * Detailed documentation on these events can be found at:
41   * 
42   * http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/webbrowser/reflist_vb.asp
43   */
44  public class JiffieEventHandler extends NewWindowEventHandler {
45  
46      protected boolean available = true;
47  
48      /***
49       * Constructor.
50       *
51       * @param parentBrowser parent browser
52       */
53      public JiffieEventHandler (InternetExplorer parentBrowser) {
54          super (parentBrowser);
55      }
56  
57      /***
58       * NewWindow2 event handler.
59       *
60       * args[0] Object expression that, optionally, receives a new, hidden
61       *         InternetExplorer object with no URL loaded.
62       * args[1] Boolean value to determine whether the current navigation
63       *         should be canceled.
64       *
65       * @param args event arguments
66       */
67      public synchronized void NewWindow2 (Variant[] args) {
68          while (!available) {
69              try {
70                  wait();
71              } catch (InterruptedException e) {}
72          }
73          available = false;
74          super.NewWindow2(args);
75          available = true;
76          notifyAll();
77      }
78  
79  
80      /***
81       * ProgressChange event handler.
82       *
83       * args[0] Long that specifies the amount of total progress
84       *         to show, or -1 when progress is complete.
85       * args[1] Long that specifies the maximum progress value.
86       *
87       * @param args event arguments
88       */
89      public synchronized void ProgressChange (Variant[] args) {
90  
91          while (!available) {
92              try {
93                  wait();
94              } catch (InterruptedException e) {}
95          }
96          available = false;
97  
98          int nProgress = args[0].toInt();
99          int nProgressMax = args[1].toInt();
100         for (Iterator itr = m_navigationListeners.iterator(); itr.hasNext();) {
101             NavigationListener l = (NavigationListener)itr.next();
102             l.progressChange(nProgress, nProgressMax);
103         }
104         available = true;
105         notifyAll();
106     }
107 
108 
109     /***
110      * OnQuit event handler.
111      *
112      * @param args event arguments
113      */
114     public void OnQuit (Variant[] args) {
115         for (Iterator itr = m_windowListeners.iterator(); itr.hasNext();) {
116             WindowListener l = (WindowListener)itr.next();
117             l.onQuit();
118         }
119     }
120 
121     /***
122      * Adds a new navigation listener.
123      *
124      * @param listener the new listener to add
125      * @return true if the listener was not already in the set of listeners
126      */
127     public synchronized boolean addNavigationListener (NavigationListener listener) {
128         boolean result = false;
129         while (!available) {
130             try {
131                 wait();
132             } catch (InterruptedException e) {}
133         }
134         available = false;
135         result = super.addNavigationListener(listener);
136         available = true;
137         notifyAll();
138         return result;
139     }
140 
141     /***
142      * Removes a navigation listener from the set of listeners.
143      *
144      * @param listener the listener to remove
145      * @return true if the listener was present in the set of listeners
146      */
147     public synchronized boolean removeNavigationListener (NavigationListener listener) {
148         boolean result = false;
149         while (!available) {
150             try {
151                 wait();
152             } catch (InterruptedException e) {}
153         }
154         available = false;
155         result = super.removeNavigationListener(listener);
156         available = true;
157         notifyAll();
158         return result;
159     }
160 
161     /***
162      * Adds a new window listener.
163      *
164      * @param listener the new listener to add
165      * @return true if the listener was not already in the set of listeners
166      */
167     public synchronized boolean addWindowListener (WindowListener listener) {
168         boolean result = false;
169         while (!available) {
170             try {
171                 wait();
172             } catch (InterruptedException e) {}
173         }
174         available = false;
175         result = super.addWindowListener(listener);
176         available = true;
177         notifyAll();
178         return result;
179     }
180 
181     /***
182      * Removes a window listener from the set of listeners.
183      *
184      * @param listener the listener to remove
185      * @return true if the listener was removed from the collection
186      *         or false if the listener was not in the collection.
187      */
188     public synchronized boolean removeWindowListener (WindowListener listener) {
189         boolean result = false;
190         while (!available) {
191             try {
192                 wait();
193             } catch (InterruptedException e) {}
194         }
195         available = false;
196         result = super.removeWindowListener(listener);
197         available = true;
198         notifyAll();
199         return result;
200     }
201 
202 
203 }