author | ohair |
Tue, 28 Dec 2010 15:53:50 -0800 | |
changeset 7668 | d4a77089c587 |
parent 6484 | f5dbd940a640 |
child 8816 | 29f983feda95 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 1996, 2010, 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.*; |
|
29 |
||
30 |
import java.awt.peer.ComponentPeer; |
|
31 |
||
32 |
import java.lang.ref.WeakReference; |
|
33 |
import java.lang.reflect.InvocationTargetException; |
|
34 |
||
35 |
import java.security.AccessController; |
|
36 |
import java.security.PrivilegedAction; |
|
37 |
||
38 |
import java.util.EmptyStackException; |
|
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
3084
diff
changeset
|
39 |
import sun.util.logging.PlatformLogger; |
2 | 40 |
|
41 |
import sun.awt.AppContext; |
|
42 |
import sun.awt.AWTAutoShutdown; |
|
43 |
import sun.awt.PeerEvent; |
|
44 |
import sun.awt.SunToolkit; |
|
1181
c5971dbeaaaa
6693974: Unify EventQueue$EventQueueItem and SunToolkit.$EventQueueItem classes
dav
parents:
2
diff
changeset
|
45 |
import sun.awt.EventQueueItem; |
3084
67ca55732362
6824169: Need to remove some AWT class dependencies
dcherepanov
parents:
1247
diff
changeset
|
46 |
import sun.awt.AWTAccessor; |
2 | 47 |
|
4365 | 48 |
import java.util.concurrent.locks.Condition; |
49 |
import java.util.concurrent.locks.Lock; |
|
50 |
||
2 | 51 |
/** |
52 |
* <code>EventQueue</code> is a platform-independent class |
|
53 |
* that queues events, both from the underlying peer classes |
|
54 |
* and from trusted application classes. |
|
55 |
* <p> |
|
56 |
* It encapsulates asynchronous event dispatch machinery which |
|
57 |
* extracts events from the queue and dispatches them by calling |
|
58 |
* {@link #dispatchEvent(AWTEvent) dispatchEvent(AWTEvent)} method |
|
59 |
* on this <code>EventQueue</code> with the event to be dispatched |
|
60 |
* as an argument. The particular behavior of this machinery is |
|
61 |
* implementation-dependent. The only requirements are that events |
|
62 |
* which were actually enqueued to this queue (note that events |
|
63 |
* being posted to the <code>EventQueue</code> can be coalesced) |
|
64 |
* are dispatched: |
|
65 |
* <dl> |
|
66 |
* <dt> Sequentially. |
|
67 |
* <dd> That is, it is not permitted that several events from |
|
68 |
* this queue are dispatched simultaneously. |
|
69 |
* <dt> In the same order as they are enqueued. |
|
70 |
* <dd> That is, if <code>AWTEvent</code> A is enqueued |
|
71 |
* to the <code>EventQueue</code> before |
|
72 |
* <code>AWTEvent</code> B then event B will not be |
|
73 |
* dispatched before event A. |
|
74 |
* </dl> |
|
75 |
* <p> |
|
76 |
* Some browsers partition applets in different code bases into |
|
77 |
* separate contexts, and establish walls between these contexts. |
|
78 |
* In such a scenario, there will be one <code>EventQueue</code> |
|
79 |
* per context. Other browsers place all applets into the same |
|
80 |
* context, implying that there will be only a single, global |
|
81 |
* <code>EventQueue</code> for all applets. This behavior is |
|
82 |
* implementation-dependent. Consult your browser's documentation |
|
83 |
* for more information. |
|
84 |
* <p> |
|
85 |
* For information on the threading issues of the event dispatch |
|
86 |
* machinery, see <a href="doc-files/AWTThreadIssues.html#Autoshutdown" |
|
87 |
* >AWT Threading Issues</a>. |
|
88 |
* |
|
89 |
* @author Thomas Ball |
|
90 |
* @author Fred Ecks |
|
91 |
* @author David Mendenhall |
|
92 |
* |
|
93 |
* @since 1.1 |
|
94 |
*/ |
|
95 |
public class EventQueue { |
|
96 |
||
97 |
// From Thread.java |
|
98 |
private static int threadInitNumber; |
|
99 |
private static synchronized int nextThreadNum() { |
|
100 |
return threadInitNumber++; |
|
101 |
} |
|
102 |
||
103 |
private static final int LOW_PRIORITY = 0; |
|
104 |
private static final int NORM_PRIORITY = 1; |
|
105 |
private static final int HIGH_PRIORITY = 2; |
|
106 |
private static final int ULTIMATE_PRIORITY = 3; |
|
107 |
||
108 |
private static final int NUM_PRIORITIES = ULTIMATE_PRIORITY + 1; |
|
109 |
||
110 |
/* |
|
111 |
* We maintain one Queue for each priority that the EventQueue supports. |
|
112 |
* That is, the EventQueue object is actually implemented as |
|
113 |
* NUM_PRIORITIES queues and all Events on a particular internal Queue |
|
114 |
* have identical priority. Events are pulled off the EventQueue starting |
|
115 |
* with the Queue of highest priority. We progress in decreasing order |
|
116 |
* across all Queues. |
|
117 |
*/ |
|
118 |
private Queue[] queues = new Queue[NUM_PRIORITIES]; |
|
119 |
||
120 |
/* |
|
121 |
* The next EventQueue on the stack, or null if this EventQueue is |
|
122 |
* on the top of the stack. If nextQueue is non-null, requests to post |
|
123 |
* an event are forwarded to nextQueue. |
|
124 |
*/ |
|
125 |
private EventQueue nextQueue; |
|
126 |
||
127 |
/* |
|
128 |
* The previous EventQueue on the stack, or null if this is the |
|
129 |
* "base" EventQueue. |
|
130 |
*/ |
|
131 |
private EventQueue previousQueue; |
|
132 |
||
4365 | 133 |
/* |
134 |
* A single lock to synchronize the push()/pop() and related operations with |
|
135 |
* all the EventQueues from the AppContext. Synchronization on any particular |
|
136 |
* event queue(s) is not enough: we should lock the whole stack. |
|
137 |
*/ |
|
138 |
private final Lock pushPopLock; |
|
139 |
private final Condition pushPopCond; |
|
140 |
||
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
141 |
/* |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
142 |
* Dummy runnable to wake up EDT from getNextEvent() after |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
143 |
push/pop is performed |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
144 |
*/ |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
145 |
private final static Runnable dummyRunnable = new Runnable() { |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
146 |
public void run() { |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
147 |
} |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
148 |
}; |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
149 |
|
2 | 150 |
private EventDispatchThread dispatchThread; |
151 |
||
152 |
private final ThreadGroup threadGroup = |
|
153 |
Thread.currentThread().getThreadGroup(); |
|
154 |
private final ClassLoader classLoader = |
|
155 |
Thread.currentThread().getContextClassLoader(); |
|
156 |
||
157 |
/* |
|
158 |
* The time stamp of the last dispatched InputEvent or ActionEvent. |
|
159 |
*/ |
|
160 |
private long mostRecentEventTime = System.currentTimeMillis(); |
|
161 |
||
162 |
/** |
|
163 |
* The modifiers field of the current event, if the current event is an |
|
164 |
* InputEvent or ActionEvent. |
|
165 |
*/ |
|
166 |
private WeakReference currentEvent; |
|
167 |
||
168 |
/* |
|
169 |
* Non-zero if a thread is waiting in getNextEvent(int) for an event of |
|
170 |
* a particular ID to be posted to the queue. |
|
171 |
*/ |
|
172 |
private int waitForID; |
|
173 |
||
174 |
private final String name = "AWT-EventQueue-" + nextThreadNum(); |
|
175 |
||
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
3084
diff
changeset
|
176 |
private static final PlatformLogger eventLog = PlatformLogger.getLogger("java.awt.event.EventQueue"); |
2 | 177 |
|
3084
67ca55732362
6824169: Need to remove some AWT class dependencies
dcherepanov
parents:
1247
diff
changeset
|
178 |
static { |
67ca55732362
6824169: Need to remove some AWT class dependencies
dcherepanov
parents:
1247
diff
changeset
|
179 |
AWTAccessor.setEventQueueAccessor( |
67ca55732362
6824169: Need to remove some AWT class dependencies
dcherepanov
parents:
1247
diff
changeset
|
180 |
new AWTAccessor.EventQueueAccessor() { |
4365 | 181 |
public Thread getDispatchThread(EventQueue eventQueue) { |
182 |
return eventQueue.getDispatchThread(); |
|
3084
67ca55732362
6824169: Need to remove some AWT class dependencies
dcherepanov
parents:
1247
diff
changeset
|
183 |
} |
4365 | 184 |
public boolean isDispatchThreadImpl(EventQueue eventQueue) { |
185 |
return eventQueue.isDispatchThreadImpl(); |
|
3084
67ca55732362
6824169: Need to remove some AWT class dependencies
dcherepanov
parents:
1247
diff
changeset
|
186 |
} |
67ca55732362
6824169: Need to remove some AWT class dependencies
dcherepanov
parents:
1247
diff
changeset
|
187 |
}); |
67ca55732362
6824169: Need to remove some AWT class dependencies
dcherepanov
parents:
1247
diff
changeset
|
188 |
} |
67ca55732362
6824169: Need to remove some AWT class dependencies
dcherepanov
parents:
1247
diff
changeset
|
189 |
|
2 | 190 |
public EventQueue() { |
191 |
for (int i = 0; i < NUM_PRIORITIES; i++) { |
|
192 |
queues[i] = new Queue(); |
|
193 |
} |
|
194 |
/* |
|
195 |
* NOTE: if you ever have to start the associated event dispatch |
|
196 |
* thread at this point, be aware of the following problem: |
|
197 |
* If this EventQueue instance is created in |
|
198 |
* SunToolkit.createNewAppContext() the started dispatch thread |
|
199 |
* may call AppContext.getAppContext() before createNewAppContext() |
|
200 |
* completes thus causing mess in thread group to appcontext mapping. |
|
201 |
*/ |
|
4365 | 202 |
|
203 |
pushPopLock = (Lock)AppContext.getAppContext().get(AppContext.EVENT_QUEUE_LOCK_KEY); |
|
204 |
pushPopCond = (Condition)AppContext.getAppContext().get(AppContext.EVENT_QUEUE_COND_KEY); |
|
2 | 205 |
} |
206 |
||
207 |
/** |
|
208 |
* Posts a 1.1-style event to the <code>EventQueue</code>. |
|
209 |
* If there is an existing event on the queue with the same ID |
|
210 |
* and event source, the source <code>Component</code>'s |
|
211 |
* <code>coalesceEvents</code> method will be called. |
|
212 |
* |
|
213 |
* @param theEvent an instance of <code>java.awt.AWTEvent</code>, |
|
214 |
* or a subclass of it |
|
215 |
* @throws NullPointerException if <code>theEvent</code> is <code>null</code> |
|
216 |
*/ |
|
217 |
public void postEvent(AWTEvent theEvent) { |
|
218 |
SunToolkit.flushPendingEvents(); |
|
219 |
postEventPrivate(theEvent); |
|
220 |
} |
|
221 |
||
222 |
/** |
|
223 |
* Posts a 1.1-style event to the <code>EventQueue</code>. |
|
224 |
* If there is an existing event on the queue with the same ID |
|
225 |
* and event source, the source <code>Component</code>'s |
|
226 |
* <code>coalesceEvents</code> method will be called. |
|
227 |
* |
|
228 |
* @param theEvent an instance of <code>java.awt.AWTEvent</code>, |
|
229 |
* or a subclass of it |
|
230 |
*/ |
|
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
231 |
private final void postEventPrivate(AWTEvent theEvent) { |
2 | 232 |
theEvent.isPosted = true; |
4365 | 233 |
pushPopLock.lock(); |
234 |
try { |
|
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
235 |
if (nextQueue != null) { |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
236 |
// Forward the event to the top of EventQueue stack |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
237 |
nextQueue.postEventPrivate(theEvent); |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
238 |
return; |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
239 |
} |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
240 |
if (dispatchThread == null) { |
2 | 241 |
if (theEvent.getSource() == AWTAutoShutdown.getInstance()) { |
242 |
return; |
|
243 |
} else { |
|
244 |
initDispatchThread(); |
|
245 |
} |
|
246 |
} |
|
247 |
postEvent(theEvent, getPriority(theEvent)); |
|
4365 | 248 |
} finally { |
249 |
pushPopLock.unlock(); |
|
2 | 250 |
} |
251 |
} |
|
252 |
||
253 |
private static int getPriority(AWTEvent theEvent) { |
|
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
254 |
if (theEvent instanceof PeerEvent) { |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
255 |
PeerEvent peerEvent = (PeerEvent)theEvent; |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
256 |
if ((peerEvent.getFlags() & PeerEvent.ULTIMATE_PRIORITY_EVENT) != 0) { |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
257 |
return ULTIMATE_PRIORITY; |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
258 |
} |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
259 |
if ((peerEvent.getFlags() & PeerEvent.PRIORITY_EVENT) != 0) { |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
260 |
return HIGH_PRIORITY; |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
261 |
} |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
262 |
if ((peerEvent.getFlags() & PeerEvent.LOW_PRIORITY_EVENT) != 0) { |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
263 |
return LOW_PRIORITY; |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
264 |
} |
2 | 265 |
} |
266 |
int id = theEvent.getID(); |
|
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
267 |
if ((id >= PaintEvent.PAINT_FIRST) && (id <= PaintEvent.PAINT_LAST)) { |
2 | 268 |
return LOW_PRIORITY; |
269 |
} |
|
270 |
return NORM_PRIORITY; |
|
271 |
} |
|
272 |
||
273 |
/** |
|
274 |
* Posts the event to the internal Queue of specified priority, |
|
275 |
* coalescing as appropriate. |
|
276 |
* |
|
277 |
* @param theEvent an instance of <code>java.awt.AWTEvent</code>, |
|
278 |
* or a subclass of it |
|
279 |
* @param priority the desired priority of the event |
|
280 |
*/ |
|
281 |
private void postEvent(AWTEvent theEvent, int priority) { |
|
282 |
if (coalesceEvent(theEvent, priority)) { |
|
283 |
return; |
|
284 |
} |
|
285 |
||
286 |
EventQueueItem newItem = new EventQueueItem(theEvent); |
|
287 |
||
288 |
cacheEQItem(newItem); |
|
289 |
||
290 |
boolean notifyID = (theEvent.getID() == this.waitForID); |
|
291 |
||
292 |
if (queues[priority].head == null) { |
|
293 |
boolean shouldNotify = noEvents(); |
|
294 |
queues[priority].head = queues[priority].tail = newItem; |
|
295 |
||
296 |
if (shouldNotify) { |
|
297 |
if (theEvent.getSource() != AWTAutoShutdown.getInstance()) { |
|
298 |
AWTAutoShutdown.getInstance().notifyThreadBusy(dispatchThread); |
|
299 |
} |
|
4365 | 300 |
pushPopCond.signalAll(); |
2 | 301 |
} else if (notifyID) { |
4365 | 302 |
pushPopCond.signalAll(); |
2 | 303 |
} |
304 |
} else { |
|
305 |
// The event was not coalesced or has non-Component source. |
|
306 |
// Insert it at the end of the appropriate Queue. |
|
307 |
queues[priority].tail.next = newItem; |
|
308 |
queues[priority].tail = newItem; |
|
309 |
if (notifyID) { |
|
4365 | 310 |
pushPopCond.signalAll(); |
2 | 311 |
} |
312 |
} |
|
313 |
} |
|
314 |
||
315 |
private boolean coalescePaintEvent(PaintEvent e) { |
|
316 |
ComponentPeer sourcePeer = ((Component)e.getSource()).peer; |
|
317 |
if (sourcePeer != null) { |
|
318 |
sourcePeer.coalescePaintEvent(e); |
|
319 |
} |
|
320 |
EventQueueItem[] cache = ((Component)e.getSource()).eventCache; |
|
321 |
if (cache == null) { |
|
322 |
return false; |
|
323 |
} |
|
324 |
int index = eventToCacheIndex(e); |
|
325 |
||
326 |
if (index != -1 && cache[index] != null) { |
|
327 |
PaintEvent merged = mergePaintEvents(e, (PaintEvent)cache[index].event); |
|
328 |
if (merged != null) { |
|
329 |
cache[index].event = merged; |
|
330 |
return true; |
|
331 |
} |
|
332 |
} |
|
333 |
return false; |
|
334 |
} |
|
335 |
||
336 |
private PaintEvent mergePaintEvents(PaintEvent a, PaintEvent b) { |
|
337 |
Rectangle aRect = a.getUpdateRect(); |
|
338 |
Rectangle bRect = b.getUpdateRect(); |
|
339 |
if (bRect.contains(aRect)) { |
|
340 |
return b; |
|
341 |
} |
|
342 |
if (aRect.contains(bRect)) { |
|
343 |
return a; |
|
344 |
} |
|
345 |
return null; |
|
346 |
} |
|
347 |
||
348 |
private boolean coalesceMouseEvent(MouseEvent e) { |
|
349 |
EventQueueItem[] cache = ((Component)e.getSource()).eventCache; |
|
350 |
if (cache == null) { |
|
351 |
return false; |
|
352 |
} |
|
353 |
int index = eventToCacheIndex(e); |
|
354 |
if (index != -1 && cache[index] != null) { |
|
355 |
cache[index].event = e; |
|
356 |
return true; |
|
357 |
} |
|
358 |
return false; |
|
359 |
} |
|
360 |
||
361 |
private boolean coalescePeerEvent(PeerEvent e) { |
|
362 |
EventQueueItem[] cache = ((Component)e.getSource()).eventCache; |
|
363 |
if (cache == null) { |
|
364 |
return false; |
|
365 |
} |
|
366 |
int index = eventToCacheIndex(e); |
|
367 |
if (index != -1 && cache[index] != null) { |
|
368 |
e = e.coalesceEvents((PeerEvent)cache[index].event); |
|
369 |
if (e != null) { |
|
370 |
cache[index].event = e; |
|
371 |
return true; |
|
372 |
} else { |
|
373 |
cache[index] = null; |
|
374 |
} |
|
375 |
} |
|
376 |
return false; |
|
377 |
} |
|
378 |
||
379 |
/* |
|
380 |
* Should avoid of calling this method by any means |
|
381 |
* as it's working time is dependant on EQ length. |
|
382 |
* In the wors case this method alone can slow down the entire application |
|
383 |
* 10 times by stalling the Event processing. |
|
384 |
* Only here by backward compatibility reasons. |
|
385 |
*/ |
|
386 |
private boolean coalesceOtherEvent(AWTEvent e, int priority) { |
|
387 |
int id = e.getID(); |
|
388 |
Component source = (Component)e.getSource(); |
|
389 |
for (EventQueueItem entry = queues[priority].head; |
|
390 |
entry != null; entry = entry.next) |
|
391 |
{ |
|
392 |
// Give Component.coalesceEvents a chance |
|
1181
c5971dbeaaaa
6693974: Unify EventQueue$EventQueueItem and SunToolkit.$EventQueueItem classes
dav
parents:
2
diff
changeset
|
393 |
if (entry.event.getSource() == source && entry.event.getID() == id) { |
2 | 394 |
AWTEvent coalescedEvent = source.coalesceEvents( |
395 |
entry.event, e); |
|
396 |
if (coalescedEvent != null) { |
|
397 |
entry.event = coalescedEvent; |
|
398 |
return true; |
|
399 |
} |
|
400 |
} |
|
401 |
} |
|
402 |
return false; |
|
403 |
} |
|
404 |
||
405 |
private boolean coalesceEvent(AWTEvent e, int priority) { |
|
406 |
if (!(e.getSource() instanceof Component)) { |
|
407 |
return false; |
|
408 |
} |
|
409 |
if (e instanceof PeerEvent) { |
|
410 |
return coalescePeerEvent((PeerEvent)e); |
|
411 |
} |
|
412 |
// The worst case |
|
413 |
if (((Component)e.getSource()).isCoalescingEnabled() |
|
414 |
&& coalesceOtherEvent(e, priority)) |
|
415 |
{ |
|
416 |
return true; |
|
417 |
} |
|
418 |
if (e instanceof PaintEvent) { |
|
419 |
return coalescePaintEvent((PaintEvent)e); |
|
420 |
} |
|
421 |
if (e instanceof MouseEvent) { |
|
422 |
return coalesceMouseEvent((MouseEvent)e); |
|
423 |
} |
|
424 |
return false; |
|
425 |
} |
|
426 |
||
427 |
private void cacheEQItem(EventQueueItem entry) { |
|
428 |
int index = eventToCacheIndex(entry.event); |
|
429 |
if (index != -1 && entry.event.getSource() instanceof Component) { |
|
430 |
Component source = (Component)entry.event.getSource(); |
|
431 |
if (source.eventCache == null) { |
|
432 |
source.eventCache = new EventQueueItem[CACHE_LENGTH]; |
|
433 |
} |
|
434 |
source.eventCache[index] = entry; |
|
435 |
} |
|
436 |
} |
|
437 |
||
438 |
private void uncacheEQItem(EventQueueItem entry) { |
|
439 |
int index = eventToCacheIndex(entry.event); |
|
440 |
if (index != -1 && entry.event.getSource() instanceof Component) { |
|
441 |
Component source = (Component)entry.event.getSource(); |
|
442 |
if (source.eventCache == null) { |
|
443 |
return; |
|
444 |
} |
|
445 |
source.eventCache[index] = null; |
|
446 |
} |
|
447 |
} |
|
448 |
||
449 |
private static final int PAINT = 0; |
|
450 |
private static final int UPDATE = 1; |
|
451 |
private static final int MOVE = 2; |
|
452 |
private static final int DRAG = 3; |
|
453 |
private static final int PEER = 4; |
|
454 |
private static final int CACHE_LENGTH = 5; |
|
455 |
||
456 |
private static int eventToCacheIndex(AWTEvent e) { |
|
457 |
switch(e.getID()) { |
|
458 |
case PaintEvent.PAINT: |
|
459 |
return PAINT; |
|
460 |
case PaintEvent.UPDATE: |
|
461 |
return UPDATE; |
|
462 |
case MouseEvent.MOUSE_MOVED: |
|
463 |
return MOVE; |
|
464 |
case MouseEvent.MOUSE_DRAGGED: |
|
465 |
return DRAG; |
|
466 |
default: |
|
467 |
return e instanceof PeerEvent ? PEER : -1; |
|
468 |
} |
|
469 |
} |
|
470 |
||
471 |
/** |
|
472 |
* Returns whether an event is pending on any of the separate |
|
473 |
* Queues. |
|
474 |
* @return whether an event is pending on any of the separate Queues |
|
475 |
*/ |
|
476 |
private boolean noEvents() { |
|
477 |
for (int i = 0; i < NUM_PRIORITIES; i++) { |
|
478 |
if (queues[i].head != null) { |
|
479 |
return false; |
|
480 |
} |
|
481 |
} |
|
482 |
||
483 |
return true; |
|
484 |
} |
|
485 |
||
486 |
/** |
|
487 |
* Removes an event from the <code>EventQueue</code> and |
|
488 |
* returns it. This method will block until an event has |
|
489 |
* been posted by another thread. |
|
490 |
* @return the next <code>AWTEvent</code> |
|
491 |
* @exception InterruptedException |
|
492 |
* if any thread has interrupted this thread |
|
493 |
*/ |
|
494 |
public AWTEvent getNextEvent() throws InterruptedException { |
|
495 |
do { |
|
496 |
/* |
|
497 |
* SunToolkit.flushPendingEvents must be called outside |
|
498 |
* of the synchronized block to avoid deadlock when |
|
499 |
* event queues are nested with push()/pop(). |
|
500 |
*/ |
|
501 |
SunToolkit.flushPendingEvents(); |
|
4365 | 502 |
pushPopLock.lock(); |
503 |
try { |
|
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
504 |
AWTEvent event = getNextEventPrivate(); |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
505 |
if (event != null) { |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
506 |
return event; |
2 | 507 |
} |
508 |
AWTAutoShutdown.getInstance().notifyThreadFree(dispatchThread); |
|
4365 | 509 |
pushPopCond.await(); |
510 |
} finally { |
|
511 |
pushPopLock.unlock(); |
|
2 | 512 |
} |
513 |
} while(true); |
|
514 |
} |
|
515 |
||
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
516 |
/* |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
517 |
* Must be called under the lock. Doesn't call flushPendingEvents() |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
518 |
*/ |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
519 |
AWTEvent getNextEventPrivate() throws InterruptedException { |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
520 |
for (int i = NUM_PRIORITIES - 1; i >= 0; i--) { |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
521 |
if (queues[i].head != null) { |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
522 |
EventQueueItem entry = queues[i].head; |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
523 |
queues[i].head = entry.next; |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
524 |
if (entry.next == null) { |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
525 |
queues[i].tail = null; |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
526 |
} |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
527 |
uncacheEQItem(entry); |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
528 |
return entry.event; |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
529 |
} |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
530 |
} |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
531 |
return null; |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
532 |
} |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
533 |
|
2 | 534 |
AWTEvent getNextEvent(int id) throws InterruptedException { |
535 |
do { |
|
536 |
/* |
|
537 |
* SunToolkit.flushPendingEvents must be called outside |
|
538 |
* of the synchronized block to avoid deadlock when |
|
539 |
* event queues are nested with push()/pop(). |
|
540 |
*/ |
|
541 |
SunToolkit.flushPendingEvents(); |
|
4365 | 542 |
pushPopLock.lock(); |
543 |
try { |
|
2 | 544 |
for (int i = 0; i < NUM_PRIORITIES; i++) { |
545 |
for (EventQueueItem entry = queues[i].head, prev = null; |
|
546 |
entry != null; prev = entry, entry = entry.next) |
|
547 |
{ |
|
1181
c5971dbeaaaa
6693974: Unify EventQueue$EventQueueItem and SunToolkit.$EventQueueItem classes
dav
parents:
2
diff
changeset
|
548 |
if (entry.event.getID() == id) { |
2 | 549 |
if (prev == null) { |
550 |
queues[i].head = entry.next; |
|
551 |
} else { |
|
552 |
prev.next = entry.next; |
|
553 |
} |
|
554 |
if (queues[i].tail == entry) { |
|
555 |
queues[i].tail = prev; |
|
556 |
} |
|
557 |
uncacheEQItem(entry); |
|
558 |
return entry.event; |
|
559 |
} |
|
560 |
} |
|
561 |
} |
|
4365 | 562 |
waitForID = id; |
563 |
pushPopCond.await(); |
|
564 |
waitForID = 0; |
|
565 |
} finally { |
|
566 |
pushPopLock.unlock(); |
|
2 | 567 |
} |
568 |
} while(true); |
|
569 |
} |
|
570 |
||
571 |
/** |
|
572 |
* Returns the first event on the <code>EventQueue</code> |
|
573 |
* without removing it. |
|
574 |
* @return the first event |
|
575 |
*/ |
|
4365 | 576 |
public AWTEvent peekEvent() { |
577 |
pushPopLock.lock(); |
|
578 |
try { |
|
579 |
for (int i = NUM_PRIORITIES - 1; i >= 0; i--) { |
|
580 |
if (queues[i].head != null) { |
|
581 |
return queues[i].head.event; |
|
582 |
} |
|
2 | 583 |
} |
4365 | 584 |
} finally { |
585 |
pushPopLock.unlock(); |
|
2 | 586 |
} |
587 |
||
588 |
return null; |
|
589 |
} |
|
590 |
||
591 |
/** |
|
592 |
* Returns the first event with the specified id, if any. |
|
593 |
* @param id the id of the type of event desired |
|
594 |
* @return the first event of the specified id or <code>null</code> |
|
595 |
* if there is no such event |
|
596 |
*/ |
|
4365 | 597 |
public AWTEvent peekEvent(int id) { |
598 |
pushPopLock.lock(); |
|
599 |
try { |
|
600 |
for (int i = NUM_PRIORITIES - 1; i >= 0; i--) { |
|
601 |
EventQueueItem q = queues[i].head; |
|
602 |
for (; q != null; q = q.next) { |
|
603 |
if (q.event.getID() == id) { |
|
604 |
return q.event; |
|
605 |
} |
|
2 | 606 |
} |
607 |
} |
|
4365 | 608 |
} finally { |
609 |
pushPopLock.unlock(); |
|
2 | 610 |
} |
611 |
||
612 |
return null; |
|
613 |
} |
|
614 |
||
615 |
/** |
|
616 |
* Dispatches an event. The manner in which the event is |
|
617 |
* dispatched depends upon the type of the event and the |
|
618 |
* type of the event's source object: |
|
619 |
* <p> </p> |
|
620 |
* <table border=1 summary="Event types, source types, and dispatch methods"> |
|
621 |
* <tr> |
|
622 |
* <th>Event Type</th> |
|
623 |
* <th>Source Type</th> |
|
624 |
* <th>Dispatched To</th> |
|
625 |
* </tr> |
|
626 |
* <tr> |
|
627 |
* <td>ActiveEvent</td> |
|
628 |
* <td>Any</td> |
|
629 |
* <td>event.dispatch()</td> |
|
630 |
* </tr> |
|
631 |
* <tr> |
|
632 |
* <td>Other</td> |
|
633 |
* <td>Component</td> |
|
634 |
* <td>source.dispatchEvent(AWTEvent)</td> |
|
635 |
* </tr> |
|
636 |
* <tr> |
|
637 |
* <td>Other</td> |
|
638 |
* <td>MenuComponent</td> |
|
639 |
* <td>source.dispatchEvent(AWTEvent)</td> |
|
640 |
* </tr> |
|
641 |
* <tr> |
|
642 |
* <td>Other</td> |
|
643 |
* <td>Other</td> |
|
644 |
* <td>No action (ignored)</td> |
|
645 |
* </tr> |
|
646 |
* </table> |
|
647 |
* <p> </p> |
|
648 |
* @param event an instance of <code>java.awt.AWTEvent</code>, |
|
649 |
* or a subclass of it |
|
650 |
* @throws NullPointerException if <code>event</code> is <code>null</code> |
|
651 |
* @since 1.2 |
|
652 |
*/ |
|
653 |
protected void dispatchEvent(AWTEvent event) { |
|
654 |
event.isPosted = true; |
|
655 |
Object src = event.getSource(); |
|
656 |
if (event instanceof ActiveEvent) { |
|
657 |
// This could become the sole method of dispatching in time. |
|
658 |
setCurrentEventAndMostRecentTimeImpl(event); |
|
659 |
||
660 |
((ActiveEvent)event).dispatch(); |
|
661 |
} else if (src instanceof Component) { |
|
662 |
((Component)src).dispatchEvent(event); |
|
663 |
event.dispatched(); |
|
664 |
} else if (src instanceof MenuComponent) { |
|
665 |
((MenuComponent)src).dispatchEvent(event); |
|
666 |
} else if (src instanceof TrayIcon) { |
|
667 |
((TrayIcon)src).dispatchEvent(event); |
|
668 |
} else if (src instanceof AWTAutoShutdown) { |
|
669 |
if (noEvents()) { |
|
670 |
dispatchThread.stopDispatching(); |
|
671 |
} |
|
672 |
} else { |
|
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
673 |
if (eventLog.isLoggable(PlatformLogger.FINE)) { |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
674 |
eventLog.fine("Unable to dispatch event: " + event); |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
675 |
} |
2 | 676 |
} |
677 |
} |
|
678 |
||
679 |
/** |
|
680 |
* Returns the timestamp of the most recent event that had a timestamp, and |
|
681 |
* that was dispatched from the <code>EventQueue</code> associated with the |
|
682 |
* calling thread. If an event with a timestamp is currently being |
|
683 |
* dispatched, its timestamp will be returned. If no events have yet |
|
684 |
* been dispatched, the EventQueue's initialization time will be |
|
685 |
* returned instead.In the current version of |
|
686 |
* the JDK, only <code>InputEvent</code>s, |
|
687 |
* <code>ActionEvent</code>s, and <code>InvocationEvent</code>s have |
|
688 |
* timestamps; however, future versions of the JDK may add timestamps to |
|
689 |
* additional event types. Note that this method should only be invoked |
|
690 |
* from an application's {@link #isDispatchThread event dispatching thread}. |
|
691 |
* If this method is |
|
692 |
* invoked from another thread, the current system time (as reported by |
|
693 |
* <code>System.currentTimeMillis()</code>) will be returned instead. |
|
694 |
* |
|
695 |
* @return the timestamp of the last <code>InputEvent</code>, |
|
696 |
* <code>ActionEvent</code>, or <code>InvocationEvent</code> to be |
|
697 |
* dispatched, or <code>System.currentTimeMillis()</code> if this |
|
698 |
* method is invoked on a thread other than an event dispatching |
|
699 |
* thread |
|
700 |
* @see java.awt.event.InputEvent#getWhen |
|
701 |
* @see java.awt.event.ActionEvent#getWhen |
|
702 |
* @see java.awt.event.InvocationEvent#getWhen |
|
703 |
* @see #isDispatchThread |
|
704 |
* |
|
705 |
* @since 1.4 |
|
706 |
*/ |
|
707 |
public static long getMostRecentEventTime() { |
|
708 |
return Toolkit.getEventQueue().getMostRecentEventTimeImpl(); |
|
709 |
} |
|
4365 | 710 |
private long getMostRecentEventTimeImpl() { |
711 |
pushPopLock.lock(); |
|
712 |
try { |
|
713 |
return (Thread.currentThread() == dispatchThread) |
|
714 |
? mostRecentEventTime |
|
715 |
: System.currentTimeMillis(); |
|
716 |
} finally { |
|
717 |
pushPopLock.unlock(); |
|
718 |
} |
|
2 | 719 |
} |
720 |
||
721 |
/** |
|
722 |
* @return most recent event time on all threads. |
|
723 |
*/ |
|
4365 | 724 |
long getMostRecentEventTimeEx() { |
725 |
pushPopLock.lock(); |
|
726 |
try { |
|
727 |
return mostRecentEventTime; |
|
728 |
} finally { |
|
729 |
pushPopLock.unlock(); |
|
730 |
} |
|
2 | 731 |
} |
732 |
||
733 |
/** |
|
734 |
* Returns the the event currently being dispatched by the |
|
735 |
* <code>EventQueue</code> associated with the calling thread. This is |
|
736 |
* useful if a method needs access to the event, but was not designed to |
|
737 |
* receive a reference to it as an argument. Note that this method should |
|
738 |
* only be invoked from an application's event dispatching thread. If this |
|
739 |
* method is invoked from another thread, null will be returned. |
|
740 |
* |
|
741 |
* @return the event currently being dispatched, or null if this method is |
|
742 |
* invoked on a thread other than an event dispatching thread |
|
743 |
* @since 1.4 |
|
744 |
*/ |
|
745 |
public static AWTEvent getCurrentEvent() { |
|
746 |
return Toolkit.getEventQueue().getCurrentEventImpl(); |
|
747 |
} |
|
4365 | 748 |
private AWTEvent getCurrentEventImpl() { |
749 |
pushPopLock.lock(); |
|
750 |
try { |
|
751 |
return (Thread.currentThread() == dispatchThread) |
|
752 |
? ((AWTEvent)currentEvent.get()) |
|
753 |
: null; |
|
754 |
} finally { |
|
755 |
pushPopLock.unlock(); |
|
756 |
} |
|
2 | 757 |
} |
758 |
||
759 |
/** |
|
760 |
* Replaces the existing <code>EventQueue</code> with the specified one. |
|
761 |
* Any pending events are transferred to the new <code>EventQueue</code> |
|
762 |
* for processing by it. |
|
763 |
* |
|
764 |
* @param newEventQueue an <code>EventQueue</code> |
|
765 |
* (or subclass thereof) instance to be use |
|
766 |
* @see java.awt.EventQueue#pop |
|
767 |
* @throws NullPointerException if <code>newEventQueue</code> is <code>null</code> |
|
768 |
* @since 1.2 |
|
769 |
*/ |
|
4365 | 770 |
public void push(EventQueue newEventQueue) { |
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
3084
diff
changeset
|
771 |
if (eventLog.isLoggable(PlatformLogger.FINE)) { |
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
3084
diff
changeset
|
772 |
eventLog.fine("EventQueue.push(" + newEventQueue + ")"); |
2 | 773 |
} |
774 |
||
4365 | 775 |
pushPopLock.lock(); |
776 |
try { |
|
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
777 |
EventQueue topQueue = this; |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
778 |
while (topQueue.nextQueue != null) { |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
779 |
topQueue = topQueue.nextQueue; |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
780 |
} |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
781 |
|
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
782 |
if ((topQueue.dispatchThread != null) && |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
783 |
(topQueue.dispatchThread.getEventQueue() == this)) |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
784 |
{ |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
785 |
newEventQueue.dispatchThread = topQueue.dispatchThread; |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
786 |
topQueue.dispatchThread.setEventQueue(newEventQueue); |
4365 | 787 |
} |
2 | 788 |
|
789 |
// Transfer all events forward to new EventQueue. |
|
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
790 |
while (topQueue.peekEvent() != null) { |
2 | 791 |
try { |
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
792 |
// Use getNextEventPrivate() as it doesn't call flushPendingEvents() |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
793 |
newEventQueue.postEventPrivate(topQueue.getNextEventPrivate()); |
2 | 794 |
} catch (InterruptedException ie) { |
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
3084
diff
changeset
|
795 |
if (eventLog.isLoggable(PlatformLogger.FINE)) { |
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
3084
diff
changeset
|
796 |
eventLog.fine("Interrupted push", ie); |
2 | 797 |
} |
798 |
} |
|
799 |
} |
|
800 |
||
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
801 |
// Wake up EDT waiting in getNextEvent(), so it can |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
802 |
// pick up a new EventQueue. Post the waking event before |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
803 |
// topQueue.nextQueue is assigned, otherwise the event would |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
804 |
// go newEventQueue |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
805 |
topQueue.postEventPrivate(new InvocationEvent(topQueue, dummyRunnable)); |
2 | 806 |
|
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
807 |
newEventQueue.previousQueue = topQueue; |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
808 |
topQueue.nextQueue = newEventQueue; |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
809 |
|
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
810 |
AppContext appContext = AppContext.getAppContext(); |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
811 |
if (appContext.get(AppContext.EVENT_QUEUE_KEY) == topQueue) { |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
812 |
appContext.put(AppContext.EVENT_QUEUE_KEY, newEventQueue); |
4365 | 813 |
} |
2 | 814 |
|
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
815 |
pushPopCond.signalAll(); |
4365 | 816 |
} finally { |
817 |
pushPopLock.unlock(); |
|
2 | 818 |
} |
819 |
} |
|
820 |
||
821 |
/** |
|
822 |
* Stops dispatching events using this <code>EventQueue</code>. |
|
823 |
* Any pending events are transferred to the previous |
|
824 |
* <code>EventQueue</code> for processing. |
|
825 |
* <p> |
|
826 |
* Warning: To avoid deadlock, do not declare this method |
|
827 |
* synchronized in a subclass. |
|
828 |
* |
|
829 |
* @exception EmptyStackException if no previous push was made |
|
830 |
* on this <code>EventQueue</code> |
|
831 |
* @see java.awt.EventQueue#push |
|
832 |
* @since 1.2 |
|
833 |
*/ |
|
834 |
protected void pop() throws EmptyStackException { |
|
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
3084
diff
changeset
|
835 |
if (eventLog.isLoggable(PlatformLogger.FINE)) { |
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
3084
diff
changeset
|
836 |
eventLog.fine("EventQueue.pop(" + this + ")"); |
2 | 837 |
} |
838 |
||
4365 | 839 |
pushPopLock.lock(); |
840 |
try { |
|
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
841 |
EventQueue topQueue = this; |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
842 |
while (topQueue.nextQueue != null) { |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
843 |
topQueue = topQueue.nextQueue; |
2 | 844 |
} |
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
845 |
EventQueue prevQueue = topQueue.previousQueue; |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
846 |
if (prevQueue == null) { |
2 | 847 |
throw new EmptyStackException(); |
848 |
} |
|
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
849 |
|
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
850 |
topQueue.previousQueue = null; |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
851 |
prevQueue.nextQueue = null; |
2 | 852 |
|
853 |
// Transfer all events back to previous EventQueue. |
|
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
854 |
while (topQueue.peekEvent() != null) { |
2 | 855 |
try { |
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
856 |
prevQueue.postEventPrivate(topQueue.getNextEventPrivate()); |
2 | 857 |
} catch (InterruptedException ie) { |
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
3084
diff
changeset
|
858 |
if (eventLog.isLoggable(PlatformLogger.FINE)) { |
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
3084
diff
changeset
|
859 |
eventLog.fine("Interrupted pop", ie); |
2 | 860 |
} |
861 |
} |
|
862 |
} |
|
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
863 |
|
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
864 |
if ((topQueue.dispatchThread != null) && |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
865 |
(topQueue.dispatchThread.getEventQueue() == this)) |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
866 |
{ |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
867 |
prevQueue.dispatchThread = topQueue.dispatchThread; |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
868 |
topQueue.dispatchThread.setEventQueue(prevQueue); |
2 | 869 |
} |
870 |
||
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
871 |
AppContext appContext = AppContext.getAppContext(); |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
872 |
if (appContext.get(AppContext.EVENT_QUEUE_KEY) == this) { |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
873 |
appContext.put(AppContext.EVENT_QUEUE_KEY, prevQueue); |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
874 |
} |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
875 |
|
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
876 |
// Wake up EDT waiting in getNextEvent(), so it can |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
877 |
// pick up a new EventQueue |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
878 |
topQueue.postEventPrivate(new InvocationEvent(topQueue, dummyRunnable)); |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
879 |
|
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
880 |
pushPopCond.signalAll(); |
4365 | 881 |
} finally { |
882 |
pushPopLock.unlock(); |
|
2 | 883 |
} |
884 |
} |
|
885 |
||
886 |
/** |
|
6484
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
887 |
* Creates a new {@code secondary loop} associated with this |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
888 |
* event queue. Use the {@link SecondaryLoop#enter} and |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
889 |
* {@link SecondaryLoop#exit} methods to start and stop the |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
890 |
* event loop and dispatch the events from this queue. |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
891 |
* |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
892 |
* @return secondaryLoop A new secondary loop object, which can |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
893 |
* be used to launch a new nested event |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
894 |
* loop and dispatch events from this queue |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
895 |
* |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
896 |
* @see SecondaryLoop#enter |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
897 |
* @see SecondaryLoop#exit |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
898 |
* |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
899 |
* @since 1.7 |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
900 |
*/ |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
901 |
public SecondaryLoop createSecondaryLoop() { |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
902 |
return createSecondaryLoop(null, null, 0); |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
903 |
} |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
904 |
|
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
905 |
SecondaryLoop createSecondaryLoop(Conditional cond, EventFilter filter, long interval) { |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
906 |
pushPopLock.lock(); |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
907 |
try { |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
908 |
if (nextQueue != null) { |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
909 |
// Forward the request to the top of EventQueue stack |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
910 |
return nextQueue.createSecondaryLoop(cond, filter, interval); |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
911 |
} |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
912 |
if (dispatchThread == null) { |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
913 |
initDispatchThread(); |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
914 |
} |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
915 |
return new WaitDispatchSupport(dispatchThread, cond, filter, interval); |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
916 |
} finally { |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
917 |
pushPopLock.unlock(); |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
918 |
} |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
919 |
} |
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
920 |
|
f5dbd940a640
6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents:
5942
diff
changeset
|
921 |
/** |
2 | 922 |
* Returns true if the calling thread is |
923 |
* {@link Toolkit#getSystemEventQueue the current AWT EventQueue}'s |
|
924 |
* dispatch thread. Use this method to ensure that a particular |
|
925 |
* task is being executed (or not being) there. |
|
926 |
* <p> |
|
927 |
* Note: use the {@link #invokeLater} or {@link #invokeAndWait} |
|
928 |
* methods to execute a task in |
|
929 |
* {@link Toolkit#getSystemEventQueue the current AWT EventQueue}'s |
|
930 |
* dispatch thread. |
|
931 |
* <p> |
|
932 |
* |
|
933 |
* @return true if running in |
|
934 |
* {@link Toolkit#getSystemEventQueue the current AWT EventQueue}'s |
|
935 |
* dispatch thread |
|
936 |
* @see #invokeLater |
|
937 |
* @see #invokeAndWait |
|
938 |
* @see Toolkit#getSystemEventQueue |
|
939 |
* @since 1.2 |
|
940 |
*/ |
|
941 |
public static boolean isDispatchThread() { |
|
942 |
EventQueue eq = Toolkit.getEventQueue(); |
|
4365 | 943 |
return eq.isDispatchThreadImpl(); |
944 |
} |
|
945 |
||
946 |
final boolean isDispatchThreadImpl() { |
|
947 |
EventQueue eq = this; |
|
948 |
pushPopLock.lock(); |
|
949 |
try { |
|
950 |
EventQueue next = eq.nextQueue; |
|
951 |
while (next != null) { |
|
952 |
eq = next; |
|
953 |
next = eq.nextQueue; |
|
954 |
} |
|
955 |
return (Thread.currentThread() == eq.dispatchThread); |
|
956 |
} finally { |
|
957 |
pushPopLock.unlock(); |
|
2 | 958 |
} |
959 |
} |
|
960 |
||
961 |
final void initDispatchThread() { |
|
4365 | 962 |
pushPopLock.lock(); |
963 |
try { |
|
3969
6e77be3973ea
6878284: Sometimes test/javax/swing/system/6799345/TestShutdown.java "hangs"
dcherepanov
parents:
3084
diff
changeset
|
964 |
AppContext appContext = AppContext.getAppContext(); |
6e77be3973ea
6878284: Sometimes test/javax/swing/system/6799345/TestShutdown.java "hangs"
dcherepanov
parents:
3084
diff
changeset
|
965 |
if (dispatchThread == null && !threadGroup.isDestroyed() && !appContext.isDisposed()) { |
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
966 |
dispatchThread = AccessController.doPrivileged( |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
967 |
new PrivilegedAction<EventDispatchThread>() { |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
968 |
public EventDispatchThread run() { |
2 | 969 |
EventDispatchThread t = |
970 |
new EventDispatchThread(threadGroup, |
|
971 |
name, |
|
972 |
EventQueue.this); |
|
973 |
t.setContextClassLoader(classLoader); |
|
974 |
t.setPriority(Thread.NORM_PRIORITY + 1); |
|
975 |
t.setDaemon(false); |
|
976 |
return t; |
|
977 |
} |
|
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
978 |
} |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
979 |
); |
2 | 980 |
AWTAutoShutdown.getInstance().notifyThreadBusy(dispatchThread); |
981 |
dispatchThread.start(); |
|
982 |
} |
|
4365 | 983 |
} finally { |
984 |
pushPopLock.unlock(); |
|
2 | 985 |
} |
986 |
} |
|
987 |
||
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
988 |
final boolean detachDispatchThread(EventDispatchThread edt) { |
4365 | 989 |
/* |
990 |
* This synchronized block is to secure that the event dispatch |
|
991 |
* thread won't die in the middle of posting a new event to the |
|
992 |
* associated event queue. It is important because we notify |
|
993 |
* that the event dispatch thread is busy after posting a new event |
|
994 |
* to its queue, so the EventQueue.dispatchThread reference must |
|
995 |
* be valid at that point. |
|
996 |
*/ |
|
997 |
pushPopLock.lock(); |
|
998 |
try { |
|
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
999 |
if (edt == dispatchThread) { |
4365 | 1000 |
/* |
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
1001 |
* Don't detach the thread if any events are pending. Not |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
1002 |
* sure if it's a possible scenario, though. |
4365 | 1003 |
* |
1004 |
* Fix for 4648733. Check both the associated java event |
|
1005 |
* queue and the PostEventQueue. |
|
1006 |
*/ |
|
1007 |
if ((peekEvent() != null) || !SunToolkit.isPostEventQueueEmpty()) { |
|
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
1008 |
return false; |
4365 | 1009 |
} |
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
1010 |
dispatchThread = null; |
4365 | 1011 |
} |
5942
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
1012 |
AWTAutoShutdown.getInstance().notifyThreadFree(edt); |
287c421fb9b2
6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents:
5506
diff
changeset
|
1013 |
return true; |
4365 | 1014 |
} finally { |
1015 |
pushPopLock.unlock(); |
|
1016 |
} |
|
2 | 1017 |
} |
1018 |
||
1019 |
/* |
|
1020 |
* Gets the <code>EventDispatchThread</code> for this |
|
1021 |
* <code>EventQueue</code>. |
|
1022 |
* @return the event dispatch thread associated with this event queue |
|
1023 |
* or <code>null</code> if this event queue doesn't have a |
|
1024 |
* working thread associated with it |
|
1025 |
* @see java.awt.EventQueue#initDispatchThread |
|
1026 |
* @see java.awt.EventQueue#detachDispatchThread |
|
1027 |
*/ |
|
1028 |
final EventDispatchThread getDispatchThread() { |
|
4365 | 1029 |
pushPopLock.lock(); |
1030 |
try { |
|
1031 |
return dispatchThread; |
|
1032 |
} finally { |
|
1033 |
pushPopLock.unlock(); |
|
1034 |
} |
|
2 | 1035 |
} |
1036 |
||
1037 |
/* |
|
1038 |
* Removes any pending events for the specified source object. |
|
1039 |
* If removeAllEvents parameter is <code>true</code> then all |
|
1040 |
* events for the specified source object are removed, if it |
|
1041 |
* is <code>false</code> then <code>SequencedEvent</code>, <code>SentEvent</code>, |
|
1042 |
* <code>FocusEvent</code>, <code>WindowEvent</code>, <code>KeyEvent</code>, |
|
1043 |
* and <code>InputMethodEvent</code> are kept in the queue, but all other |
|
1044 |
* events are removed. |
|
1045 |
* |
|
1046 |
* This method is normally called by the source's |
|
1047 |
* <code>removeNotify</code> method. |
|
1048 |
*/ |
|
1049 |
final void removeSourceEvents(Object source, boolean removeAllEvents) { |
|
1050 |
SunToolkit.flushPendingEvents(); |
|
4365 | 1051 |
pushPopLock.lock(); |
1052 |
try { |
|
2 | 1053 |
for (int i = 0; i < NUM_PRIORITIES; i++) { |
1054 |
EventQueueItem entry = queues[i].head; |
|
1055 |
EventQueueItem prev = null; |
|
1056 |
while (entry != null) { |
|
1057 |
if ((entry.event.getSource() == source) |
|
1058 |
&& (removeAllEvents |
|
1059 |
|| ! (entry.event instanceof SequencedEvent |
|
1060 |
|| entry.event instanceof SentEvent |
|
1061 |
|| entry.event instanceof FocusEvent |
|
1062 |
|| entry.event instanceof WindowEvent |
|
1063 |
|| entry.event instanceof KeyEvent |
|
1064 |
|| entry.event instanceof InputMethodEvent))) |
|
1065 |
{ |
|
1066 |
if (entry.event instanceof SequencedEvent) { |
|
1067 |
((SequencedEvent)entry.event).dispose(); |
|
1068 |
} |
|
1069 |
if (entry.event instanceof SentEvent) { |
|
1070 |
((SentEvent)entry.event).dispose(); |
|
1071 |
} |
|
1072 |
if (prev == null) { |
|
1073 |
queues[i].head = entry.next; |
|
1074 |
} else { |
|
1075 |
prev.next = entry.next; |
|
1076 |
} |
|
1077 |
uncacheEQItem(entry); |
|
1078 |
} else { |
|
1079 |
prev = entry; |
|
1080 |
} |
|
1081 |
entry = entry.next; |
|
1082 |
} |
|
1083 |
queues[i].tail = prev; |
|
1084 |
} |
|
4365 | 1085 |
} finally { |
1086 |
pushPopLock.unlock(); |
|
2 | 1087 |
} |
1088 |
} |
|
1089 |
||
1090 |
static void setCurrentEventAndMostRecentTime(AWTEvent e) { |
|
1091 |
Toolkit.getEventQueue().setCurrentEventAndMostRecentTimeImpl(e); |
|
1092 |
} |
|
4365 | 1093 |
private void setCurrentEventAndMostRecentTimeImpl(AWTEvent e) { |
1094 |
pushPopLock.lock(); |
|
1095 |
try { |
|
1096 |
if (Thread.currentThread() != dispatchThread) { |
|
1097 |
return; |
|
1098 |
} |
|
2 | 1099 |
|
4365 | 1100 |
currentEvent = new WeakReference(e); |
2 | 1101 |
|
4365 | 1102 |
// This series of 'instanceof' checks should be replaced with a |
1103 |
// polymorphic type (for example, an interface which declares a |
|
1104 |
// getWhen() method). However, this would require us to make such |
|
1105 |
// a type public, or to place it in sun.awt. Both of these approaches |
|
1106 |
// have been frowned upon. So for now, we hack. |
|
1107 |
// |
|
1108 |
// In tiger, we will probably give timestamps to all events, so this |
|
1109 |
// will no longer be an issue. |
|
1110 |
long mostRecentEventTime2 = Long.MIN_VALUE; |
|
1111 |
if (e instanceof InputEvent) { |
|
1112 |
InputEvent ie = (InputEvent)e; |
|
1113 |
mostRecentEventTime2 = ie.getWhen(); |
|
1114 |
} else if (e instanceof InputMethodEvent) { |
|
1115 |
InputMethodEvent ime = (InputMethodEvent)e; |
|
1116 |
mostRecentEventTime2 = ime.getWhen(); |
|
1117 |
} else if (e instanceof ActionEvent) { |
|
1118 |
ActionEvent ae = (ActionEvent)e; |
|
1119 |
mostRecentEventTime2 = ae.getWhen(); |
|
1120 |
} else if (e instanceof InvocationEvent) { |
|
1121 |
InvocationEvent ie = (InvocationEvent)e; |
|
1122 |
mostRecentEventTime2 = ie.getWhen(); |
|
1123 |
} |
|
1124 |
mostRecentEventTime = Math.max(mostRecentEventTime, mostRecentEventTime2); |
|
1125 |
} finally { |
|
1126 |
pushPopLock.unlock(); |
|
2 | 1127 |
} |
1128 |
} |
|
1129 |
||
1130 |
/** |
|
1131 |
* Causes <code>runnable</code> to have its <code>run</code> |
|
1132 |
* method called in the {@link #isDispatchThread dispatch thread} of |
|
1133 |
* {@link Toolkit#getSystemEventQueue the system EventQueue}. |
|
1134 |
* This will happen after all pending events are processed. |
|
1135 |
* |
|
1136 |
* @param runnable the <code>Runnable</code> whose <code>run</code> |
|
1137 |
* method should be executed |
|
1138 |
* asynchronously in the |
|
1139 |
* {@link #isDispatchThread event dispatch thread} |
|
1140 |
* of {@link Toolkit#getSystemEventQueue the system EventQueue} |
|
1141 |
* @see #invokeAndWait |
|
1142 |
* @see Toolkit#getSystemEventQueue |
|
1143 |
* @see #isDispatchThread |
|
1144 |
* @since 1.2 |
|
1145 |
*/ |
|
1146 |
public static void invokeLater(Runnable runnable) { |
|
1147 |
Toolkit.getEventQueue().postEvent( |
|
1148 |
new InvocationEvent(Toolkit.getDefaultToolkit(), runnable)); |
|
1149 |
} |
|
1150 |
||
1151 |
/** |
|
1152 |
* Causes <code>runnable</code> to have its <code>run</code> |
|
1153 |
* method called in the {@link #isDispatchThread dispatch thread} of |
|
1154 |
* {@link Toolkit#getSystemEventQueue the system EventQueue}. |
|
1155 |
* This will happen after all pending events are processed. |
|
1156 |
* The call blocks until this has happened. This method |
|
1157 |
* will throw an Error if called from the |
|
1158 |
* {@link #isDispatchThread event dispatcher thread}. |
|
1159 |
* |
|
1160 |
* @param runnable the <code>Runnable</code> whose <code>run</code> |
|
1161 |
* method should be executed |
|
1162 |
* synchronously in the |
|
1163 |
* {@link #isDispatchThread event dispatch thread} |
|
1164 |
* of {@link Toolkit#getSystemEventQueue the system EventQueue} |
|
1165 |
* @exception InterruptedException if any thread has |
|
1166 |
* interrupted this thread |
|
1167 |
* @exception InvocationTargetException if an throwable is thrown |
|
1168 |
* when running <code>runnable</code> |
|
1169 |
* @see #invokeLater |
|
1170 |
* @see Toolkit#getSystemEventQueue |
|
1171 |
* @see #isDispatchThread |
|
1172 |
* @since 1.2 |
|
1173 |
*/ |
|
1174 |
public static void invokeAndWait(Runnable runnable) |
|
1175 |
throws InterruptedException, InvocationTargetException { |
|
1176 |
||
1177 |
if (EventQueue.isDispatchThread()) { |
|
1178 |
throw new Error("Cannot call invokeAndWait from the event dispatcher thread"); |
|
1179 |
} |
|
1180 |
||
1181 |
class AWTInvocationLock {} |
|
1182 |
Object lock = new AWTInvocationLock(); |
|
1183 |
||
1184 |
InvocationEvent event = |
|
1185 |
new InvocationEvent(Toolkit.getDefaultToolkit(), runnable, lock, |
|
1186 |
true); |
|
1187 |
||
1188 |
synchronized (lock) { |
|
1189 |
Toolkit.getEventQueue().postEvent(event); |
|
4264
40c232605c68
6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents:
3972
diff
changeset
|
1190 |
while (!event.isDispatched()) { |
40c232605c68
6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents:
3972
diff
changeset
|
1191 |
lock.wait(); |
40c232605c68
6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents:
3972
diff
changeset
|
1192 |
} |
2 | 1193 |
} |
1194 |
||
1195 |
Throwable eventThrowable = event.getThrowable(); |
|
1196 |
if (eventThrowable != null) { |
|
1197 |
throw new InvocationTargetException(eventThrowable); |
|
1198 |
} |
|
1199 |
} |
|
1200 |
||
1201 |
/* |
|
1202 |
* Called from PostEventQueue.postEvent to notify that a new event |
|
1203 |
* appeared. First it proceeds to the EventQueue on the top of the |
|
1204 |
* stack, then notifies the associated dispatch thread if it exists |
|
1205 |
* or starts a new one otherwise. |
|
1206 |
*/ |
|
1207 |
private void wakeup(boolean isShutdown) { |
|
4365 | 1208 |
pushPopLock.lock(); |
1209 |
try { |
|
2 | 1210 |
if (nextQueue != null) { |
1211 |
// Forward call to the top of EventQueue stack. |
|
1212 |
nextQueue.wakeup(isShutdown); |
|
1213 |
} else if (dispatchThread != null) { |
|
4365 | 1214 |
pushPopCond.signalAll(); |
2 | 1215 |
} else if (!isShutdown) { |
1216 |
initDispatchThread(); |
|
1217 |
} |
|
4365 | 1218 |
} finally { |
1219 |
pushPopLock.unlock(); |
|
2 | 1220 |
} |
1221 |
} |
|
1222 |
} |
|
1223 |
||
1224 |
/** |
|
1225 |
* The Queue object holds pointers to the beginning and end of one internal |
|
1226 |
* queue. An EventQueue object is composed of multiple internal Queues, one |
|
1227 |
* for each priority supported by the EventQueue. All Events on a particular |
|
1228 |
* internal Queue have identical priority. |
|
1229 |
*/ |
|
1230 |
class Queue { |
|
1231 |
EventQueueItem head; |
|
1232 |
EventQueueItem tail; |
|
1233 |
} |