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

Print this page




  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.MouseEvent;
  29 import java.awt.event.ActionEvent;
  30 import java.awt.event.WindowEvent;
  31 
  32 import java.util.ArrayList;
  33 import sun.util.logging.PlatformLogger;
  34 
  35 import sun.awt.dnd.SunDragSourceContextPeer;
  36 import sun.awt.EventQueueDelegate;
  37 
  38 /**
  39  * EventDispatchThread is a package-private AWT class which takes
  40  * events off the EventQueue and dispatches them to the appropriate
  41  * AWT components.
  42  *
  43  * The Thread starts a "permanent" event pump with a call to
  44  * pumpEvents(Conditional) in its run() method. Event handlers can choose to
  45  * block this event pump at any time, but should start a new pump (<b>not</b>
  46  * a new EventDispatchThread) by again calling pumpEvents(Conditional). This
  47  * secondary event pump will exit automatically as soon as the Condtional
  48  * evaluate()s to false and an additional Event is pumped and dispatched.
  49  *
  50  * @author Tom Ball
  51  * @author Amy Fowler
  52  * @author Fred Ecks
  53  * @author David Mendenhall
  54  *
  55  * @since 1.1
  56  */


 141                     eventFilters.add(filter);
 142                 }
 143             }
 144         }
 145     }
 146 
 147     void removeEventFilter(EventFilter filter) {
 148         if (eventLog.isLoggable(PlatformLogger.Level.FINEST)) {
 149             eventLog.finest("removing the event filter: " + filter);
 150         }
 151         synchronized (eventFilters) {
 152             eventFilters.remove(filter);
 153         }
 154     }
 155 
 156     void pumpOneEventForFilters(int id) {
 157         AWTEvent event = null;
 158         boolean eventOK = false;
 159         try {
 160             EventQueue eq = null;
 161             EventQueueDelegate.Delegate delegate = null;
 162             do {
 163                 // EventQueue may change during the dispatching
 164                 eq = getEventQueue();
 165                 delegate = EventQueueDelegate.getDelegate();
 166 
 167                 if (delegate != null && id == ANY_EVENT) {
 168                     event = delegate.getNextEvent(eq);
 169                 } else {
 170                     event = (id == ANY_EVENT) ? eq.getNextEvent() : eq.getNextEvent(id);
 171                 }
 172 
 173                 eventOK = true;
 174                 synchronized (eventFilters) {
 175                     for (int i = eventFilters.size() - 1; i >= 0; i--) {
 176                         EventFilter f = eventFilters.get(i);
 177                         EventFilter.FilterAction accept = f.acceptEvent(event);
 178                         if (accept == EventFilter.FilterAction.REJECT) {
 179                             eventOK = false;
 180                             break;
 181                         } else if (accept == EventFilter.FilterAction.ACCEPT_IMMEDIATELY) {
 182                             break;
 183                         }
 184                     }
 185                 }
 186                 eventOK = eventOK && SunDragSourceContextPeer.checkEvent(event);
 187                 if (!eventOK) {
 188                     event.consume();
 189                 }
 190             }
 191             while (eventOK == false);
 192 
 193             if (eventLog.isLoggable(PlatformLogger.Level.FINEST)) {
 194                 eventLog.finest("Dispatching: " + event);
 195             }
 196 
 197             Object handle = null;
 198             if (delegate != null) {
 199                 handle = delegate.beforeDispatch(event);
 200             }
 201             eq.dispatchEvent(event);
 202             if (delegate != null) {
 203                 delegate.afterDispatch(event, handle);
 204             }
 205         }
 206         catch (ThreadDeath death) {
 207             doDispatch = false;
 208             throw death;
 209         }
 210         catch (InterruptedException interruptedException) {
 211             doDispatch = false; // AppContext.dispose() interrupts all
 212                                 // Threads in the AppContext
 213         }
 214         catch (Throwable e) {
 215             processException(e);
 216         }
 217     }
 218 
 219     private void processException(Throwable e) {
 220         if (eventLog.isLoggable(PlatformLogger.Level.FINE)) {
 221             eventLog.fine("Processing exception: " + e);
 222         }
 223         getUncaughtExceptionHandler().uncaughtException(this, e);
 224     }




  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.MouseEvent;
  29 import java.awt.event.ActionEvent;
  30 import java.awt.event.WindowEvent;
  31 
  32 import java.util.ArrayList;
  33 import sun.util.logging.PlatformLogger;
  34 
  35 import sun.awt.dnd.SunDragSourceContextPeer;

  36 
  37 /**
  38  * EventDispatchThread is a package-private AWT class which takes
  39  * events off the EventQueue and dispatches them to the appropriate
  40  * AWT components.
  41  *
  42  * The Thread starts a "permanent" event pump with a call to
  43  * pumpEvents(Conditional) in its run() method. Event handlers can choose to
  44  * block this event pump at any time, but should start a new pump (<b>not</b>
  45  * a new EventDispatchThread) by again calling pumpEvents(Conditional). This
  46  * secondary event pump will exit automatically as soon as the Condtional
  47  * evaluate()s to false and an additional Event is pumped and dispatched.
  48  *
  49  * @author Tom Ball
  50  * @author Amy Fowler
  51  * @author Fred Ecks
  52  * @author David Mendenhall
  53  *
  54  * @since 1.1
  55  */


 140                     eventFilters.add(filter);
 141                 }
 142             }
 143         }
 144     }
 145 
 146     void removeEventFilter(EventFilter filter) {
 147         if (eventLog.isLoggable(PlatformLogger.Level.FINEST)) {
 148             eventLog.finest("removing the event filter: " + filter);
 149         }
 150         synchronized (eventFilters) {
 151             eventFilters.remove(filter);
 152         }
 153     }
 154 
 155     void pumpOneEventForFilters(int id) {
 156         AWTEvent event = null;
 157         boolean eventOK = false;
 158         try {
 159             EventQueue eq = null;

 160             do {
 161                 // EventQueue may change during the dispatching
 162                 eq = getEventQueue();

 163 



 164                 event = (id == ANY_EVENT) ? eq.getNextEvent() : eq.getNextEvent(id);

 165 
 166                 eventOK = true;
 167                 synchronized (eventFilters) {
 168                     for (int i = eventFilters.size() - 1; i >= 0; i--) {
 169                         EventFilter f = eventFilters.get(i);
 170                         EventFilter.FilterAction accept = f.acceptEvent(event);
 171                         if (accept == EventFilter.FilterAction.REJECT) {
 172                             eventOK = false;
 173                             break;
 174                         } else if (accept == EventFilter.FilterAction.ACCEPT_IMMEDIATELY) {
 175                             break;
 176                         }
 177                     }
 178                 }
 179                 eventOK = eventOK && SunDragSourceContextPeer.checkEvent(event);
 180                 if (!eventOK) {
 181                     event.consume();
 182                 }
 183             }
 184             while (eventOK == false);
 185 
 186             if (eventLog.isLoggable(PlatformLogger.Level.FINEST)) {
 187                 eventLog.finest("Dispatching: " + event);
 188             }
 189 




 190             eq.dispatchEvent(event);



 191         }
 192         catch (ThreadDeath death) {
 193             doDispatch = false;
 194             throw death;
 195         }
 196         catch (InterruptedException interruptedException) {
 197             doDispatch = false; // AppContext.dispose() interrupts all
 198                                 // Threads in the AppContext
 199         }
 200         catch (Throwable e) {
 201             processException(e);
 202         }
 203     }
 204 
 205     private void processException(Throwable e) {
 206         if (eventLog.isLoggable(PlatformLogger.Level.FINE)) {
 207             eventLog.fine("Processing exception: " + e);
 208         }
 209         getUncaughtExceptionHandler().uncaughtException(this, e);
 210     }