author | henryjen |
Tue, 10 Jun 2014 16:18:54 -0700 | |
changeset 24865 | 09b1d992ca72 |
parent 21263 | 77142152d60f |
child 25206 | f1ed7d27ec7f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
21263 | 2 |
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.awt; |
|
27 |
||
28 |
import java.awt.event.MouseEvent; |
|
29 |
import java.awt.event.ActionEvent; |
|
30 |
import java.awt.event.WindowEvent; |
|
31 |
||
10583
8bb208d39fb1
7081670: Disposing an AppContext can lead to a spinning EventDispatchThread
anthony
parents:
7668
diff
changeset
|
32 |
import java.util.ArrayList; |
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
1979
diff
changeset
|
33 |
import sun.util.logging.PlatformLogger; |
2 | 34 |
|
35 |
import sun.awt.dnd.SunDragSourceContextPeer; |
|
1293 | 36 |
import sun.awt.EventQueueDelegate; |
2 | 37 |
|
38 |
/** |
|
39 |
* EventDispatchThread is a package-private AWT class which takes |
|
40 |
* events off the EventQueue and dispatches them to the appropriate |
|
41 |
* AWT components. |
|
42 |
* |
|
43 |
* The Thread starts a "permanent" event pump with a call to |
|
44 |
* pumpEvents(Conditional) in its run() method. Event handlers can choose to |
|
45 |
* block this event pump at any time, but should start a new pump (<b>not</b> |
|
46 |
* a new EventDispatchThread) by again calling pumpEvents(Conditional). This |
|
47 |
* secondary event pump will exit automatically as soon as the Condtional |
|
48 |
* evaluate()s to false and an additional Event is pumped and dispatched. |
|
49 |
* |
|
50 |
* @author Tom Ball |
|
51 |
* @author Amy Fowler |
|
52 |
* @author Fred Ecks |
|
53 |
* @author David Mendenhall |
|
54 |
* |
|
55 |
* @since 1.1 |
|
56 |
*/ |
|
57 |
class EventDispatchThread extends Thread { |
|
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
58 |
|
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
1979
diff
changeset
|
59 |
private static final PlatformLogger eventLog = PlatformLogger.getLogger("java.awt.event.EventDispatchThread"); |
2 | 60 |
|
61 |
private EventQueue theQueue; |
|
21263 | 62 |
private volatile boolean doDispatch = true; |
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
63 |
|
2 | 64 |
private static final int ANY_EVENT = -1; |
65 |
||
10583
8bb208d39fb1
7081670: Disposing an AppContext can lead to a spinning EventDispatchThread
anthony
parents:
7668
diff
changeset
|
66 |
private ArrayList<EventFilter> eventFilters = new ArrayList<EventFilter>(); |
2 | 67 |
|
68 |
EventDispatchThread(ThreadGroup group, String name, EventQueue queue) { |
|
69 |
super(group, name); |
|
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
70 |
setEventQueue(queue); |
2 | 71 |
} |
72 |
||
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
73 |
/* |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
74 |
* Must be called on EDT only, that's why no synchronization |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
75 |
*/ |
2 | 76 |
public void stopDispatching() { |
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
77 |
doDispatch = false; |
2 | 78 |
} |
79 |
||
80 |
public void run() { |
|
21263 | 81 |
try { |
82 |
pumpEvents(new Conditional() { |
|
83 |
public boolean evaluate() { |
|
84 |
return true; |
|
2 | 85 |
} |
21263 | 86 |
}); |
87 |
} finally { |
|
88 |
getEventQueue().detachDispatchThread(this); |
|
2 | 89 |
} |
90 |
} |
|
91 |
||
92 |
void pumpEvents(Conditional cond) { |
|
93 |
pumpEvents(ANY_EVENT, cond); |
|
94 |
} |
|
95 |
||
96 |
void pumpEventsForHierarchy(Conditional cond, Component modalComponent) { |
|
97 |
pumpEventsForHierarchy(ANY_EVENT, cond, modalComponent); |
|
98 |
} |
|
99 |
||
100 |
void pumpEvents(int id, Conditional cond) { |
|
101 |
pumpEventsForHierarchy(id, cond, null); |
|
102 |
} |
|
103 |
||
6484
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
104 |
void pumpEventsForHierarchy(int id, Conditional cond, Component modalComponent) { |
2 | 105 |
pumpEventsForFilter(id, cond, new HierarchyEventFilter(modalComponent)); |
106 |
} |
|
107 |
||
108 |
void pumpEventsForFilter(Conditional cond, EventFilter filter) { |
|
109 |
pumpEventsForFilter(ANY_EVENT, cond, filter); |
|
110 |
} |
|
111 |
||
112 |
void pumpEventsForFilter(int id, Conditional cond, EventFilter filter) { |
|
113 |
addEventFilter(filter); |
|
6484
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
114 |
doDispatch = true; |
21263 | 115 |
while (doDispatch && !isInterrupted() && cond.evaluate()) { |
10583
8bb208d39fb1
7081670: Disposing an AppContext can lead to a spinning EventDispatchThread
anthony
parents:
7668
diff
changeset
|
116 |
pumpOneEventForFilters(id); |
2 | 117 |
} |
118 |
removeEventFilter(filter); |
|
119 |
} |
|
120 |
||
121 |
void addEventFilter(EventFilter filter) { |
|
18178
ee71c923891d
8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents:
16839
diff
changeset
|
122 |
if (eventLog.isLoggable(PlatformLogger.Level.FINEST)) { |
16839
d0f2e97b7359
8010297: Missing isLoggable() checks in logging code
anthony
parents:
15639
diff
changeset
|
123 |
eventLog.finest("adding the event filter: " + filter); |
d0f2e97b7359
8010297: Missing isLoggable() checks in logging code
anthony
parents:
15639
diff
changeset
|
124 |
} |
2 | 125 |
synchronized (eventFilters) { |
126 |
if (!eventFilters.contains(filter)) { |
|
127 |
if (filter instanceof ModalEventFilter) { |
|
128 |
ModalEventFilter newFilter = (ModalEventFilter)filter; |
|
129 |
int k = 0; |
|
130 |
for (k = 0; k < eventFilters.size(); k++) { |
|
131 |
EventFilter f = eventFilters.get(k); |
|
132 |
if (f instanceof ModalEventFilter) { |
|
133 |
ModalEventFilter cf = (ModalEventFilter)f; |
|
134 |
if (cf.compareTo(newFilter) > 0) { |
|
135 |
break; |
|
136 |
} |
|
137 |
} |
|
138 |
} |
|
139 |
eventFilters.add(k, filter); |
|
140 |
} else { |
|
141 |
eventFilters.add(filter); |
|
142 |
} |
|
143 |
} |
|
144 |
} |
|
145 |
} |
|
146 |
||
147 |
void removeEventFilter(EventFilter filter) { |
|
18178
ee71c923891d
8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents:
16839
diff
changeset
|
148 |
if (eventLog.isLoggable(PlatformLogger.Level.FINEST)) { |
16839
d0f2e97b7359
8010297: Missing isLoggable() checks in logging code
anthony
parents:
15639
diff
changeset
|
149 |
eventLog.finest("removing the event filter: " + filter); |
d0f2e97b7359
8010297: Missing isLoggable() checks in logging code
anthony
parents:
15639
diff
changeset
|
150 |
} |
2 | 151 |
synchronized (eventFilters) { |
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
152 |
eventFilters.remove(filter); |
2 | 153 |
} |
154 |
} |
|
155 |
||
10583
8bb208d39fb1
7081670: Disposing an AppContext can lead to a spinning EventDispatchThread
anthony
parents:
7668
diff
changeset
|
156 |
void pumpOneEventForFilters(int id) { |
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
157 |
AWTEvent event = null; |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
158 |
boolean eventOK = false; |
2 | 159 |
try { |
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
160 |
EventQueue eq = null; |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
161 |
EventQueueDelegate.Delegate delegate = null; |
2 | 162 |
do { |
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
163 |
// EventQueue may change during the dispatching |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
164 |
eq = getEventQueue(); |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
165 |
delegate = EventQueueDelegate.getDelegate(); |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
166 |
|
1293 | 167 |
if (delegate != null && id == ANY_EVENT) { |
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
168 |
event = delegate.getNextEvent(eq); |
1293 | 169 |
} else { |
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
170 |
event = (id == ANY_EVENT) ? eq.getNextEvent() : eq.getNextEvent(id); |
1293 | 171 |
} |
2 | 172 |
|
173 |
eventOK = true; |
|
174 |
synchronized (eventFilters) { |
|
175 |
for (int i = eventFilters.size() - 1; i >= 0; i--) { |
|
176 |
EventFilter f = eventFilters.get(i); |
|
177 |
EventFilter.FilterAction accept = f.acceptEvent(event); |
|
178 |
if (accept == EventFilter.FilterAction.REJECT) { |
|
179 |
eventOK = false; |
|
180 |
break; |
|
181 |
} else if (accept == EventFilter.FilterAction.ACCEPT_IMMEDIATELY) { |
|
182 |
break; |
|
183 |
} |
|
184 |
} |
|
185 |
} |
|
186 |
eventOK = eventOK && SunDragSourceContextPeer.checkEvent(event); |
|
187 |
if (!eventOK) { |
|
188 |
event.consume(); |
|
189 |
} |
|
190 |
} |
|
191 |
while (eventOK == false); |
|
192 |
||
18178
ee71c923891d
8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents:
16839
diff
changeset
|
193 |
if (eventLog.isLoggable(PlatformLogger.Level.FINEST)) { |
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
1979
diff
changeset
|
194 |
eventLog.finest("Dispatching: " + event); |
2 | 195 |
} |
196 |
||
1293 | 197 |
Object handle = null; |
198 |
if (delegate != null) { |
|
199 |
handle = delegate.beforeDispatch(event); |
|
200 |
} |
|
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
201 |
eq.dispatchEvent(event); |
1293 | 202 |
if (delegate != null) { |
203 |
delegate.afterDispatch(event, handle); |
|
204 |
} |
|
2 | 205 |
} |
206 |
catch (ThreadDeath death) { |
|
21263 | 207 |
doDispatch = false; |
10583
8bb208d39fb1
7081670: Disposing an AppContext can lead to a spinning EventDispatchThread
anthony
parents:
7668
diff
changeset
|
208 |
throw death; |
2 | 209 |
} |
210 |
catch (InterruptedException interruptedException) { |
|
21263 | 211 |
doDispatch = false; // AppContext.dispose() interrupts all |
212 |
// Threads in the AppContext |
|
2 | 213 |
} |
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
214 |
catch (Throwable e) { |
1961
436a5a828d9f
6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents:
2
diff
changeset
|
215 |
processException(e); |
2 | 216 |
} |
217 |
} |
|
218 |
||
1961
436a5a828d9f
6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents:
2
diff
changeset
|
219 |
private void processException(Throwable e) { |
18178
ee71c923891d
8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents:
16839
diff
changeset
|
220 |
if (eventLog.isLoggable(PlatformLogger.Level.FINE)) { |
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
1979
diff
changeset
|
221 |
eventLog.fine("Processing exception: " + e); |
2 | 222 |
} |
1961
436a5a828d9f
6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents:
2
diff
changeset
|
223 |
getUncaughtExceptionHandler().uncaughtException(this, e); |
2 | 224 |
} |
225 |
||
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
226 |
public synchronized EventQueue getEventQueue() { |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
227 |
return theQueue; |
2 | 228 |
} |
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
229 |
public synchronized void setEventQueue(EventQueue eq) { |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
230 |
theQueue = eq; |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
231 |
} |
2 | 232 |
|
233 |
private static class HierarchyEventFilter implements EventFilter { |
|
234 |
private Component modalComponent; |
|
235 |
public HierarchyEventFilter(Component modalComponent) { |
|
236 |
this.modalComponent = modalComponent; |
|
237 |
} |
|
238 |
public FilterAction acceptEvent(AWTEvent event) { |
|
239 |
if (modalComponent != null) { |
|
240 |
int eventID = event.getID(); |
|
241 |
boolean mouseEvent = (eventID >= MouseEvent.MOUSE_FIRST) && |
|
242 |
(eventID <= MouseEvent.MOUSE_LAST); |
|
243 |
boolean actionEvent = (eventID >= ActionEvent.ACTION_FIRST) && |
|
244 |
(eventID <= ActionEvent.ACTION_LAST); |
|
245 |
boolean windowClosingEvent = (eventID == WindowEvent.WINDOW_CLOSING); |
|
246 |
/* |
|
247 |
* filter out MouseEvent and ActionEvent that's outside |
|
248 |
* the modalComponent hierarchy. |
|
249 |
* KeyEvent is handled by using enqueueKeyEvent |
|
250 |
* in Dialog.show |
|
251 |
*/ |
|
252 |
if (Component.isInstanceOf(modalComponent, "javax.swing.JInternalFrame")) { |
|
253 |
/* |
|
254 |
* Modal internal frames are handled separately. If event is |
|
255 |
* for some component from another heavyweight than modalComp, |
|
256 |
* it is accepted. If heavyweight is the same - we still accept |
|
257 |
* event and perform further filtering in LightweightDispatcher |
|
258 |
*/ |
|
259 |
return windowClosingEvent ? FilterAction.REJECT : FilterAction.ACCEPT; |
|
260 |
} |
|
261 |
if (mouseEvent || actionEvent || windowClosingEvent) { |
|
262 |
Object o = event.getSource(); |
|
263 |
if (o instanceof sun.awt.ModalExclude) { |
|
264 |
// Exclude this object from modality and |
|
265 |
// continue to pump it's events. |
|
266 |
return FilterAction.ACCEPT; |
|
267 |
} else if (o instanceof Component) { |
|
268 |
Component c = (Component) o; |
|
269 |
// 5.0u3 modal exclusion |
|
270 |
boolean modalExcluded = false; |
|
271 |
if (modalComponent instanceof Container) { |
|
272 |
while (c != modalComponent && c != null) { |
|
273 |
if ((c instanceof Window) && |
|
274 |
(sun.awt.SunToolkit.isModalExcluded((Window)c))) { |
|
275 |
// Exclude this window and all its children from |
|
276 |
// modality and continue to pump it's events. |
|
277 |
modalExcluded = true; |
|
278 |
break; |
|
279 |
} |
|
280 |
c = c.getParent(); |
|
281 |
} |
|
282 |
} |
|
283 |
if (!modalExcluded && (c != modalComponent)) { |
|
284 |
return FilterAction.REJECT; |
|
285 |
} |
|
286 |
} |
|
287 |
} |
|
288 |
} |
|
289 |
return FilterAction.ACCEPT; |
|
290 |
} |
|
291 |
} |
|
292 |
} |