1 7 8 package java.awt; 9 10 import java.awt.event.InputEvent ; 11 import java.awt.event.MouseEvent ; 12 import java.awt.event.ActionEvent ; 13 import java.awt.event.WindowEvent ; 14 import java.lang.reflect.Method ; 15 import java.security.AccessController ; 16 import sun.security.action.GetPropertyAction; 17 import sun.awt.DebugHelper; 18 import sun.awt.AWTAutoShutdown; 19 import sun.awt.SunToolkit; 20 21 import sun.awt.dnd.SunDragSourceContextPeer; 22 23 43 class EventDispatchThread extends Thread { 44 private static final DebugHelper dbg = DebugHelper.create(EventDispatchThread .class); 45 46 private EventQueue theQueue; 47 private boolean doDispatch = true; 48 private static final int ANY_EVENT = -1; 49 50 EventDispatchThread(ThreadGroup group, String name, EventQueue queue) { 51 super(group, name); 52 theQueue = queue; 53 } 54 55 void stopDispatchingImpl(boolean wait) { 56 60 StopDispatchEvent stopEvent = new StopDispatchEvent(); 61 62 if (Thread.currentThread() != this) { 64 65 theQueue.postEventPrivate(stopEvent); 73 74 if (wait) { 75 try { 76 join(); 77 } catch(InterruptedException e) { 78 } 79 } 80 } else { 81 stopEvent.dispatch(); 82 } 83 synchronized (theQueue) { 84 if (theQueue.getDispatchThread() == this) { 85 theQueue.detachDispatchThread(); 86 } 87 } 88 } 89 90 public void stopDispatching() { 91 stopDispatchingImpl(true); 92 } 93 94 public void stopDispatchingLater() { 95 stopDispatchingImpl(false); 96 } 97 98 class StopDispatchEvent extends AWTEvent implements ActiveEvent { 99 public StopDispatchEvent() { 100 super(EventDispatchThread.this,0); 101 } 102 103 public void dispatch() { 104 doDispatch = false; 105 } 106 } 107 108 public void run() { 109 try { 110 pumpEvents(new Conditional () { 111 public boolean evaluate() { 112 return true; 113 } 114 }); 115 } finally { 116 124 synchronized (theQueue) { 125 if (theQueue.getDispatchThread() == this) { 126 theQueue.detachDispatchThread(); 127 } 128 135 139 if (theQueue.peekEvent() != null || 140 !SunToolkit.isPostEventQueueEmpty()) { 141 theQueue.initDispatchThread(); 142 } 143 AWTAutoShutdown.getInstance().notifyThreadFree(this); 144 } 145 } 146 } 147 148 void pumpEvents(Conditional cond) { 149 pumpEvents(ANY_EVENT, cond); 150 } 151 152 void pumpEventsForHierarchy(Conditional cond, Component modalComponent) { 153 pumpEventsForHierarchy(ANY_EVENT, cond, modalComponent); 154 } 155 156 void pumpEvents(int id, Conditional cond) { 157 pumpEventsForHierarchy(id, cond, null); 158 } 159 160 void pumpEventsForHierarchy(int id, Conditional cond, Component modalComponent) 161 { 162 while (doDispatch && cond.evaluate()) { 163 if (isInterrupted() || !pumpOneEventForHierarchy(id, modalComponent)) { 164 doDispatch = false; 165 } 166 } 167 } 168 169 boolean checkMouseEventForModalJInternalFrame(MouseEvent me, Component modalComp) { 170 if (modalComp instanceof javax.swing.JInternalFrame ) { 174 Container c; 175 synchronized (modalComp.getTreeLock()) { 176 c = ((Container )modalComp).getHeavyweightContainer(); 177 } 178 if (me.getSource() == c) 179 return true; 180 } 181 return false; 182 } 183 184 boolean pumpOneEventForHierarchy(int id, Component modalComponent) { 185 try { 186 AWTEvent event; 187 boolean eventOK; 188 do { 189 event = (id == ANY_EVENT) 190 ? theQueue.getNextEvent() 191 : theQueue.getNextEvent(id); 192 193 eventOK = true; 194 if (modalComponent != null) { 195 201 int eventID = event.getID(); 202 if (((eventID >= MouseEvent.MOUSE_FIRST && 203 eventID <= MouseEvent.MOUSE_LAST) && 204 !(checkMouseEventForModalJInternalFrame((MouseEvent ) 205 event, modalComponent))) || 206 (eventID >= ActionEvent.ACTION_FIRST && 207 eventID <= ActionEvent.ACTION_LAST) || 208 eventID == WindowEvent.WINDOW_CLOSING) { 209 Object o = event.getSource(); 210 if (o instanceof sun.awt.ModalExclude) { 211 } else if (o instanceof Component ) { 214 Component c = (Component ) o; 215 boolean modalExcluded = false; 216 if (modalComponent instanceof Container ) { 217 while (c != modalComponent && c != null) { 218 if ((c instanceof Window ) && 219 (sun.awt.SunToolkit.isModalExcluded((Window )c))) { 220 modalExcluded = true; 223 break; 224 } 225 c = c.getParent(); 226 } 227 } 228 if (!modalExcluded && (c != modalComponent)) { 229 eventOK = false; 230 } 231 } 232 } 233 } 234 eventOK = eventOK && SunDragSourceContextPeer.checkEvent(event); 235 if (!eventOK) { 236 event.consume(); 237 } 238 } while (eventOK == false); 239 240 if ( dbg.on ) dbg.println("Dispatching: "+event); 241 242 theQueue.dispatchEvent(event); 243 return true; 244 } catch (ThreadDeath death) { 245 return false; 246 247 } catch (InterruptedException interruptedException) { 248 return false; 251 } catch (RuntimeException e) { 253 processException(e, modalComponent != null); 254 } catch (Error e) { 255 processException(e, modalComponent != null); 256 } 257 return true; 258 } 259 260 private void processException(Throwable e, boolean isModal) { 261 if (!handleException(e)) { 262 if (isModal) { 272 System.err.println( 273 "Exception occurred during event dispatching:"); 274 e.printStackTrace(); 275 } else if (e instanceof RuntimeException ) { 276 throw (RuntimeException )e; 277 } else if (e instanceof Error ) { 278 throw (Error )e; 279 } 280 } 281 } 282 283 private static final String handlerPropName = "sun.awt.exception.handler"; 284 private static String handlerClassName = null; 285 private static String NO_HANDLER = new String (); 286 287 323 private boolean handleException(Throwable thrown) { 324 325 try { 326 327 if (handlerClassName == NO_HANDLER) { 328 return false; 329 } 330 331 332 if (handlerClassName == null) { 333 handlerClassName = ((String ) AccessController.doPrivileged( 334 new GetPropertyAction(handlerPropName))); 335 if (handlerClassName == null) { 336 handlerClassName = NO_HANDLER; 337 return false; 338 } 339 } 340 341 342 Method m; 343 Object h; 344 try { 345 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 346 Class c = Class.forName(handlerClassName, true, cl); 347 m = c.getMethod("handle", new Class [] { Throwable .class }); 348 h = c.newInstance(); 349 } catch (Throwable x) { 350 handlerClassName = NO_HANDLER; 351 return false; 352 } 353 354 355 m.invoke(h, new Object [] { thrown }); 356 357 } catch (Throwable x) { 358 return false; 359 } 360 361 return true; 362 } 363 364 boolean isDispatching(EventQueue eq) { 365 return theQueue.equals(eq); 366 } 367 368 EventQueue getEventQueue() { return theQueue; } 369 } 370 | Popular Tags |