author | lana |
Mon, 20 Oct 2008 19:07:12 -0700 | |
changeset 1971 | 553b81ac1756 |
parent 1961 | 436a5a828d9f |
parent 1293 | ef349b440fc9 |
child 1979 | f47a542ee9b7 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved. |
|
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; |
|
39 |
import java.util.logging.*; |
|
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 { |
|
64 |
private static final Logger eventLog = Logger.getLogger("java.awt.event.EventDispatchThread"); |
|
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 |
} |
|
107 |
synchronized (theQueue) { |
|
108 |
if (theQueue.getDispatchThread() == this) { |
|
109 |
theQueue.detachDispatchThread(); |
|
110 |
} |
|
111 |
} |
|
112 |
} |
|
113 |
||
114 |
public void stopDispatching() { |
|
115 |
stopDispatchingImpl(true); |
|
116 |
} |
|
117 |
||
118 |
public void stopDispatchingLater() { |
|
119 |
stopDispatchingImpl(false); |
|
120 |
} |
|
121 |
||
122 |
class StopDispatchEvent extends AWTEvent implements ActiveEvent { |
|
123 |
/* |
|
124 |
* serialVersionUID |
|
125 |
*/ |
|
126 |
static final long serialVersionUID = -3692158172100730735L; |
|
127 |
||
128 |
public StopDispatchEvent() { |
|
129 |
super(EventDispatchThread.this,0); |
|
130 |
} |
|
131 |
||
132 |
public void dispatch() { |
|
133 |
doDispatch = false; |
|
134 |
} |
|
135 |
} |
|
136 |
||
137 |
public void run() { |
|
138 |
try { |
|
139 |
pumpEvents(new Conditional() { |
|
140 |
public boolean evaluate() { |
|
141 |
return true; |
|
142 |
} |
|
143 |
}); |
|
144 |
} finally { |
|
145 |
/* |
|
146 |
* This synchronized block is to secure that the event dispatch |
|
147 |
* thread won't die in the middle of posting a new event to the |
|
148 |
* associated event queue. It is important because we notify |
|
149 |
* that the event dispatch thread is busy after posting a new event |
|
150 |
* to its queue, so the EventQueue.dispatchThread reference must |
|
151 |
* be valid at that point. |
|
152 |
*/ |
|
153 |
synchronized (theQueue) { |
|
154 |
if (theQueue.getDispatchThread() == this) { |
|
155 |
theQueue.detachDispatchThread(); |
|
156 |
} |
|
157 |
/* |
|
158 |
* Event dispatch thread dies in case of an uncaught exception. |
|
159 |
* A new event dispatch thread for this queue will be started |
|
160 |
* only if a new event is posted to it. In case if no more |
|
161 |
* events are posted after this thread died all events that |
|
162 |
* currently are in the queue will never be dispatched. |
|
163 |
*/ |
|
164 |
/* |
|
165 |
* Fix for 4648733. Check both the associated java event |
|
166 |
* queue and the PostEventQueue. |
|
167 |
*/ |
|
168 |
if (theQueue.peekEvent() != null || |
|
169 |
!SunToolkit.isPostEventQueueEmpty()) { |
|
170 |
theQueue.initDispatchThread(); |
|
171 |
} |
|
172 |
AWTAutoShutdown.getInstance().notifyThreadFree(this); |
|
173 |
} |
|
174 |
} |
|
175 |
} |
|
176 |
||
177 |
void pumpEvents(Conditional cond) { |
|
178 |
pumpEvents(ANY_EVENT, cond); |
|
179 |
} |
|
180 |
||
181 |
void pumpEventsForHierarchy(Conditional cond, Component modalComponent) { |
|
182 |
pumpEventsForHierarchy(ANY_EVENT, cond, modalComponent); |
|
183 |
} |
|
184 |
||
185 |
void pumpEvents(int id, Conditional cond) { |
|
186 |
pumpEventsForHierarchy(id, cond, null); |
|
187 |
} |
|
188 |
||
189 |
void pumpEventsForHierarchy(int id, Conditional cond, Component modalComponent) |
|
190 |
{ |
|
191 |
pumpEventsForFilter(id, cond, new HierarchyEventFilter(modalComponent)); |
|
192 |
} |
|
193 |
||
194 |
void pumpEventsForFilter(Conditional cond, EventFilter filter) { |
|
195 |
pumpEventsForFilter(ANY_EVENT, cond, filter); |
|
196 |
} |
|
197 |
||
198 |
void pumpEventsForFilter(int id, Conditional cond, EventFilter filter) { |
|
199 |
addEventFilter(filter); |
|
200 |
while (doDispatch && cond.evaluate()) { |
|
201 |
if (isInterrupted() || !pumpOneEventForFilters(id)) { |
|
202 |
doDispatch = false; |
|
203 |
} |
|
204 |
} |
|
205 |
removeEventFilter(filter); |
|
206 |
} |
|
207 |
||
208 |
void addEventFilter(EventFilter filter) { |
|
209 |
synchronized (eventFilters) { |
|
210 |
if (!eventFilters.contains(filter)) { |
|
211 |
if (filter instanceof ModalEventFilter) { |
|
212 |
ModalEventFilter newFilter = (ModalEventFilter)filter; |
|
213 |
int k = 0; |
|
214 |
for (k = 0; k < eventFilters.size(); k++) { |
|
215 |
EventFilter f = eventFilters.get(k); |
|
216 |
if (f instanceof ModalEventFilter) { |
|
217 |
ModalEventFilter cf = (ModalEventFilter)f; |
|
218 |
if (cf.compareTo(newFilter) > 0) { |
|
219 |
break; |
|
220 |
} |
|
221 |
} |
|
222 |
} |
|
223 |
eventFilters.add(k, filter); |
|
224 |
modalFiltersCount++; |
|
225 |
} else { |
|
226 |
eventFilters.add(filter); |
|
227 |
} |
|
228 |
} |
|
229 |
} |
|
230 |
} |
|
231 |
||
232 |
void removeEventFilter(EventFilter filter) { |
|
233 |
synchronized (eventFilters) { |
|
234 |
if (eventFilters.contains(filter)) { |
|
235 |
if (filter instanceof ModalEventFilter) { |
|
236 |
modalFiltersCount--; |
|
237 |
} |
|
238 |
eventFilters.remove(filter); |
|
239 |
} |
|
240 |
} |
|
241 |
} |
|
242 |
||
243 |
boolean pumpOneEventForFilters(int id) { |
|
244 |
try { |
|
245 |
AWTEvent event; |
|
246 |
boolean eventOK; |
|
1293 | 247 |
EventQueueDelegate.Delegate delegate = |
248 |
EventQueueDelegate.getDelegate(); |
|
2 | 249 |
do { |
1293 | 250 |
if (delegate != null && id == ANY_EVENT) { |
251 |
event = delegate.getNextEvent(theQueue); |
|
252 |
} else { |
|
253 |
event = (id == ANY_EVENT) |
|
254 |
? theQueue.getNextEvent() |
|
255 |
: theQueue.getNextEvent(id); |
|
256 |
} |
|
2 | 257 |
|
258 |
eventOK = true; |
|
259 |
synchronized (eventFilters) { |
|
260 |
for (int i = eventFilters.size() - 1; i >= 0; i--) { |
|
261 |
EventFilter f = eventFilters.get(i); |
|
262 |
EventFilter.FilterAction accept = f.acceptEvent(event); |
|
263 |
if (accept == EventFilter.FilterAction.REJECT) { |
|
264 |
eventOK = false; |
|
265 |
break; |
|
266 |
} else if (accept == EventFilter.FilterAction.ACCEPT_IMMEDIATELY) { |
|
267 |
break; |
|
268 |
} |
|
269 |
} |
|
270 |
} |
|
271 |
eventOK = eventOK && SunDragSourceContextPeer.checkEvent(event); |
|
272 |
if (!eventOK) { |
|
273 |
event.consume(); |
|
274 |
} |
|
275 |
} |
|
276 |
while (eventOK == false); |
|
277 |
||
278 |
if (eventLog.isLoggable(Level.FINEST)) { |
|
279 |
eventLog.log(Level.FINEST, "Dispatching: " + event); |
|
280 |
} |
|
281 |
||
1293 | 282 |
Object handle = null; |
283 |
if (delegate != null) { |
|
284 |
handle = delegate.beforeDispatch(event); |
|
285 |
} |
|
2 | 286 |
theQueue.dispatchEvent(event); |
1293 | 287 |
if (delegate != null) { |
288 |
delegate.afterDispatch(event, handle); |
|
289 |
} |
|
2 | 290 |
return true; |
291 |
} |
|
292 |
catch (ThreadDeath death) { |
|
293 |
return false; |
|
294 |
||
295 |
} |
|
296 |
catch (InterruptedException interruptedException) { |
|
297 |
return false; // AppContext.dispose() interrupts all |
|
298 |
// Threads in the AppContext |
|
299 |
||
300 |
} |
|
301 |
// Can get and throw only unchecked exceptions |
|
302 |
catch (RuntimeException e) { |
|
1961
436a5a828d9f
6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents:
2
diff
changeset
|
303 |
processException(e); |
2 | 304 |
} catch (Error e) { |
1961
436a5a828d9f
6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents:
2
diff
changeset
|
305 |
processException(e); |
2 | 306 |
} |
307 |
return true; |
|
308 |
} |
|
309 |
||
1961
436a5a828d9f
6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents:
2
diff
changeset
|
310 |
private void processException(Throwable e) { |
2 | 311 |
if (eventLog.isLoggable(Level.FINE)) { |
1961
436a5a828d9f
6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents:
2
diff
changeset
|
312 |
eventLog.log(Level.FINE, "Processing exception: " + e); |
2 | 313 |
} |
1961
436a5a828d9f
6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents:
2
diff
changeset
|
314 |
getUncaughtExceptionHandler().uncaughtException(this, e); |
436a5a828d9f
6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents:
2
diff
changeset
|
315 |
// don't rethrow the exception to avoid EDT recreation |
2 | 316 |
} |
317 |
||
318 |
boolean isDispatching(EventQueue eq) { |
|
319 |
return theQueue.equals(eq); |
|
320 |
} |
|
321 |
||
322 |
EventQueue getEventQueue() { return theQueue; } |
|
323 |
||
324 |
private static class HierarchyEventFilter implements EventFilter { |
|
325 |
private Component modalComponent; |
|
326 |
public HierarchyEventFilter(Component modalComponent) { |
|
327 |
this.modalComponent = modalComponent; |
|
328 |
} |
|
329 |
public FilterAction acceptEvent(AWTEvent event) { |
|
330 |
if (modalComponent != null) { |
|
331 |
int eventID = event.getID(); |
|
332 |
boolean mouseEvent = (eventID >= MouseEvent.MOUSE_FIRST) && |
|
333 |
(eventID <= MouseEvent.MOUSE_LAST); |
|
334 |
boolean actionEvent = (eventID >= ActionEvent.ACTION_FIRST) && |
|
335 |
(eventID <= ActionEvent.ACTION_LAST); |
|
336 |
boolean windowClosingEvent = (eventID == WindowEvent.WINDOW_CLOSING); |
|
337 |
/* |
|
338 |
* filter out MouseEvent and ActionEvent that's outside |
|
339 |
* the modalComponent hierarchy. |
|
340 |
* KeyEvent is handled by using enqueueKeyEvent |
|
341 |
* in Dialog.show |
|
342 |
*/ |
|
343 |
if (Component.isInstanceOf(modalComponent, "javax.swing.JInternalFrame")) { |
|
344 |
/* |
|
345 |
* Modal internal frames are handled separately. If event is |
|
346 |
* for some component from another heavyweight than modalComp, |
|
347 |
* it is accepted. If heavyweight is the same - we still accept |
|
348 |
* event and perform further filtering in LightweightDispatcher |
|
349 |
*/ |
|
350 |
return windowClosingEvent ? FilterAction.REJECT : FilterAction.ACCEPT; |
|
351 |
} |
|
352 |
if (mouseEvent || actionEvent || windowClosingEvent) { |
|
353 |
Object o = event.getSource(); |
|
354 |
if (o instanceof sun.awt.ModalExclude) { |
|
355 |
// Exclude this object from modality and |
|
356 |
// continue to pump it's events. |
|
357 |
return FilterAction.ACCEPT; |
|
358 |
} else if (o instanceof Component) { |
|
359 |
Component c = (Component) o; |
|
360 |
// 5.0u3 modal exclusion |
|
361 |
boolean modalExcluded = false; |
|
362 |
if (modalComponent instanceof Container) { |
|
363 |
while (c != modalComponent && c != null) { |
|
364 |
if ((c instanceof Window) && |
|
365 |
(sun.awt.SunToolkit.isModalExcluded((Window)c))) { |
|
366 |
// Exclude this window and all its children from |
|
367 |
// modality and continue to pump it's events. |
|
368 |
modalExcluded = true; |
|
369 |
break; |
|
370 |
} |
|
371 |
c = c.getParent(); |
|
372 |
} |
|
373 |
} |
|
374 |
if (!modalExcluded && (c != modalComponent)) { |
|
375 |
return FilterAction.REJECT; |
|
376 |
} |
|
377 |
} |
|
378 |
} |
|
379 |
} |
|
380 |
return FilterAction.ACCEPT; |
|
381 |
} |
|
382 |
} |
|
383 |
} |