author | art |
Thu, 11 Sep 2008 10:38:00 +0400 | |
changeset 1961 | 436a5a828d9f |
parent 2 | 90ce3da70b43 |
child 1971 | 553b81ac1756 |
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; |
|
42 |
||
43 |
/** |
|
44 |
* EventDispatchThread is a package-private AWT class which takes |
|
45 |
* events off the EventQueue and dispatches them to the appropriate |
|
46 |
* AWT components. |
|
47 |
* |
|
48 |
* The Thread starts a "permanent" event pump with a call to |
|
49 |
* pumpEvents(Conditional) in its run() method. Event handlers can choose to |
|
50 |
* block this event pump at any time, but should start a new pump (<b>not</b> |
|
51 |
* a new EventDispatchThread) by again calling pumpEvents(Conditional). This |
|
52 |
* secondary event pump will exit automatically as soon as the Condtional |
|
53 |
* evaluate()s to false and an additional Event is pumped and dispatched. |
|
54 |
* |
|
55 |
* @author Tom Ball |
|
56 |
* @author Amy Fowler |
|
57 |
* @author Fred Ecks |
|
58 |
* @author David Mendenhall |
|
59 |
* |
|
60 |
* @since 1.1 |
|
61 |
*/ |
|
62 |
class EventDispatchThread extends Thread { |
|
63 |
private static final Logger eventLog = Logger.getLogger("java.awt.event.EventDispatchThread"); |
|
64 |
||
65 |
private EventQueue theQueue; |
|
66 |
private boolean doDispatch = true; |
|
67 |
private static final int ANY_EVENT = -1; |
|
68 |
||
69 |
private Vector<EventFilter> eventFilters = new Vector<EventFilter>(); |
|
70 |
// used in handleException |
|
71 |
private int modalFiltersCount = 0; |
|
72 |
||
73 |
EventDispatchThread(ThreadGroup group, String name, EventQueue queue) { |
|
74 |
super(group, name); |
|
75 |
theQueue = queue; |
|
76 |
} |
|
77 |
||
78 |
void stopDispatchingImpl(boolean wait) { |
|
79 |
// Note: We stop dispatching via a flag rather than using |
|
80 |
// Thread.interrupt() because we can't guarantee that the wait() |
|
81 |
// we interrupt will be EventQueue.getNextEvent()'s. -fredx 8-11-98 |
|
82 |
||
83 |
StopDispatchEvent stopEvent = new StopDispatchEvent(); |
|
84 |
||
85 |
// wait for the dispatcher to complete |
|
86 |
if (Thread.currentThread() != this) { |
|
87 |
||
88 |
// fix 4122683, 4128923 |
|
89 |
// Post an empty event to ensure getNextEvent is unblocked |
|
90 |
// |
|
91 |
// We have to use postEventPrivate instead of postEvent because |
|
92 |
// EventQueue.pop calls EventDispatchThread.stopDispatching. |
|
93 |
// Calling SunToolkit.flushPendingEvents in this case could |
|
94 |
// lead to deadlock. |
|
95 |
theQueue.postEventPrivate(stopEvent); |
|
96 |
||
97 |
if (wait) { |
|
98 |
try { |
|
99 |
join(); |
|
100 |
} catch(InterruptedException e) { |
|
101 |
} |
|
102 |
} |
|
103 |
} else { |
|
104 |
stopEvent.dispatch(); |
|
105 |
} |
|
106 |
synchronized (theQueue) { |
|
107 |
if (theQueue.getDispatchThread() == this) { |
|
108 |
theQueue.detachDispatchThread(); |
|
109 |
} |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
public void stopDispatching() { |
|
114 |
stopDispatchingImpl(true); |
|
115 |
} |
|
116 |
||
117 |
public void stopDispatchingLater() { |
|
118 |
stopDispatchingImpl(false); |
|
119 |
} |
|
120 |
||
121 |
class StopDispatchEvent extends AWTEvent implements ActiveEvent { |
|
122 |
/* |
|
123 |
* serialVersionUID |
|
124 |
*/ |
|
125 |
static final long serialVersionUID = -3692158172100730735L; |
|
126 |
||
127 |
public StopDispatchEvent() { |
|
128 |
super(EventDispatchThread.this,0); |
|
129 |
} |
|
130 |
||
131 |
public void dispatch() { |
|
132 |
doDispatch = false; |
|
133 |
} |
|
134 |
} |
|
135 |
||
136 |
public void run() { |
|
137 |
try { |
|
138 |
pumpEvents(new Conditional() { |
|
139 |
public boolean evaluate() { |
|
140 |
return true; |
|
141 |
} |
|
142 |
}); |
|
143 |
} finally { |
|
144 |
/* |
|
145 |
* This synchronized block is to secure that the event dispatch |
|
146 |
* thread won't die in the middle of posting a new event to the |
|
147 |
* associated event queue. It is important because we notify |
|
148 |
* that the event dispatch thread is busy after posting a new event |
|
149 |
* to its queue, so the EventQueue.dispatchThread reference must |
|
150 |
* be valid at that point. |
|
151 |
*/ |
|
152 |
synchronized (theQueue) { |
|
153 |
if (theQueue.getDispatchThread() == this) { |
|
154 |
theQueue.detachDispatchThread(); |
|
155 |
} |
|
156 |
/* |
|
157 |
* Event dispatch thread dies in case of an uncaught exception. |
|
158 |
* A new event dispatch thread for this queue will be started |
|
159 |
* only if a new event is posted to it. In case if no more |
|
160 |
* events are posted after this thread died all events that |
|
161 |
* currently are in the queue will never be dispatched. |
|
162 |
*/ |
|
163 |
/* |
|
164 |
* Fix for 4648733. Check both the associated java event |
|
165 |
* queue and the PostEventQueue. |
|
166 |
*/ |
|
167 |
if (theQueue.peekEvent() != null || |
|
168 |
!SunToolkit.isPostEventQueueEmpty()) { |
|
169 |
theQueue.initDispatchThread(); |
|
170 |
} |
|
171 |
AWTAutoShutdown.getInstance().notifyThreadFree(this); |
|
172 |
} |
|
173 |
} |
|
174 |
} |
|
175 |
||
176 |
void pumpEvents(Conditional cond) { |
|
177 |
pumpEvents(ANY_EVENT, cond); |
|
178 |
} |
|
179 |
||
180 |
void pumpEventsForHierarchy(Conditional cond, Component modalComponent) { |
|
181 |
pumpEventsForHierarchy(ANY_EVENT, cond, modalComponent); |
|
182 |
} |
|
183 |
||
184 |
void pumpEvents(int id, Conditional cond) { |
|
185 |
pumpEventsForHierarchy(id, cond, null); |
|
186 |
} |
|
187 |
||
188 |
void pumpEventsForHierarchy(int id, Conditional cond, Component modalComponent) |
|
189 |
{ |
|
190 |
pumpEventsForFilter(id, cond, new HierarchyEventFilter(modalComponent)); |
|
191 |
} |
|
192 |
||
193 |
void pumpEventsForFilter(Conditional cond, EventFilter filter) { |
|
194 |
pumpEventsForFilter(ANY_EVENT, cond, filter); |
|
195 |
} |
|
196 |
||
197 |
void pumpEventsForFilter(int id, Conditional cond, EventFilter filter) { |
|
198 |
addEventFilter(filter); |
|
199 |
while (doDispatch && cond.evaluate()) { |
|
200 |
if (isInterrupted() || !pumpOneEventForFilters(id)) { |
|
201 |
doDispatch = false; |
|
202 |
} |
|
203 |
} |
|
204 |
removeEventFilter(filter); |
|
205 |
} |
|
206 |
||
207 |
void addEventFilter(EventFilter filter) { |
|
208 |
synchronized (eventFilters) { |
|
209 |
if (!eventFilters.contains(filter)) { |
|
210 |
if (filter instanceof ModalEventFilter) { |
|
211 |
ModalEventFilter newFilter = (ModalEventFilter)filter; |
|
212 |
int k = 0; |
|
213 |
for (k = 0; k < eventFilters.size(); k++) { |
|
214 |
EventFilter f = eventFilters.get(k); |
|
215 |
if (f instanceof ModalEventFilter) { |
|
216 |
ModalEventFilter cf = (ModalEventFilter)f; |
|
217 |
if (cf.compareTo(newFilter) > 0) { |
|
218 |
break; |
|
219 |
} |
|
220 |
} |
|
221 |
} |
|
222 |
eventFilters.add(k, filter); |
|
223 |
modalFiltersCount++; |
|
224 |
} else { |
|
225 |
eventFilters.add(filter); |
|
226 |
} |
|
227 |
} |
|
228 |
} |
|
229 |
} |
|
230 |
||
231 |
void removeEventFilter(EventFilter filter) { |
|
232 |
synchronized (eventFilters) { |
|
233 |
if (eventFilters.contains(filter)) { |
|
234 |
if (filter instanceof ModalEventFilter) { |
|
235 |
modalFiltersCount--; |
|
236 |
} |
|
237 |
eventFilters.remove(filter); |
|
238 |
} |
|
239 |
} |
|
240 |
} |
|
241 |
||
242 |
boolean pumpOneEventForFilters(int id) { |
|
243 |
try { |
|
244 |
AWTEvent event; |
|
245 |
boolean eventOK; |
|
246 |
do { |
|
247 |
event = (id == ANY_EVENT) |
|
248 |
? theQueue.getNextEvent() |
|
249 |
: theQueue.getNextEvent(id); |
|
250 |
||
251 |
eventOK = true; |
|
252 |
synchronized (eventFilters) { |
|
253 |
for (int i = eventFilters.size() - 1; i >= 0; i--) { |
|
254 |
EventFilter f = eventFilters.get(i); |
|
255 |
EventFilter.FilterAction accept = f.acceptEvent(event); |
|
256 |
if (accept == EventFilter.FilterAction.REJECT) { |
|
257 |
eventOK = false; |
|
258 |
break; |
|
259 |
} else if (accept == EventFilter.FilterAction.ACCEPT_IMMEDIATELY) { |
|
260 |
break; |
|
261 |
} |
|
262 |
} |
|
263 |
} |
|
264 |
eventOK = eventOK && SunDragSourceContextPeer.checkEvent(event); |
|
265 |
if (!eventOK) { |
|
266 |
event.consume(); |
|
267 |
} |
|
268 |
} |
|
269 |
while (eventOK == false); |
|
270 |
||
271 |
if (eventLog.isLoggable(Level.FINEST)) { |
|
272 |
eventLog.log(Level.FINEST, "Dispatching: " + event); |
|
273 |
} |
|
274 |
||
275 |
theQueue.dispatchEvent(event); |
|
276 |
return true; |
|
277 |
} |
|
278 |
catch (ThreadDeath death) { |
|
279 |
return false; |
|
280 |
||
281 |
} |
|
282 |
catch (InterruptedException interruptedException) { |
|
283 |
return false; // AppContext.dispose() interrupts all |
|
284 |
// Threads in the AppContext |
|
285 |
||
286 |
} |
|
287 |
// Can get and throw only unchecked exceptions |
|
288 |
catch (RuntimeException e) { |
|
1961
436a5a828d9f
6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents:
2
diff
changeset
|
289 |
processException(e); |
2 | 290 |
} catch (Error e) { |
1961
436a5a828d9f
6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents:
2
diff
changeset
|
291 |
processException(e); |
2 | 292 |
} |
293 |
return true; |
|
294 |
} |
|
295 |
||
1961
436a5a828d9f
6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents:
2
diff
changeset
|
296 |
private void processException(Throwable e) { |
2 | 297 |
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
|
298 |
eventLog.log(Level.FINE, "Processing exception: " + e); |
2 | 299 |
} |
1961
436a5a828d9f
6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents:
2
diff
changeset
|
300 |
getUncaughtExceptionHandler().uncaughtException(this, e); |
436a5a828d9f
6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents:
2
diff
changeset
|
301 |
// don't rethrow the exception to avoid EDT recreation |
2 | 302 |
} |
303 |
||
304 |
boolean isDispatching(EventQueue eq) { |
|
305 |
return theQueue.equals(eq); |
|
306 |
} |
|
307 |
||
308 |
EventQueue getEventQueue() { return theQueue; } |
|
309 |
||
310 |
private static class HierarchyEventFilter implements EventFilter { |
|
311 |
private Component modalComponent; |
|
312 |
public HierarchyEventFilter(Component modalComponent) { |
|
313 |
this.modalComponent = modalComponent; |
|
314 |
} |
|
315 |
public FilterAction acceptEvent(AWTEvent event) { |
|
316 |
if (modalComponent != null) { |
|
317 |
int eventID = event.getID(); |
|
318 |
boolean mouseEvent = (eventID >= MouseEvent.MOUSE_FIRST) && |
|
319 |
(eventID <= MouseEvent.MOUSE_LAST); |
|
320 |
boolean actionEvent = (eventID >= ActionEvent.ACTION_FIRST) && |
|
321 |
(eventID <= ActionEvent.ACTION_LAST); |
|
322 |
boolean windowClosingEvent = (eventID == WindowEvent.WINDOW_CLOSING); |
|
323 |
/* |
|
324 |
* filter out MouseEvent and ActionEvent that's outside |
|
325 |
* the modalComponent hierarchy. |
|
326 |
* KeyEvent is handled by using enqueueKeyEvent |
|
327 |
* in Dialog.show |
|
328 |
*/ |
|
329 |
if (Component.isInstanceOf(modalComponent, "javax.swing.JInternalFrame")) { |
|
330 |
/* |
|
331 |
* Modal internal frames are handled separately. If event is |
|
332 |
* for some component from another heavyweight than modalComp, |
|
333 |
* it is accepted. If heavyweight is the same - we still accept |
|
334 |
* event and perform further filtering in LightweightDispatcher |
|
335 |
*/ |
|
336 |
return windowClosingEvent ? FilterAction.REJECT : FilterAction.ACCEPT; |
|
337 |
} |
|
338 |
if (mouseEvent || actionEvent || windowClosingEvent) { |
|
339 |
Object o = event.getSource(); |
|
340 |
if (o instanceof sun.awt.ModalExclude) { |
|
341 |
// Exclude this object from modality and |
|
342 |
// continue to pump it's events. |
|
343 |
return FilterAction.ACCEPT; |
|
344 |
} else if (o instanceof Component) { |
|
345 |
Component c = (Component) o; |
|
346 |
// 5.0u3 modal exclusion |
|
347 |
boolean modalExcluded = false; |
|
348 |
if (modalComponent instanceof Container) { |
|
349 |
while (c != modalComponent && c != null) { |
|
350 |
if ((c instanceof Window) && |
|
351 |
(sun.awt.SunToolkit.isModalExcluded((Window)c))) { |
|
352 |
// Exclude this window and all its children from |
|
353 |
// modality and continue to pump it's events. |
|
354 |
modalExcluded = true; |
|
355 |
break; |
|
356 |
} |
|
357 |
c = c.getParent(); |
|
358 |
} |
|
359 |
} |
|
360 |
if (!modalExcluded && (c != modalComponent)) { |
|
361 |
return FilterAction.REJECT; |
|
362 |
} |
|
363 |
} |
|
364 |
} |
|
365 |
} |
|
366 |
return FilterAction.ACCEPT; |
|
367 |
} |
|
368 |
} |
|
369 |
} |