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 void pumpEvents(Conditional cond) { 111 pumpEvents(ANY_EVENT, cond); 112 } 113 114 void pumpEventsForHierarchy(Conditional cond, Component modalComponent) { 115 pumpEventsForHierarchy(ANY_EVENT, cond, modalComponent); 116 } 117 118 void pumpEvents(int id, Conditional cond) { 119 pumpEventsForHierarchy(id, cond, null); 120 } 121 122 void pumpEventsForHierarchy(int id, Conditional cond, Component modalComponent) { 123 pumpEventsForFilter(id, cond, new HierarchyEventFilter(modalComponent)); 124 } 125 126 void pumpEventsForFilter(Conditional cond, EventFilter filter) { 127 pumpEventsForFilter(ANY_EVENT, cond, filter); 128 } 129 130 void pumpEventsForFilter(int id, Conditional cond, EventFilter filter) { 131 addEventFilter(filter); 132 doDispatch = !shutdown; 133 while (doDispatch && cond.evaluate()) { 134 if (shutdown || !pumpOneEventForFilters(id)) { 135 doDispatch = false; 136 } 137 } 138 removeEventFilter(filter); 139 } 140 141 void addEventFilter(EventFilter filter) { 142 eventLog.finest("adding the event filter: " + filter); 143 synchronized (eventFilters) { 144 if (!eventFilters.contains(filter)) { 145 if (filter instanceof ModalEventFilter) { 146 ModalEventFilter newFilter = (ModalEventFilter)filter; 147 int k = 0; 148 for (k = 0; k < eventFilters.size(); k++) { 149 EventFilter f = eventFilters.get(k); 150 if (f instanceof ModalEventFilter) { 151 ModalEventFilter cf = (ModalEventFilter)f; 152 if (cf.compareTo(newFilter) > 0) { 153 break; 154 } 155 } 156 } 157 eventFilters.add(k, filter); 158 } else { 159 eventFilters.add(filter); 160 } 161 } 162 } 163 } 164 165 void removeEventFilter(EventFilter filter) { 166 eventLog.finest("removing the event filter: " + filter); 167 synchronized (eventFilters) { 168 eventFilters.remove(filter); 169 } 170 } 171 172 boolean pumpOneEventForFilters(int id) { 173 AWTEvent event = null; 174 boolean eventOK = false; 175 try { 176 EventQueue eq = null; 177 EventQueueDelegate.Delegate delegate = null; 178 do { 179 // EventQueue may change during the dispatching 180 eq = getEventQueue(); 181 delegate = EventQueueDelegate.getDelegate(); 182 183 if (delegate != null && id == ANY_EVENT) { 184 event = delegate.getNextEvent(eq); 185 } else { 186 event = (id == ANY_EVENT) ? eq.getNextEvent() : eq.getNextEvent(id); 187 } 188 189 eventOK = true; 190 synchronized (eventFilters) { 191 for (int i = eventFilters.size() - 1; i >= 0; i--) { 192 EventFilter f = eventFilters.get(i); 193 EventFilter.FilterAction accept = f.acceptEvent(event); 194 if (accept == EventFilter.FilterAction.REJECT) { 195 eventOK = false; 196 break; 197 } else if (accept == EventFilter.FilterAction.ACCEPT_IMMEDIATELY) { 198 break; 199 } 200 } 201 } 202 eventOK = eventOK && SunDragSourceContextPeer.checkEvent(event); 203 if (!eventOK) { 204 event.consume(); 205 } 206 } 207 while (eventOK == false); 208 209 if (eventLog.isLoggable(PlatformLogger.FINEST)) { 210 eventLog.finest("Dispatching: " + event); 211 } 212 213 Object handle = null; 214 if (delegate != null) { 215 handle = delegate.beforeDispatch(event); 216 } 217 eq.dispatchEvent(event); 218 if (delegate != null) { 219 delegate.afterDispatch(event, handle); 220 } 221 222 return true; 223 } 224 catch (ThreadDeath death) { 225 shutdown = true; 226 throw death; 227 } 228 catch (InterruptedException interruptedException) { 229 shutdown = true; 230 return false; // AppContext.dispose() interrupts all 231 // Threads in the AppContext 232 233 } 234 catch (Throwable e) { 235 processException(e); 236 } 237 238 return true; 239 } 240 241 private void processException(Throwable e) { 242 if (eventLog.isLoggable(PlatformLogger.FINE)) { 243 eventLog.fine("Processing exception: " + e); 244 } 245 getUncaughtExceptionHandler().uncaughtException(this, e); 246 } 247 248 public synchronized EventQueue getEventQueue() { 249 return theQueue; 250 } 251 public synchronized void setEventQueue(EventQueue eq) { 252 theQueue = eq; 253 } 254 255 private static class HierarchyEventFilter implements EventFilter { 256 private Component modalComponent; 257 public HierarchyEventFilter(Component modalComponent) { 258 this.modalComponent = modalComponent; 259 } 260 public FilterAction acceptEvent(AWTEvent event) { 261 if (modalComponent != null) { 262 int eventID = event.getID(); 263 boolean mouseEvent = (eventID >= MouseEvent.MOUSE_FIRST) && 264 (eventID <= MouseEvent.MOUSE_LAST); 265 boolean actionEvent = (eventID >= ActionEvent.ACTION_FIRST) && 266 (eventID <= ActionEvent.ACTION_LAST); 267 boolean windowClosingEvent = (eventID == WindowEvent.WINDOW_CLOSING); 268 /* 269 * filter out MouseEvent and ActionEvent that's outside 270 * the modalComponent hierarchy. 271 * KeyEvent is handled by using enqueueKeyEvent 272 * in Dialog.show 273 */ 274 if (Component.isInstanceOf(modalComponent, "javax.swing.JInternalFrame")) { 275 /* 276 * Modal internal frames are handled separately. If event is 277 * for some component from another heavyweight than modalComp, 278 * it is accepted. If heavyweight is the same - we still accept 279 * event and perform further filtering in LightweightDispatcher 280 */ 281 return windowClosingEvent ? FilterAction.REJECT : FilterAction.ACCEPT; 282 } 283 if (mouseEvent || actionEvent || windowClosingEvent) { 284 Object o = event.getSource(); 285 if (o instanceof sun.awt.ModalExclude) { 286 // Exclude this object from modality and 287 // continue to pump it's events. 288 return FilterAction.ACCEPT; 289 } else if (o instanceof Component) { 290 Component c = (Component) o; 291 // 5.0u3 modal exclusion 292 boolean modalExcluded = false; 293 if (modalComponent instanceof Container) { 294 while (c != modalComponent && c != null) { 295 if ((c instanceof Window) && 296 (sun.awt.SunToolkit.isModalExcluded((Window)c))) { 297 // Exclude this window and all its children from 298 // modality and continue to pump it's events. 299 modalExcluded = true; 300 break; 301 } 302 c = c.getParent(); 303 } 304 } 305 if (!modalExcluded && (c != modalComponent)) { 306 return FilterAction.REJECT; 307 } 308 } 309 } 310 } 311 return FilterAction.ACCEPT; 312 } 313 } 314 }