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