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