2
|
1 |
/*
|
|
2 |
* Copyright 2000-2007 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package java.awt;
|
|
27 |
|
|
28 |
import java.util.LinkedList;
|
|
29 |
import sun.awt.AppContext;
|
|
30 |
import sun.awt.SunToolkit;
|
|
31 |
|
|
32 |
/**
|
|
33 |
* A mechanism for ensuring that a series of AWTEvents are executed in a
|
|
34 |
* precise order, even across multiple AppContexts. The nested events will be
|
|
35 |
* dispatched in the order in which their wrapping SequencedEvents were
|
|
36 |
* constructed. The only exception to this rule is if the peer of the target of
|
|
37 |
* the nested event was destroyed (with a call to Component.removeNotify)
|
|
38 |
* before the wrapping SequencedEvent was able to be dispatched. In this case,
|
|
39 |
* the nested event is never dispatched.
|
|
40 |
*
|
|
41 |
* @author David Mendenhall
|
|
42 |
*/
|
|
43 |
class SequencedEvent extends AWTEvent implements ActiveEvent {
|
|
44 |
/*
|
|
45 |
* serialVersionUID
|
|
46 |
*/
|
|
47 |
private static final long serialVersionUID = 547742659238625067L;
|
|
48 |
|
|
49 |
private static final int ID =
|
|
50 |
java.awt.event.FocusEvent.FOCUS_LAST + 1;
|
|
51 |
private static final LinkedList list = new LinkedList();
|
|
52 |
|
|
53 |
private final AWTEvent nested;
|
|
54 |
private AppContext appContext;
|
|
55 |
private boolean disposed;
|
|
56 |
|
|
57 |
/**
|
|
58 |
* Constructs a new SequencedEvent which will dispatch the specified
|
|
59 |
* nested event.
|
|
60 |
*
|
|
61 |
* @param nested the AWTEvent which this SequencedEvent's dispatch()
|
|
62 |
* method will dispatch
|
|
63 |
*/
|
|
64 |
public SequencedEvent(AWTEvent nested) {
|
|
65 |
super(nested.getSource(), ID);
|
|
66 |
this.nested = nested;
|
|
67 |
synchronized (SequencedEvent.class) {
|
|
68 |
list.add(this);
|
|
69 |
}
|
|
70 |
}
|
|
71 |
|
|
72 |
/**
|
|
73 |
* Dispatches the nested event after all previous nested events have been
|
|
74 |
* dispatched or disposed. If this method is invoked before all previous nested events
|
|
75 |
* have been dispatched, then this method blocks until such a point is
|
|
76 |
* reached.
|
|
77 |
* While waiting disposes nested events to disposed AppContext
|
|
78 |
*
|
|
79 |
* NOTE: Locking protocol. Since dispose() can get EventQueue lock,
|
|
80 |
* dispatch() shall never call dispose() while holding the lock on the list,
|
|
81 |
* as EventQueue lock is held during dispatching. The locks should be acquired
|
|
82 |
* in the same order.
|
|
83 |
*/
|
|
84 |
public final void dispatch() {
|
|
85 |
try {
|
|
86 |
appContext = AppContext.getAppContext();
|
|
87 |
|
|
88 |
if (getFirst() != this) {
|
|
89 |
if (EventQueue.isDispatchThread()) {
|
|
90 |
EventDispatchThread edt = (EventDispatchThread)
|
|
91 |
Thread.currentThread();
|
|
92 |
edt.pumpEvents(SentEvent.ID, new Conditional() {
|
|
93 |
public boolean evaluate() {
|
|
94 |
return !SequencedEvent.this.isFirstOrDisposed();
|
|
95 |
}
|
|
96 |
});
|
|
97 |
} else {
|
|
98 |
while(!isFirstOrDisposed()) {
|
|
99 |
synchronized (SequencedEvent.class) {
|
|
100 |
try {
|
|
101 |
SequencedEvent.class.wait(1000);
|
|
102 |
} catch (InterruptedException e) {
|
|
103 |
break;
|
|
104 |
}
|
|
105 |
}
|
|
106 |
}
|
|
107 |
}
|
|
108 |
}
|
|
109 |
|
|
110 |
if (!disposed) {
|
|
111 |
KeyboardFocusManager.getCurrentKeyboardFocusManager().
|
|
112 |
setCurrentSequencedEvent(this);
|
|
113 |
Toolkit.getEventQueue().dispatchEvent(nested);
|
|
114 |
}
|
|
115 |
} finally {
|
|
116 |
dispose();
|
|
117 |
}
|
|
118 |
}
|
|
119 |
|
|
120 |
/**
|
|
121 |
* true only if event exists and nested source appContext is disposed.
|
|
122 |
*/
|
|
123 |
private final static boolean isOwnerAppContextDisposed(SequencedEvent se) {
|
|
124 |
if (se != null) {
|
|
125 |
Object target = se.nested.getSource();
|
|
126 |
if (target instanceof Component) {
|
|
127 |
return ((Component)target).appContext.isDisposed();
|
|
128 |
}
|
|
129 |
}
|
|
130 |
return false;
|
|
131 |
}
|
|
132 |
|
|
133 |
/**
|
|
134 |
* Sequenced events are dispatched in order, so we cannot dispatch
|
|
135 |
* until we are the first sequenced event in the queue (i.e. it's our
|
|
136 |
* turn). But while we wait for our turn to dispatch, the event
|
|
137 |
* could have been disposed for a number of reasons.
|
|
138 |
*/
|
|
139 |
public final boolean isFirstOrDisposed() {
|
|
140 |
if (disposed) {
|
|
141 |
return true;
|
|
142 |
}
|
|
143 |
// getFirstWithContext can dispose this
|
|
144 |
return this == getFirstWithContext() || disposed;
|
|
145 |
}
|
|
146 |
|
|
147 |
private final synchronized static SequencedEvent getFirst() {
|
|
148 |
return (SequencedEvent)list.getFirst();
|
|
149 |
}
|
|
150 |
|
|
151 |
/* Disposes all events from disposed AppContext
|
|
152 |
* return first valid event
|
|
153 |
*/
|
|
154 |
private final static SequencedEvent getFirstWithContext() {
|
|
155 |
SequencedEvent first = getFirst();
|
|
156 |
while(isOwnerAppContextDisposed(first)) {
|
|
157 |
first.dispose();
|
|
158 |
first = getFirst();
|
|
159 |
}
|
|
160 |
return first;
|
|
161 |
}
|
|
162 |
|
|
163 |
/**
|
|
164 |
* Disposes of this instance. This method is invoked once the nested event
|
|
165 |
* has been dispatched and handled, or when the peer of the target of the
|
|
166 |
* nested event has been disposed with a call to Component.removeNotify.
|
|
167 |
*
|
|
168 |
* NOTE: Locking protocol. Since SunToolkit.postEvent can get EventQueue lock,
|
|
169 |
* it shall never be called while holding the lock on the list,
|
|
170 |
* as EventQueue lock is held during dispatching and dispatch() will get
|
|
171 |
* lock on the list. The locks should be acquired in the same order.
|
|
172 |
*/
|
|
173 |
final void dispose() {
|
|
174 |
synchronized (SequencedEvent.class) {
|
|
175 |
if (disposed) {
|
|
176 |
return;
|
|
177 |
}
|
|
178 |
if (KeyboardFocusManager.getCurrentKeyboardFocusManager().
|
|
179 |
getCurrentSequencedEvent() == this) {
|
|
180 |
KeyboardFocusManager.getCurrentKeyboardFocusManager().
|
|
181 |
setCurrentSequencedEvent(null);
|
|
182 |
}
|
|
183 |
disposed = true;
|
|
184 |
}
|
|
185 |
// Wake myself up
|
|
186 |
if (appContext != null) {
|
|
187 |
SunToolkit.postEvent(appContext, new SentEvent());
|
|
188 |
}
|
|
189 |
|
|
190 |
SequencedEvent next = null;
|
|
191 |
|
|
192 |
synchronized (SequencedEvent.class) {
|
|
193 |
SequencedEvent.class.notifyAll();
|
|
194 |
|
|
195 |
if (list.getFirst() == this) {
|
|
196 |
list.removeFirst();
|
|
197 |
|
|
198 |
if (!list.isEmpty()) {
|
|
199 |
next = (SequencedEvent)list.getFirst();
|
|
200 |
}
|
|
201 |
} else {
|
|
202 |
list.remove(this);
|
|
203 |
}
|
|
204 |
}
|
|
205 |
// Wake up waiting threads
|
|
206 |
if (next != null && next.appContext != null) {
|
|
207 |
SunToolkit.postEvent(next.appContext, new SentEvent());
|
|
208 |
}
|
|
209 |
}
|
|
210 |
}
|