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