author | mchung |
Tue, 08 Dec 2009 09:02:09 -0800 | |
changeset 4374 | f672d9cf521e |
parent 4365 | 4ac67034e98b |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
1247 | 2 |
* Copyright 1996-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package java.awt; |
|
27 |
||
28 |
import java.awt.event.*; |
|
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 |
||
2 | 141 |
private EventDispatchThread dispatchThread; |
142 |
||
143 |
private final ThreadGroup threadGroup = |
|
144 |
Thread.currentThread().getThreadGroup(); |
|
145 |
private final ClassLoader classLoader = |
|
146 |
Thread.currentThread().getContextClassLoader(); |
|
147 |
||
148 |
/* |
|
149 |
* The time stamp of the last dispatched InputEvent or ActionEvent. |
|
150 |
*/ |
|
151 |
private long mostRecentEventTime = System.currentTimeMillis(); |
|
152 |
||
153 |
/** |
|
154 |
* The modifiers field of the current event, if the current event is an |
|
155 |
* InputEvent or ActionEvent. |
|
156 |
*/ |
|
157 |
private WeakReference currentEvent; |
|
158 |
||
159 |
/* |
|
160 |
* Non-zero if a thread is waiting in getNextEvent(int) for an event of |
|
161 |
* a particular ID to be posted to the queue. |
|
162 |
*/ |
|
163 |
private int waitForID; |
|
164 |
||
165 |
private final String name = "AWT-EventQueue-" + nextThreadNum(); |
|
166 |
||
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
3084
diff
changeset
|
167 |
private static final PlatformLogger eventLog = PlatformLogger.getLogger("java.awt.event.EventQueue"); |
2 | 168 |
|
3084
67ca55732362
6824169: Need to remove some AWT class dependencies
dcherepanov
parents:
1247
diff
changeset
|
169 |
static { |
67ca55732362
6824169: Need to remove some AWT class dependencies
dcherepanov
parents:
1247
diff
changeset
|
170 |
AWTAccessor.setEventQueueAccessor( |
67ca55732362
6824169: Need to remove some AWT class dependencies
dcherepanov
parents:
1247
diff
changeset
|
171 |
new AWTAccessor.EventQueueAccessor() { |
4365 | 172 |
public Thread getDispatchThread(EventQueue eventQueue) { |
173 |
return eventQueue.getDispatchThread(); |
|
3084
67ca55732362
6824169: Need to remove some AWT class dependencies
dcherepanov
parents:
1247
diff
changeset
|
174 |
} |
4365 | 175 |
public boolean isDispatchThreadImpl(EventQueue eventQueue) { |
176 |
return eventQueue.isDispatchThreadImpl(); |
|
3084
67ca55732362
6824169: Need to remove some AWT class dependencies
dcherepanov
parents:
1247
diff
changeset
|
177 |
} |
67ca55732362
6824169: Need to remove some AWT class dependencies
dcherepanov
parents:
1247
diff
changeset
|
178 |
}); |
67ca55732362
6824169: Need to remove some AWT class dependencies
dcherepanov
parents:
1247
diff
changeset
|
179 |
} |
67ca55732362
6824169: Need to remove some AWT class dependencies
dcherepanov
parents:
1247
diff
changeset
|
180 |
|
2 | 181 |
public EventQueue() { |
182 |
for (int i = 0; i < NUM_PRIORITIES; i++) { |
|
183 |
queues[i] = new Queue(); |
|
184 |
} |
|
185 |
/* |
|
186 |
* NOTE: if you ever have to start the associated event dispatch |
|
187 |
* thread at this point, be aware of the following problem: |
|
188 |
* If this EventQueue instance is created in |
|
189 |
* SunToolkit.createNewAppContext() the started dispatch thread |
|
190 |
* may call AppContext.getAppContext() before createNewAppContext() |
|
191 |
* completes thus causing mess in thread group to appcontext mapping. |
|
192 |
*/ |
|
4365 | 193 |
|
194 |
pushPopLock = (Lock)AppContext.getAppContext().get(AppContext.EVENT_QUEUE_LOCK_KEY); |
|
195 |
pushPopCond = (Condition)AppContext.getAppContext().get(AppContext.EVENT_QUEUE_COND_KEY); |
|
2 | 196 |
} |
197 |
||
198 |
/** |
|
199 |
* Posts a 1.1-style event to the <code>EventQueue</code>. |
|
200 |
* If there is an existing event on the queue with the same ID |
|
201 |
* and event source, the source <code>Component</code>'s |
|
202 |
* <code>coalesceEvents</code> method will be called. |
|
203 |
* |
|
204 |
* @param theEvent an instance of <code>java.awt.AWTEvent</code>, |
|
205 |
* or a subclass of it |
|
206 |
* @throws NullPointerException if <code>theEvent</code> is <code>null</code> |
|
207 |
*/ |
|
208 |
public void postEvent(AWTEvent theEvent) { |
|
209 |
SunToolkit.flushPendingEvents(); |
|
210 |
postEventPrivate(theEvent); |
|
211 |
} |
|
212 |
||
213 |
/** |
|
214 |
* Posts a 1.1-style event to the <code>EventQueue</code>. |
|
215 |
* If there is an existing event on the queue with the same ID |
|
216 |
* and event source, the source <code>Component</code>'s |
|
217 |
* <code>coalesceEvents</code> method will be called. |
|
218 |
* |
|
219 |
* @param theEvent an instance of <code>java.awt.AWTEvent</code>, |
|
220 |
* or a subclass of it |
|
221 |
*/ |
|
222 |
final void postEventPrivate(AWTEvent theEvent) { |
|
223 |
theEvent.isPosted = true; |
|
4365 | 224 |
pushPopLock.lock(); |
225 |
try { |
|
2 | 226 |
if (dispatchThread == null && nextQueue == null) { |
227 |
if (theEvent.getSource() == AWTAutoShutdown.getInstance()) { |
|
228 |
return; |
|
229 |
} else { |
|
230 |
initDispatchThread(); |
|
231 |
} |
|
232 |
} |
|
233 |
if (nextQueue != null) { |
|
234 |
// Forward event to top of EventQueue stack. |
|
235 |
nextQueue.postEventPrivate(theEvent); |
|
236 |
return; |
|
237 |
} |
|
238 |
postEvent(theEvent, getPriority(theEvent)); |
|
4365 | 239 |
} finally { |
240 |
pushPopLock.unlock(); |
|
2 | 241 |
} |
242 |
} |
|
243 |
||
244 |
private static int getPriority(AWTEvent theEvent) { |
|
245 |
if (theEvent instanceof PeerEvent && |
|
246 |
(((PeerEvent)theEvent).getFlags() & |
|
247 |
PeerEvent.ULTIMATE_PRIORITY_EVENT) != 0) |
|
248 |
{ |
|
249 |
return ULTIMATE_PRIORITY; |
|
250 |
} |
|
251 |
||
252 |
if (theEvent instanceof PeerEvent && |
|
253 |
(((PeerEvent)theEvent).getFlags() & |
|
254 |
PeerEvent.PRIORITY_EVENT) != 0) |
|
255 |
{ |
|
256 |
return HIGH_PRIORITY; |
|
257 |
} |
|
258 |
||
259 |
if (theEvent instanceof PeerEvent && |
|
260 |
(((PeerEvent)theEvent).getFlags() & |
|
261 |
PeerEvent.LOW_PRIORITY_EVENT) != 0) |
|
262 |
{ |
|
263 |
return LOW_PRIORITY; |
|
264 |
} |
|
265 |
||
266 |
int id = theEvent.getID(); |
|
267 |
if (id == PaintEvent.PAINT || id == PaintEvent.UPDATE) { |
|
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 { |
|
2 | 504 |
for (int i = NUM_PRIORITIES - 1; i >= 0; i--) { |
505 |
if (queues[i].head != null) { |
|
506 |
EventQueueItem entry = queues[i].head; |
|
507 |
queues[i].head = entry.next; |
|
508 |
if (entry.next == null) { |
|
509 |
queues[i].tail = null; |
|
510 |
} |
|
511 |
uncacheEQItem(entry); |
|
512 |
return entry.event; |
|
513 |
} |
|
514 |
} |
|
515 |
AWTAutoShutdown.getInstance().notifyThreadFree(dispatchThread); |
|
4365 | 516 |
pushPopCond.await(); |
517 |
} finally { |
|
518 |
pushPopLock.unlock(); |
|
2 | 519 |
} |
520 |
} while(true); |
|
521 |
} |
|
522 |
||
523 |
AWTEvent getNextEvent(int id) throws InterruptedException { |
|
524 |
do { |
|
525 |
/* |
|
526 |
* SunToolkit.flushPendingEvents must be called outside |
|
527 |
* of the synchronized block to avoid deadlock when |
|
528 |
* event queues are nested with push()/pop(). |
|
529 |
*/ |
|
530 |
SunToolkit.flushPendingEvents(); |
|
4365 | 531 |
pushPopLock.lock(); |
532 |
try { |
|
2 | 533 |
for (int i = 0; i < NUM_PRIORITIES; i++) { |
534 |
for (EventQueueItem entry = queues[i].head, prev = null; |
|
535 |
entry != null; prev = entry, entry = entry.next) |
|
536 |
{ |
|
1181
c5971dbeaaaa
6693974: Unify EventQueue$EventQueueItem and SunToolkit.$EventQueueItem classes
dav
parents:
2
diff
changeset
|
537 |
if (entry.event.getID() == id) { |
2 | 538 |
if (prev == null) { |
539 |
queues[i].head = entry.next; |
|
540 |
} else { |
|
541 |
prev.next = entry.next; |
|
542 |
} |
|
543 |
if (queues[i].tail == entry) { |
|
544 |
queues[i].tail = prev; |
|
545 |
} |
|
546 |
uncacheEQItem(entry); |
|
547 |
return entry.event; |
|
548 |
} |
|
549 |
} |
|
550 |
} |
|
4365 | 551 |
waitForID = id; |
552 |
pushPopCond.await(); |
|
553 |
waitForID = 0; |
|
554 |
} finally { |
|
555 |
pushPopLock.unlock(); |
|
2 | 556 |
} |
557 |
} while(true); |
|
558 |
} |
|
559 |
||
560 |
/** |
|
561 |
* Returns the first event on the <code>EventQueue</code> |
|
562 |
* without removing it. |
|
563 |
* @return the first event |
|
564 |
*/ |
|
4365 | 565 |
public AWTEvent peekEvent() { |
566 |
pushPopLock.lock(); |
|
567 |
try { |
|
568 |
for (int i = NUM_PRIORITIES - 1; i >= 0; i--) { |
|
569 |
if (queues[i].head != null) { |
|
570 |
return queues[i].head.event; |
|
571 |
} |
|
2 | 572 |
} |
4365 | 573 |
} finally { |
574 |
pushPopLock.unlock(); |
|
2 | 575 |
} |
576 |
||
577 |
return null; |
|
578 |
} |
|
579 |
||
580 |
/** |
|
581 |
* Returns the first event with the specified id, if any. |
|
582 |
* @param id the id of the type of event desired |
|
583 |
* @return the first event of the specified id or <code>null</code> |
|
584 |
* if there is no such event |
|
585 |
*/ |
|
4365 | 586 |
public AWTEvent peekEvent(int id) { |
587 |
pushPopLock.lock(); |
|
588 |
try { |
|
589 |
for (int i = NUM_PRIORITIES - 1; i >= 0; i--) { |
|
590 |
EventQueueItem q = queues[i].head; |
|
591 |
for (; q != null; q = q.next) { |
|
592 |
if (q.event.getID() == id) { |
|
593 |
return q.event; |
|
594 |
} |
|
2 | 595 |
} |
596 |
} |
|
4365 | 597 |
} finally { |
598 |
pushPopLock.unlock(); |
|
2 | 599 |
} |
600 |
||
601 |
return null; |
|
602 |
} |
|
603 |
||
604 |
/** |
|
605 |
* Dispatches an event. The manner in which the event is |
|
606 |
* dispatched depends upon the type of the event and the |
|
607 |
* type of the event's source object: |
|
608 |
* <p> </p> |
|
609 |
* <table border=1 summary="Event types, source types, and dispatch methods"> |
|
610 |
* <tr> |
|
611 |
* <th>Event Type</th> |
|
612 |
* <th>Source Type</th> |
|
613 |
* <th>Dispatched To</th> |
|
614 |
* </tr> |
|
615 |
* <tr> |
|
616 |
* <td>ActiveEvent</td> |
|
617 |
* <td>Any</td> |
|
618 |
* <td>event.dispatch()</td> |
|
619 |
* </tr> |
|
620 |
* <tr> |
|
621 |
* <td>Other</td> |
|
622 |
* <td>Component</td> |
|
623 |
* <td>source.dispatchEvent(AWTEvent)</td> |
|
624 |
* </tr> |
|
625 |
* <tr> |
|
626 |
* <td>Other</td> |
|
627 |
* <td>MenuComponent</td> |
|
628 |
* <td>source.dispatchEvent(AWTEvent)</td> |
|
629 |
* </tr> |
|
630 |
* <tr> |
|
631 |
* <td>Other</td> |
|
632 |
* <td>Other</td> |
|
633 |
* <td>No action (ignored)</td> |
|
634 |
* </tr> |
|
635 |
* </table> |
|
636 |
* <p> </p> |
|
637 |
* @param event an instance of <code>java.awt.AWTEvent</code>, |
|
638 |
* or a subclass of it |
|
639 |
* @throws NullPointerException if <code>event</code> is <code>null</code> |
|
640 |
* @since 1.2 |
|
641 |
*/ |
|
642 |
protected void dispatchEvent(AWTEvent event) { |
|
643 |
event.isPosted = true; |
|
644 |
Object src = event.getSource(); |
|
645 |
if (event instanceof ActiveEvent) { |
|
646 |
// This could become the sole method of dispatching in time. |
|
647 |
setCurrentEventAndMostRecentTimeImpl(event); |
|
648 |
||
649 |
((ActiveEvent)event).dispatch(); |
|
650 |
} else if (src instanceof Component) { |
|
651 |
((Component)src).dispatchEvent(event); |
|
652 |
event.dispatched(); |
|
653 |
} else if (src instanceof MenuComponent) { |
|
654 |
((MenuComponent)src).dispatchEvent(event); |
|
655 |
} else if (src instanceof TrayIcon) { |
|
656 |
((TrayIcon)src).dispatchEvent(event); |
|
657 |
} else if (src instanceof AWTAutoShutdown) { |
|
658 |
if (noEvents()) { |
|
659 |
dispatchThread.stopDispatching(); |
|
660 |
} |
|
661 |
} else { |
|
662 |
System.err.println("unable to dispatch event: " + event); |
|
663 |
} |
|
664 |
} |
|
665 |
||
666 |
/** |
|
667 |
* Returns the timestamp of the most recent event that had a timestamp, and |
|
668 |
* that was dispatched from the <code>EventQueue</code> associated with the |
|
669 |
* calling thread. If an event with a timestamp is currently being |
|
670 |
* dispatched, its timestamp will be returned. If no events have yet |
|
671 |
* been dispatched, the EventQueue's initialization time will be |
|
672 |
* returned instead.In the current version of |
|
673 |
* the JDK, only <code>InputEvent</code>s, |
|
674 |
* <code>ActionEvent</code>s, and <code>InvocationEvent</code>s have |
|
675 |
* timestamps; however, future versions of the JDK may add timestamps to |
|
676 |
* additional event types. Note that this method should only be invoked |
|
677 |
* from an application's {@link #isDispatchThread event dispatching thread}. |
|
678 |
* If this method is |
|
679 |
* invoked from another thread, the current system time (as reported by |
|
680 |
* <code>System.currentTimeMillis()</code>) will be returned instead. |
|
681 |
* |
|
682 |
* @return the timestamp of the last <code>InputEvent</code>, |
|
683 |
* <code>ActionEvent</code>, or <code>InvocationEvent</code> to be |
|
684 |
* dispatched, or <code>System.currentTimeMillis()</code> if this |
|
685 |
* method is invoked on a thread other than an event dispatching |
|
686 |
* thread |
|
687 |
* @see java.awt.event.InputEvent#getWhen |
|
688 |
* @see java.awt.event.ActionEvent#getWhen |
|
689 |
* @see java.awt.event.InvocationEvent#getWhen |
|
690 |
* @see #isDispatchThread |
|
691 |
* |
|
692 |
* @since 1.4 |
|
693 |
*/ |
|
694 |
public static long getMostRecentEventTime() { |
|
695 |
return Toolkit.getEventQueue().getMostRecentEventTimeImpl(); |
|
696 |
} |
|
4365 | 697 |
private long getMostRecentEventTimeImpl() { |
698 |
pushPopLock.lock(); |
|
699 |
try { |
|
700 |
return (Thread.currentThread() == dispatchThread) |
|
701 |
? mostRecentEventTime |
|
702 |
: System.currentTimeMillis(); |
|
703 |
} finally { |
|
704 |
pushPopLock.unlock(); |
|
705 |
} |
|
2 | 706 |
} |
707 |
||
708 |
/** |
|
709 |
* @return most recent event time on all threads. |
|
710 |
*/ |
|
4365 | 711 |
long getMostRecentEventTimeEx() { |
712 |
pushPopLock.lock(); |
|
713 |
try { |
|
714 |
return mostRecentEventTime; |
|
715 |
} finally { |
|
716 |
pushPopLock.unlock(); |
|
717 |
} |
|
2 | 718 |
} |
719 |
||
720 |
/** |
|
721 |
* Returns the the event currently being dispatched by the |
|
722 |
* <code>EventQueue</code> associated with the calling thread. This is |
|
723 |
* useful if a method needs access to the event, but was not designed to |
|
724 |
* receive a reference to it as an argument. Note that this method should |
|
725 |
* only be invoked from an application's event dispatching thread. If this |
|
726 |
* method is invoked from another thread, null will be returned. |
|
727 |
* |
|
728 |
* @return the event currently being dispatched, or null if this method is |
|
729 |
* invoked on a thread other than an event dispatching thread |
|
730 |
* @since 1.4 |
|
731 |
*/ |
|
732 |
public static AWTEvent getCurrentEvent() { |
|
733 |
return Toolkit.getEventQueue().getCurrentEventImpl(); |
|
734 |
} |
|
4365 | 735 |
private AWTEvent getCurrentEventImpl() { |
736 |
pushPopLock.lock(); |
|
737 |
try { |
|
738 |
return (Thread.currentThread() == dispatchThread) |
|
739 |
? ((AWTEvent)currentEvent.get()) |
|
740 |
: null; |
|
741 |
} finally { |
|
742 |
pushPopLock.unlock(); |
|
743 |
} |
|
2 | 744 |
} |
745 |
||
746 |
/** |
|
747 |
* Replaces the existing <code>EventQueue</code> with the specified one. |
|
748 |
* Any pending events are transferred to the new <code>EventQueue</code> |
|
749 |
* for processing by it. |
|
750 |
* |
|
751 |
* @param newEventQueue an <code>EventQueue</code> |
|
752 |
* (or subclass thereof) instance to be use |
|
753 |
* @see java.awt.EventQueue#pop |
|
754 |
* @throws NullPointerException if <code>newEventQueue</code> is <code>null</code> |
|
755 |
* @since 1.2 |
|
756 |
*/ |
|
4365 | 757 |
public void push(EventQueue newEventQueue) { |
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
3084
diff
changeset
|
758 |
if (eventLog.isLoggable(PlatformLogger.FINE)) { |
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
3084
diff
changeset
|
759 |
eventLog.fine("EventQueue.push(" + newEventQueue + ")"); |
2 | 760 |
} |
761 |
||
4365 | 762 |
pushPopLock.lock(); |
763 |
try { |
|
764 |
EventQueue toPush = this; |
|
765 |
while (toPush.nextQueue != null) { |
|
766 |
toPush = toPush.nextQueue; |
|
767 |
} |
|
2 | 768 |
|
769 |
// Transfer all events forward to new EventQueue. |
|
4365 | 770 |
while (toPush.peekEvent() != null) { |
2 | 771 |
try { |
4365 | 772 |
newEventQueue.postEventPrivate(toPush.getNextEvent()); |
2 | 773 |
} catch (InterruptedException ie) { |
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
3084
diff
changeset
|
774 |
if (eventLog.isLoggable(PlatformLogger.FINE)) { |
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
3084
diff
changeset
|
775 |
eventLog.fine("Interrupted push", ie); |
2 | 776 |
} |
777 |
} |
|
778 |
} |
|
779 |
||
4365 | 780 |
newEventQueue.previousQueue = toPush; |
2 | 781 |
|
4365 | 782 |
/* |
783 |
* Stop the event dispatch thread associated with the currently |
|
784 |
* active event queue, so that after the new queue is pushed |
|
785 |
* on the top this event dispatch thread won't prevent AWT from |
|
786 |
* being automatically shut down. |
|
787 |
* Use stopDispatchingLater() to avoid deadlock: stopDispatching() |
|
788 |
* waits for the dispatch thread to exit, which in turn waits |
|
789 |
* for the lock in EQ.detachDispatchThread(), which is hold by |
|
790 |
* this method. |
|
791 |
*/ |
|
792 |
if (toPush.dispatchThread != null) { |
|
793 |
toPush.dispatchThread.stopDispatchingLater(); |
|
794 |
} |
|
2 | 795 |
|
4365 | 796 |
toPush.nextQueue = newEventQueue; |
797 |
||
798 |
AppContext appContext = AppContext.getAppContext(); |
|
799 |
if (appContext.get(AppContext.EVENT_QUEUE_KEY) == toPush) { |
|
800 |
appContext.put(AppContext.EVENT_QUEUE_KEY, newEventQueue); |
|
801 |
} |
|
802 |
} finally { |
|
803 |
pushPopLock.unlock(); |
|
2 | 804 |
} |
805 |
} |
|
806 |
||
807 |
/** |
|
808 |
* Stops dispatching events using this <code>EventQueue</code>. |
|
809 |
* Any pending events are transferred to the previous |
|
810 |
* <code>EventQueue</code> for processing. |
|
811 |
* <p> |
|
812 |
* Warning: To avoid deadlock, do not declare this method |
|
813 |
* synchronized in a subclass. |
|
814 |
* |
|
815 |
* @exception EmptyStackException if no previous push was made |
|
816 |
* on this <code>EventQueue</code> |
|
817 |
* @see java.awt.EventQueue#push |
|
818 |
* @since 1.2 |
|
819 |
*/ |
|
820 |
protected void pop() throws EmptyStackException { |
|
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
3084
diff
changeset
|
821 |
if (eventLog.isLoggable(PlatformLogger.FINE)) { |
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
3084
diff
changeset
|
822 |
eventLog.fine("EventQueue.pop(" + this + ")"); |
2 | 823 |
} |
824 |
||
4365 | 825 |
EventDispatchThread dt = null; |
826 |
pushPopLock.lock(); |
|
827 |
try { |
|
828 |
EventQueue toPop = this; |
|
829 |
while (toPop.nextQueue != null) { |
|
830 |
toPop = toPop.nextQueue; |
|
2 | 831 |
} |
4365 | 832 |
EventQueue prev = toPop.previousQueue; |
833 |
if (prev == null) { |
|
2 | 834 |
throw new EmptyStackException(); |
835 |
} |
|
4365 | 836 |
toPop.previousQueue = null; |
2 | 837 |
|
838 |
// Transfer all events back to previous EventQueue. |
|
4365 | 839 |
prev.nextQueue = null; |
840 |
while (toPop.peekEvent() != null) { |
|
2 | 841 |
try { |
4365 | 842 |
prev.postEventPrivate(toPop.getNextEvent()); |
2 | 843 |
} catch (InterruptedException ie) { |
3938
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
3084
diff
changeset
|
844 |
if (eventLog.isLoggable(PlatformLogger.FINE)) { |
ef327bd847c0
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents:
3084
diff
changeset
|
845 |
eventLog.fine("Interrupted pop", ie); |
2 | 846 |
} |
847 |
} |
|
848 |
} |
|
849 |
AppContext appContext = AppContext.getAppContext(); |
|
850 |
if (appContext.get(AppContext.EVENT_QUEUE_KEY) == this) { |
|
4365 | 851 |
appContext.put(AppContext.EVENT_QUEUE_KEY, prev); |
2 | 852 |
} |
853 |
||
4365 | 854 |
dt = toPop.dispatchThread; |
855 |
} finally { |
|
856 |
pushPopLock.unlock(); |
|
2 | 857 |
} |
858 |
||
859 |
if (dt != null) { |
|
860 |
dt.stopDispatching(); // Must be done outside synchronized |
|
861 |
// block to avoid possible deadlock |
|
862 |
} |
|
863 |
} |
|
864 |
||
865 |
/** |
|
866 |
* Returns true if the calling thread is |
|
867 |
* {@link Toolkit#getSystemEventQueue the current AWT EventQueue}'s |
|
868 |
* dispatch thread. Use this method to ensure that a particular |
|
869 |
* task is being executed (or not being) there. |
|
870 |
* <p> |
|
871 |
* Note: use the {@link #invokeLater} or {@link #invokeAndWait} |
|
872 |
* methods to execute a task in |
|
873 |
* {@link Toolkit#getSystemEventQueue the current AWT EventQueue}'s |
|
874 |
* dispatch thread. |
|
875 |
* <p> |
|
876 |
* |
|
877 |
* @return true if running in |
|
878 |
* {@link Toolkit#getSystemEventQueue the current AWT EventQueue}'s |
|
879 |
* dispatch thread |
|
880 |
* @see #invokeLater |
|
881 |
* @see #invokeAndWait |
|
882 |
* @see Toolkit#getSystemEventQueue |
|
883 |
* @since 1.2 |
|
884 |
*/ |
|
885 |
public static boolean isDispatchThread() { |
|
886 |
EventQueue eq = Toolkit.getEventQueue(); |
|
4365 | 887 |
return eq.isDispatchThreadImpl(); |
888 |
} |
|
889 |
||
890 |
final boolean isDispatchThreadImpl() { |
|
891 |
EventQueue eq = this; |
|
892 |
pushPopLock.lock(); |
|
893 |
try { |
|
894 |
EventQueue next = eq.nextQueue; |
|
895 |
while (next != null) { |
|
896 |
eq = next; |
|
897 |
next = eq.nextQueue; |
|
898 |
} |
|
899 |
return (Thread.currentThread() == eq.dispatchThread); |
|
900 |
} finally { |
|
901 |
pushPopLock.unlock(); |
|
2 | 902 |
} |
903 |
} |
|
904 |
||
905 |
final void initDispatchThread() { |
|
4365 | 906 |
pushPopLock.lock(); |
907 |
try { |
|
3969
6e77be3973ea
6878284: Sometimes test/javax/swing/system/6799345/TestShutdown.java "hangs"
dcherepanov
parents:
3084
diff
changeset
|
908 |
AppContext appContext = AppContext.getAppContext(); |
6e77be3973ea
6878284: Sometimes test/javax/swing/system/6799345/TestShutdown.java "hangs"
dcherepanov
parents:
3084
diff
changeset
|
909 |
if (dispatchThread == null && !threadGroup.isDestroyed() && !appContext.isDisposed()) { |
2 | 910 |
dispatchThread = (EventDispatchThread) |
911 |
AccessController.doPrivileged(new PrivilegedAction() { |
|
912 |
public Object run() { |
|
913 |
EventDispatchThread t = |
|
914 |
new EventDispatchThread(threadGroup, |
|
915 |
name, |
|
916 |
EventQueue.this); |
|
917 |
t.setContextClassLoader(classLoader); |
|
918 |
t.setPriority(Thread.NORM_PRIORITY + 1); |
|
919 |
t.setDaemon(false); |
|
920 |
return t; |
|
921 |
} |
|
922 |
}); |
|
923 |
AWTAutoShutdown.getInstance().notifyThreadBusy(dispatchThread); |
|
924 |
dispatchThread.start(); |
|
925 |
} |
|
4365 | 926 |
} finally { |
927 |
pushPopLock.unlock(); |
|
2 | 928 |
} |
929 |
} |
|
930 |
||
4365 | 931 |
final void detachDispatchThread(EventDispatchThread edt, boolean restart) { |
932 |
/* |
|
933 |
* This synchronized block is to secure that the event dispatch |
|
934 |
* thread won't die in the middle of posting a new event to the |
|
935 |
* associated event queue. It is important because we notify |
|
936 |
* that the event dispatch thread is busy after posting a new event |
|
937 |
* to its queue, so the EventQueue.dispatchThread reference must |
|
938 |
* be valid at that point. |
|
939 |
*/ |
|
940 |
pushPopLock.lock(); |
|
941 |
try { |
|
942 |
EventDispatchThread oldDispatchThread = dispatchThread; |
|
943 |
if (dispatchThread == edt) { |
|
944 |
dispatchThread = null; |
|
945 |
} |
|
946 |
if (restart) { |
|
947 |
/* |
|
948 |
* Event dispatch thread dies in case of an uncaught exception. |
|
949 |
* A new event dispatch thread for this queue will be started |
|
950 |
* only if a new event is posted to it. In case if no more |
|
951 |
* events are posted after this thread died all events that |
|
952 |
* currently are in the queue will never be dispatched. |
|
953 |
* |
|
954 |
* Fix for 4648733. Check both the associated java event |
|
955 |
* queue and the PostEventQueue. |
|
956 |
*/ |
|
957 |
if ((peekEvent() != null) || !SunToolkit.isPostEventQueueEmpty()) { |
|
958 |
initDispatchThread(); |
|
959 |
} |
|
960 |
AWTAutoShutdown.getInstance().notifyThreadFree(oldDispatchThread); |
|
961 |
} |
|
962 |
} finally { |
|
963 |
pushPopLock.unlock(); |
|
964 |
} |
|
2 | 965 |
} |
966 |
||
967 |
/* |
|
968 |
* Gets the <code>EventDispatchThread</code> for this |
|
969 |
* <code>EventQueue</code>. |
|
970 |
* @return the event dispatch thread associated with this event queue |
|
971 |
* or <code>null</code> if this event queue doesn't have a |
|
972 |
* working thread associated with it |
|
973 |
* @see java.awt.EventQueue#initDispatchThread |
|
974 |
* @see java.awt.EventQueue#detachDispatchThread |
|
975 |
*/ |
|
976 |
final EventDispatchThread getDispatchThread() { |
|
4365 | 977 |
pushPopLock.lock(); |
978 |
try { |
|
979 |
return dispatchThread; |
|
980 |
} finally { |
|
981 |
pushPopLock.unlock(); |
|
982 |
} |
|
2 | 983 |
} |
984 |
||
985 |
/* |
|
986 |
* Removes any pending events for the specified source object. |
|
987 |
* If removeAllEvents parameter is <code>true</code> then all |
|
988 |
* events for the specified source object are removed, if it |
|
989 |
* is <code>false</code> then <code>SequencedEvent</code>, <code>SentEvent</code>, |
|
990 |
* <code>FocusEvent</code>, <code>WindowEvent</code>, <code>KeyEvent</code>, |
|
991 |
* and <code>InputMethodEvent</code> are kept in the queue, but all other |
|
992 |
* events are removed. |
|
993 |
* |
|
994 |
* This method is normally called by the source's |
|
995 |
* <code>removeNotify</code> method. |
|
996 |
*/ |
|
997 |
final void removeSourceEvents(Object source, boolean removeAllEvents) { |
|
998 |
SunToolkit.flushPendingEvents(); |
|
4365 | 999 |
pushPopLock.lock(); |
1000 |
try { |
|
2 | 1001 |
for (int i = 0; i < NUM_PRIORITIES; i++) { |
1002 |
EventQueueItem entry = queues[i].head; |
|
1003 |
EventQueueItem prev = null; |
|
1004 |
while (entry != null) { |
|
1005 |
if ((entry.event.getSource() == source) |
|
1006 |
&& (removeAllEvents |
|
1007 |
|| ! (entry.event instanceof SequencedEvent |
|
1008 |
|| entry.event instanceof SentEvent |
|
1009 |
|| entry.event instanceof FocusEvent |
|
1010 |
|| entry.event instanceof WindowEvent |
|
1011 |
|| entry.event instanceof KeyEvent |
|
1012 |
|| entry.event instanceof InputMethodEvent))) |
|
1013 |
{ |
|
1014 |
if (entry.event instanceof SequencedEvent) { |
|
1015 |
((SequencedEvent)entry.event).dispose(); |
|
1016 |
} |
|
1017 |
if (entry.event instanceof SentEvent) { |
|
1018 |
((SentEvent)entry.event).dispose(); |
|
1019 |
} |
|
1020 |
if (prev == null) { |
|
1021 |
queues[i].head = entry.next; |
|
1022 |
} else { |
|
1023 |
prev.next = entry.next; |
|
1024 |
} |
|
1025 |
uncacheEQItem(entry); |
|
1026 |
} else { |
|
1027 |
prev = entry; |
|
1028 |
} |
|
1029 |
entry = entry.next; |
|
1030 |
} |
|
1031 |
queues[i].tail = prev; |
|
1032 |
} |
|
4365 | 1033 |
} finally { |
1034 |
pushPopLock.unlock(); |
|
2 | 1035 |
} |
1036 |
} |
|
1037 |
||
1038 |
static void setCurrentEventAndMostRecentTime(AWTEvent e) { |
|
1039 |
Toolkit.getEventQueue().setCurrentEventAndMostRecentTimeImpl(e); |
|
1040 |
} |
|
4365 | 1041 |
private void setCurrentEventAndMostRecentTimeImpl(AWTEvent e) { |
1042 |
pushPopLock.lock(); |
|
1043 |
try { |
|
1044 |
if (Thread.currentThread() != dispatchThread) { |
|
1045 |
return; |
|
1046 |
} |
|
2 | 1047 |
|
4365 | 1048 |
currentEvent = new WeakReference(e); |
2 | 1049 |
|
4365 | 1050 |
// This series of 'instanceof' checks should be replaced with a |
1051 |
// polymorphic type (for example, an interface which declares a |
|
1052 |
// getWhen() method). However, this would require us to make such |
|
1053 |
// a type public, or to place it in sun.awt. Both of these approaches |
|
1054 |
// have been frowned upon. So for now, we hack. |
|
1055 |
// |
|
1056 |
// In tiger, we will probably give timestamps to all events, so this |
|
1057 |
// will no longer be an issue. |
|
1058 |
long mostRecentEventTime2 = Long.MIN_VALUE; |
|
1059 |
if (e instanceof InputEvent) { |
|
1060 |
InputEvent ie = (InputEvent)e; |
|
1061 |
mostRecentEventTime2 = ie.getWhen(); |
|
1062 |
} else if (e instanceof InputMethodEvent) { |
|
1063 |
InputMethodEvent ime = (InputMethodEvent)e; |
|
1064 |
mostRecentEventTime2 = ime.getWhen(); |
|
1065 |
} else if (e instanceof ActionEvent) { |
|
1066 |
ActionEvent ae = (ActionEvent)e; |
|
1067 |
mostRecentEventTime2 = ae.getWhen(); |
|
1068 |
} else if (e instanceof InvocationEvent) { |
|
1069 |
InvocationEvent ie = (InvocationEvent)e; |
|
1070 |
mostRecentEventTime2 = ie.getWhen(); |
|
1071 |
} |
|
1072 |
mostRecentEventTime = Math.max(mostRecentEventTime, mostRecentEventTime2); |
|
1073 |
} finally { |
|
1074 |
pushPopLock.unlock(); |
|
2 | 1075 |
} |
1076 |
} |
|
1077 |
||
1078 |
/** |
|
1079 |
* Causes <code>runnable</code> to have its <code>run</code> |
|
1080 |
* method called in the {@link #isDispatchThread dispatch thread} of |
|
1081 |
* {@link Toolkit#getSystemEventQueue the system EventQueue}. |
|
1082 |
* This will happen after all pending events are processed. |
|
1083 |
* |
|
1084 |
* @param runnable the <code>Runnable</code> whose <code>run</code> |
|
1085 |
* method should be executed |
|
1086 |
* asynchronously in the |
|
1087 |
* {@link #isDispatchThread event dispatch thread} |
|
1088 |
* of {@link Toolkit#getSystemEventQueue the system EventQueue} |
|
1089 |
* @see #invokeAndWait |
|
1090 |
* @see Toolkit#getSystemEventQueue |
|
1091 |
* @see #isDispatchThread |
|
1092 |
* @since 1.2 |
|
1093 |
*/ |
|
1094 |
public static void invokeLater(Runnable runnable) { |
|
1095 |
Toolkit.getEventQueue().postEvent( |
|
1096 |
new InvocationEvent(Toolkit.getDefaultToolkit(), runnable)); |
|
1097 |
} |
|
1098 |
||
1099 |
/** |
|
1100 |
* Causes <code>runnable</code> to have its <code>run</code> |
|
1101 |
* method called in the {@link #isDispatchThread dispatch thread} of |
|
1102 |
* {@link Toolkit#getSystemEventQueue the system EventQueue}. |
|
1103 |
* This will happen after all pending events are processed. |
|
1104 |
* The call blocks until this has happened. This method |
|
1105 |
* will throw an Error if called from the |
|
1106 |
* {@link #isDispatchThread event dispatcher thread}. |
|
1107 |
* |
|
1108 |
* @param runnable the <code>Runnable</code> whose <code>run</code> |
|
1109 |
* method should be executed |
|
1110 |
* synchronously in the |
|
1111 |
* {@link #isDispatchThread event dispatch thread} |
|
1112 |
* of {@link Toolkit#getSystemEventQueue the system EventQueue} |
|
1113 |
* @exception InterruptedException if any thread has |
|
1114 |
* interrupted this thread |
|
1115 |
* @exception InvocationTargetException if an throwable is thrown |
|
1116 |
* when running <code>runnable</code> |
|
1117 |
* @see #invokeLater |
|
1118 |
* @see Toolkit#getSystemEventQueue |
|
1119 |
* @see #isDispatchThread |
|
1120 |
* @since 1.2 |
|
1121 |
*/ |
|
1122 |
public static void invokeAndWait(Runnable runnable) |
|
1123 |
throws InterruptedException, InvocationTargetException { |
|
1124 |
||
1125 |
if (EventQueue.isDispatchThread()) { |
|
1126 |
throw new Error("Cannot call invokeAndWait from the event dispatcher thread"); |
|
1127 |
} |
|
1128 |
||
1129 |
class AWTInvocationLock {} |
|
1130 |
Object lock = new AWTInvocationLock(); |
|
1131 |
||
1132 |
InvocationEvent event = |
|
1133 |
new InvocationEvent(Toolkit.getDefaultToolkit(), runnable, lock, |
|
1134 |
true); |
|
1135 |
||
1136 |
synchronized (lock) { |
|
1137 |
Toolkit.getEventQueue().postEvent(event); |
|
4264
40c232605c68
6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents:
3972
diff
changeset
|
1138 |
while (!event.isDispatched()) { |
40c232605c68
6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents:
3972
diff
changeset
|
1139 |
lock.wait(); |
40c232605c68
6852111: Unhandled 'spurious wakeup' in java.awt.EventQueue.invokeAndWait()
dcherepanov
parents:
3972
diff
changeset
|
1140 |
} |
2 | 1141 |
} |
1142 |
||
1143 |
Throwable eventThrowable = event.getThrowable(); |
|
1144 |
if (eventThrowable != null) { |
|
1145 |
throw new InvocationTargetException(eventThrowable); |
|
1146 |
} |
|
1147 |
} |
|
1148 |
||
1149 |
/* |
|
1150 |
* Called from PostEventQueue.postEvent to notify that a new event |
|
1151 |
* appeared. First it proceeds to the EventQueue on the top of the |
|
1152 |
* stack, then notifies the associated dispatch thread if it exists |
|
1153 |
* or starts a new one otherwise. |
|
1154 |
*/ |
|
1155 |
private void wakeup(boolean isShutdown) { |
|
4365 | 1156 |
pushPopLock.lock(); |
1157 |
try { |
|
2 | 1158 |
if (nextQueue != null) { |
1159 |
// Forward call to the top of EventQueue stack. |
|
1160 |
nextQueue.wakeup(isShutdown); |
|
1161 |
} else if (dispatchThread != null) { |
|
4365 | 1162 |
pushPopCond.signalAll(); |
2 | 1163 |
} else if (!isShutdown) { |
1164 |
initDispatchThread(); |
|
1165 |
} |
|
4365 | 1166 |
} finally { |
1167 |
pushPopLock.unlock(); |
|
2 | 1168 |
} |
1169 |
} |
|
1170 |
} |
|
1171 |
||
1172 |
/** |
|
1173 |
* The Queue object holds pointers to the beginning and end of one internal |
|
1174 |
* queue. An EventQueue object is composed of multiple internal Queues, one |
|
1175 |
* for each priority supported by the EventQueue. All Events on a particular |
|
1176 |
* internal Queue have identical priority. |
|
1177 |
*/ |
|
1178 |
class Queue { |
|
1179 |
EventQueueItem head; |
|
1180 |
EventQueueItem tail; |
|
1181 |
} |