src/share/classes/java/awt/EventDispatchThread.java

Print this page




  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.Vector;
  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     private boolean threadDeathCaught = false;
  70 
  71     private static final int ANY_EVENT = -1;
  72 
  73     private Vector<EventFilter> eventFilters = new Vector<EventFilter>();
  74 
  75     EventDispatchThread(ThreadGroup group, String name, EventQueue queue) {
  76         super(group, name);
  77         setEventQueue(queue);
  78     }
  79 
  80     /*
  81      * Must be called on EDT only, that's why no synchronization
  82      */
  83     public void stopDispatching() {
  84         doDispatch = false;
  85     }
  86 
  87     public void run() {
  88         while (true) {
  89             try {
  90                 pumpEvents(new Conditional() {
  91                     public boolean evaluate() {
  92                         return true;
  93                     }
  94                 });
  95             } finally {
  96                 EventQueue eq = getEventQueue();
  97                 if (eq.detachDispatchThread(this) || threadDeathCaught) {
  98                     break;
  99                 }
 100             }
 101         }
 102     }
 103 
 104     void pumpEvents(Conditional cond) {
 105         pumpEvents(ANY_EVENT, cond);
 106     }
 107 
 108     void pumpEventsForHierarchy(Conditional cond, Component modalComponent) {
 109         pumpEventsForHierarchy(ANY_EVENT, cond, modalComponent);
 110     }
 111 
 112     void pumpEvents(int id, Conditional cond) {
 113         pumpEventsForHierarchy(id, cond, null);
 114     }
 115 
 116     void pumpEventsForHierarchy(int id, Conditional cond, Component modalComponent) {
 117         pumpEventsForFilter(id, cond, new HierarchyEventFilter(modalComponent));


 199                 }
 200             }
 201             while (eventOK == false);
 202 
 203             if (eventLog.isLoggable(PlatformLogger.FINEST)) {
 204                 eventLog.finest("Dispatching: " + event);
 205             }
 206 
 207             Object handle = null;
 208             if (delegate != null) {
 209                 handle = delegate.beforeDispatch(event);
 210             }
 211             eq.dispatchEvent(event);
 212             if (delegate != null) {
 213                 delegate.afterDispatch(event, handle);
 214             }
 215 
 216             return true;
 217         }
 218         catch (ThreadDeath death) {
 219             threadDeathCaught = true;
 220             return false;
 221 
 222         }
 223         catch (InterruptedException interruptedException) {
 224             return false; // AppContext.dispose() interrupts all
 225                           // Threads in the AppContext
 226 
 227         }
 228         catch (Throwable e) {
 229             processException(e);
 230         }
 231 
 232         return true;
 233     }
 234 
 235     private void processException(Throwable e) {
 236         if (eventLog.isLoggable(PlatformLogger.FINE)) {
 237             eventLog.fine("Processing exception: " + e);
 238         }
 239         getUncaughtExceptionHandler().uncaughtException(this, e);
 240     }
 241 




  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));


 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