author | mchung |
Tue, 08 Dec 2009 09:02:09 -0800 | |
changeset 4374 | f672d9cf521e |
parent 4365 | 4ac67034e98b |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
1639 | 2 |
* Copyright 1996-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package java.awt; |
|
27 |
||
28 |
import java.awt.event.InputEvent; |
|
29 |
import java.awt.event.MouseEvent; |
|
30 |
import java.awt.event.ActionEvent; |
|
31 |
import java.awt.event.WindowEvent; |
|
32 |
import java.lang.reflect.Method; |
|
33 |
import java.security.AccessController; |
|
34 |
import sun.security.action.GetPropertyAction; |
|
35 |
import sun.awt.AWTAutoShutdown; |
|
36 |
import sun.awt.SunToolkit; |
|
37 |
||
38 |
import java.util.Vector; |
|
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
1979
diff
changeset
|
39 |
import sun.util.logging.PlatformLogger; |
2 | 40 |
|
41 |
import sun.awt.dnd.SunDragSourceContextPeer; |
|
1293 | 42 |
import sun.awt.EventQueueDelegate; |
2 | 43 |
|
44 |
/** |
|
45 |
* EventDispatchThread is a package-private AWT class which takes |
|
46 |
* events off the EventQueue and dispatches them to the appropriate |
|
47 |
* AWT components. |
|
48 |
* |
|
49 |
* The Thread starts a "permanent" event pump with a call to |
|
50 |
* pumpEvents(Conditional) in its run() method. Event handlers can choose to |
|
51 |
* block this event pump at any time, but should start a new pump (<b>not</b> |
|
52 |
* a new EventDispatchThread) by again calling pumpEvents(Conditional). This |
|
53 |
* secondary event pump will exit automatically as soon as the Condtional |
|
54 |
* evaluate()s to false and an additional Event is pumped and dispatched. |
|
55 |
* |
|
56 |
* @author Tom Ball |
|
57 |
* @author Amy Fowler |
|
58 |
* @author Fred Ecks |
|
59 |
* @author David Mendenhall |
|
60 |
* |
|
61 |
* @since 1.1 |
|
62 |
*/ |
|
63 |
class EventDispatchThread extends Thread { |
|
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
1979
diff
changeset
|
64 |
private static final PlatformLogger eventLog = PlatformLogger.getLogger("java.awt.event.EventDispatchThread"); |
2 | 65 |
|
66 |
private EventQueue theQueue; |
|
67 |
private boolean doDispatch = true; |
|
68 |
private static final int ANY_EVENT = -1; |
|
69 |
||
70 |
private Vector<EventFilter> eventFilters = new Vector<EventFilter>(); |
|
71 |
// used in handleException |
|
72 |
private int modalFiltersCount = 0; |
|
73 |
||
74 |
EventDispatchThread(ThreadGroup group, String name, EventQueue queue) { |
|
75 |
super(group, name); |
|
76 |
theQueue = queue; |
|
77 |
} |
|
78 |
||
79 |
void stopDispatchingImpl(boolean wait) { |
|
80 |
// Note: We stop dispatching via a flag rather than using |
|
81 |
// Thread.interrupt() because we can't guarantee that the wait() |
|
82 |
// we interrupt will be EventQueue.getNextEvent()'s. -fredx 8-11-98 |
|
83 |
||
84 |
StopDispatchEvent stopEvent = new StopDispatchEvent(); |
|
85 |
||
86 |
// wait for the dispatcher to complete |
|
87 |
if (Thread.currentThread() != this) { |
|
88 |
||
89 |
// fix 4122683, 4128923 |
|
90 |
// Post an empty event to ensure getNextEvent is unblocked |
|
91 |
// |
|
92 |
// We have to use postEventPrivate instead of postEvent because |
|
93 |
// EventQueue.pop calls EventDispatchThread.stopDispatching. |
|
94 |
// Calling SunToolkit.flushPendingEvents in this case could |
|
95 |
// lead to deadlock. |
|
96 |
theQueue.postEventPrivate(stopEvent); |
|
97 |
||
98 |
if (wait) { |
|
99 |
try { |
|
100 |
join(); |
|
101 |
} catch(InterruptedException e) { |
|
102 |
} |
|
103 |
} |
|
104 |
} else { |
|
105 |
stopEvent.dispatch(); |
|
106 |
} |
|
4365 | 107 |
|
108 |
theQueue.detachDispatchThread(this, false); |
|
2 | 109 |
} |
110 |
||
111 |
public void stopDispatching() { |
|
112 |
stopDispatchingImpl(true); |
|
113 |
} |
|
114 |
||
115 |
public void stopDispatchingLater() { |
|
116 |
stopDispatchingImpl(false); |
|
117 |
} |
|
118 |
||
119 |
class StopDispatchEvent extends AWTEvent implements ActiveEvent { |
|
120 |
/* |
|
121 |
* serialVersionUID |
|
122 |
*/ |
|
123 |
static final long serialVersionUID = -3692158172100730735L; |
|
124 |
||
125 |
public StopDispatchEvent() { |
|
126 |
super(EventDispatchThread.this,0); |
|
127 |
} |
|
128 |
||
129 |
public void dispatch() { |
|
130 |
doDispatch = false; |
|
131 |
} |
|
132 |
} |
|
133 |
||
134 |
public void run() { |
|
135 |
try { |
|
136 |
pumpEvents(new Conditional() { |
|
137 |
public boolean evaluate() { |
|
138 |
return true; |
|
139 |
} |
|
140 |
}); |
|
141 |
} finally { |
|
4365 | 142 |
theQueue.detachDispatchThread(this, true); |
2 | 143 |
} |
144 |
} |
|
145 |
||
146 |
void pumpEvents(Conditional cond) { |
|
147 |
pumpEvents(ANY_EVENT, cond); |
|
148 |
} |
|
149 |
||
150 |
void pumpEventsForHierarchy(Conditional cond, Component modalComponent) { |
|
151 |
pumpEventsForHierarchy(ANY_EVENT, cond, modalComponent); |
|
152 |
} |
|
153 |
||
154 |
void pumpEvents(int id, Conditional cond) { |
|
155 |
pumpEventsForHierarchy(id, cond, null); |
|
156 |
} |
|
157 |
||
158 |
void pumpEventsForHierarchy(int id, Conditional cond, Component modalComponent) |
|
159 |
{ |
|
160 |
pumpEventsForFilter(id, cond, new HierarchyEventFilter(modalComponent)); |
|
161 |
} |
|
162 |
||
163 |
void pumpEventsForFilter(Conditional cond, EventFilter filter) { |
|
164 |
pumpEventsForFilter(ANY_EVENT, cond, filter); |
|
165 |
} |
|
166 |
||
167 |
void pumpEventsForFilter(int id, Conditional cond, EventFilter filter) { |
|
168 |
addEventFilter(filter); |
|
169 |
while (doDispatch && cond.evaluate()) { |
|
170 |
if (isInterrupted() || !pumpOneEventForFilters(id)) { |
|
171 |
doDispatch = false; |
|
172 |
} |
|
173 |
} |
|
174 |
removeEventFilter(filter); |
|
175 |
} |
|
176 |
||
177 |
void addEventFilter(EventFilter filter) { |
|
178 |
synchronized (eventFilters) { |
|
179 |
if (!eventFilters.contains(filter)) { |
|
180 |
if (filter instanceof ModalEventFilter) { |
|
181 |
ModalEventFilter newFilter = (ModalEventFilter)filter; |
|
182 |
int k = 0; |
|
183 |
for (k = 0; k < eventFilters.size(); k++) { |
|
184 |
EventFilter f = eventFilters.get(k); |
|
185 |
if (f instanceof ModalEventFilter) { |
|
186 |
ModalEventFilter cf = (ModalEventFilter)f; |
|
187 |
if (cf.compareTo(newFilter) > 0) { |
|
188 |
break; |
|
189 |
} |
|
190 |
} |
|
191 |
} |
|
192 |
eventFilters.add(k, filter); |
|
193 |
modalFiltersCount++; |
|
194 |
} else { |
|
195 |
eventFilters.add(filter); |
|
196 |
} |
|
197 |
} |
|
198 |
} |
|
199 |
} |
|
200 |
||
201 |
void removeEventFilter(EventFilter filter) { |
|
202 |
synchronized (eventFilters) { |
|
203 |
if (eventFilters.contains(filter)) { |
|
204 |
if (filter instanceof ModalEventFilter) { |
|
205 |
modalFiltersCount--; |
|
206 |
} |
|
207 |
eventFilters.remove(filter); |
|
208 |
} |
|
209 |
} |
|
210 |
} |
|
211 |
||
212 |
boolean pumpOneEventForFilters(int id) { |
|
213 |
try { |
|
214 |
AWTEvent event; |
|
215 |
boolean eventOK; |
|
1293 | 216 |
EventQueueDelegate.Delegate delegate = |
217 |
EventQueueDelegate.getDelegate(); |
|
2 | 218 |
do { |
1293 | 219 |
if (delegate != null && id == ANY_EVENT) { |
220 |
event = delegate.getNextEvent(theQueue); |
|
221 |
} else { |
|
222 |
event = (id == ANY_EVENT) |
|
223 |
? theQueue.getNextEvent() |
|
224 |
: theQueue.getNextEvent(id); |
|
225 |
} |
|
2 | 226 |
|
227 |
eventOK = true; |
|
228 |
synchronized (eventFilters) { |
|
229 |
for (int i = eventFilters.size() - 1; i >= 0; i--) { |
|
230 |
EventFilter f = eventFilters.get(i); |
|
231 |
EventFilter.FilterAction accept = f.acceptEvent(event); |
|
232 |
if (accept == EventFilter.FilterAction.REJECT) { |
|
233 |
eventOK = false; |
|
234 |
break; |
|
235 |
} else if (accept == EventFilter.FilterAction.ACCEPT_IMMEDIATELY) { |
|
236 |
break; |
|
237 |
} |
|
238 |
} |
|
239 |
} |
|
240 |
eventOK = eventOK && SunDragSourceContextPeer.checkEvent(event); |
|
241 |
if (!eventOK) { |
|
242 |
event.consume(); |
|
243 |
} |
|
244 |
} |
|
245 |
while (eventOK == false); |
|
246 |
||
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
1979
diff
changeset
|
247 |
if (eventLog.isLoggable(PlatformLogger.FINEST)) { |
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
1979
diff
changeset
|
248 |
eventLog.finest("Dispatching: " + event); |
2 | 249 |
} |
250 |
||
1293 | 251 |
Object handle = null; |
252 |
if (delegate != null) { |
|
253 |
handle = delegate.beforeDispatch(event); |
|
254 |
} |
|
2 | 255 |
theQueue.dispatchEvent(event); |
1293 | 256 |
if (delegate != null) { |
257 |
delegate.afterDispatch(event, handle); |
|
258 |
} |
|
2 | 259 |
return true; |
260 |
} |
|
261 |
catch (ThreadDeath death) { |
|
262 |
return false; |
|
263 |
||
264 |
} |
|
265 |
catch (InterruptedException interruptedException) { |
|
266 |
return false; // AppContext.dispose() interrupts all |
|
267 |
// Threads in the AppContext |
|
268 |
||
269 |
} |
|
270 |
// Can get and throw only unchecked exceptions |
|
271 |
catch (RuntimeException e) { |
|
1961
436a5a828d9f
6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents:
2
diff
changeset
|
272 |
processException(e); |
2 | 273 |
} catch (Error e) { |
1961
436a5a828d9f
6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents:
2
diff
changeset
|
274 |
processException(e); |
2 | 275 |
} |
276 |
return true; |
|
277 |
} |
|
278 |
||
1961
436a5a828d9f
6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents:
2
diff
changeset
|
279 |
private void processException(Throwable e) { |
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
1979
diff
changeset
|
280 |
if (eventLog.isLoggable(PlatformLogger.FINE)) { |
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
1979
diff
changeset
|
281 |
eventLog.fine("Processing exception: " + e); |
2 | 282 |
} |
1961
436a5a828d9f
6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents:
2
diff
changeset
|
283 |
getUncaughtExceptionHandler().uncaughtException(this, e); |
436a5a828d9f
6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents:
2
diff
changeset
|
284 |
// don't rethrow the exception to avoid EDT recreation |
2 | 285 |
} |
286 |
||
287 |
boolean isDispatching(EventQueue eq) { |
|
288 |
return theQueue.equals(eq); |
|
289 |
} |
|
290 |
||
291 |
EventQueue getEventQueue() { return theQueue; } |
|
292 |
||
293 |
private static class HierarchyEventFilter implements EventFilter { |
|
294 |
private Component modalComponent; |
|
295 |
public HierarchyEventFilter(Component modalComponent) { |
|
296 |
this.modalComponent = modalComponent; |
|
297 |
} |
|
298 |
public FilterAction acceptEvent(AWTEvent event) { |
|
299 |
if (modalComponent != null) { |
|
300 |
int eventID = event.getID(); |
|
301 |
boolean mouseEvent = (eventID >= MouseEvent.MOUSE_FIRST) && |
|
302 |
(eventID <= MouseEvent.MOUSE_LAST); |
|
303 |
boolean actionEvent = (eventID >= ActionEvent.ACTION_FIRST) && |
|
304 |
(eventID <= ActionEvent.ACTION_LAST); |
|
305 |
boolean windowClosingEvent = (eventID == WindowEvent.WINDOW_CLOSING); |
|
306 |
/* |
|
307 |
* filter out MouseEvent and ActionEvent that's outside |
|
308 |
* the modalComponent hierarchy. |
|
309 |
* KeyEvent is handled by using enqueueKeyEvent |
|
310 |
* in Dialog.show |
|
311 |
*/ |
|
312 |
if (Component.isInstanceOf(modalComponent, "javax.swing.JInternalFrame")) { |
|
313 |
/* |
|
314 |
* Modal internal frames are handled separately. If event is |
|
315 |
* for some component from another heavyweight than modalComp, |
|
316 |
* it is accepted. If heavyweight is the same - we still accept |
|
317 |
* event and perform further filtering in LightweightDispatcher |
|
318 |
*/ |
|
319 |
return windowClosingEvent ? FilterAction.REJECT : FilterAction.ACCEPT; |
|
320 |
} |
|
321 |
if (mouseEvent || actionEvent || windowClosingEvent) { |
|
322 |
Object o = event.getSource(); |
|
323 |
if (o instanceof sun.awt.ModalExclude) { |
|
324 |
// Exclude this object from modality and |
|
325 |
// continue to pump it's events. |
|
326 |
return FilterAction.ACCEPT; |
|
327 |
} else if (o instanceof Component) { |
|
328 |
Component c = (Component) o; |
|
329 |
// 5.0u3 modal exclusion |
|
330 |
boolean modalExcluded = false; |
|
331 |
if (modalComponent instanceof Container) { |
|
332 |
while (c != modalComponent && c != null) { |
|
333 |
if ((c instanceof Window) && |
|
334 |
(sun.awt.SunToolkit.isModalExcluded((Window)c))) { |
|
335 |
// Exclude this window and all its children from |
|
336 |
// modality and continue to pump it's events. |
|
337 |
modalExcluded = true; |
|
338 |
break; |
|
339 |
} |
|
340 |
c = c.getParent(); |
|
341 |
} |
|
342 |
} |
|
343 |
if (!modalExcluded && (c != modalComponent)) { |
|
344 |
return FilterAction.REJECT; |
|
345 |
} |
|
346 |
} |
|
347 |
} |
|
348 |
} |
|
349 |
return FilterAction.ACCEPT; |
|
350 |
} |
|
351 |
} |
|
352 |
} |