src/share/classes/java/awt/EventQueue.java

Print this page




 146      * Dummy runnable to wake up EDT from getNextEvent() after
 147      push/pop is performed
 148      */
 149     private final static Runnable dummyRunnable = new Runnable() {
 150         public void run() {
 151         }
 152     };
 153 
 154     private EventDispatchThread dispatchThread;
 155 
 156     private final ThreadGroup threadGroup =
 157         Thread.currentThread().getThreadGroup();
 158     private final ClassLoader classLoader =
 159         Thread.currentThread().getContextClassLoader();
 160 
 161     /*
 162      * The time stamp of the last dispatched InputEvent or ActionEvent.
 163      */
 164     private long mostRecentEventTime = System.currentTimeMillis();
 165 





 166     /**
 167      * The modifiers field of the current event, if the current event is an
 168      * InputEvent or ActionEvent.
 169      */
 170     private WeakReference currentEvent;
 171 
 172     /*
 173      * Non-zero if a thread is waiting in getNextEvent(int) for an event of
 174      * a particular ID to be posted to the queue.
 175      */
 176     private volatile int waitForID;
 177 
 178     private final String name = "AWT-EventQueue-" + threadInitNumber.getAndIncrement();
 179 
 180     private static final PlatformLogger eventLog = PlatformLogger.getLogger("java.awt.event.EventQueue");
 181 
 182     static {
 183         AWTAccessor.setEventQueueAccessor(
 184             new AWTAccessor.EventQueueAccessor() {
 185                 public Thread getDispatchThread(EventQueue eventQueue) {


1121                             ((SentEvent)entry.event).dispose();
1122                         }
1123                         if (prev == null) {
1124                             queues[i].head = entry.next;
1125                         } else {
1126                             prev.next = entry.next;
1127                         }
1128                         uncacheEQItem(entry);
1129                     } else {
1130                         prev = entry;
1131                     }
1132                     entry = entry.next;
1133                 }
1134                 queues[i].tail = prev;
1135             }
1136         } finally {
1137             pushPopLock.unlock();
1138         }
1139     }
1140 









1141     static void setCurrentEventAndMostRecentTime(AWTEvent e) {
1142         Toolkit.getEventQueue().setCurrentEventAndMostRecentTimeImpl(e);
1143     }
1144     private void setCurrentEventAndMostRecentTimeImpl(AWTEvent e) {
1145         pushPopLock.lock();
1146         try {
1147             if (Thread.currentThread() != dispatchThread) {
1148                 return;
1149             }
1150 
1151             currentEvent = new WeakReference(e);
1152 
1153             // This series of 'instanceof' checks should be replaced with a
1154             // polymorphic type (for example, an interface which declares a
1155             // getWhen() method). However, this would require us to make such
1156             // a type public, or to place it in sun.awt. Both of these approaches
1157             // have been frowned upon. So for now, we hack.
1158             //
1159             // In tiger, we will probably give timestamps to all events, so this
1160             // will no longer be an issue.
1161             long mostRecentEventTime2 = Long.MIN_VALUE;
1162             if (e instanceof InputEvent) {
1163                 InputEvent ie = (InputEvent)e;
1164                 mostRecentEventTime2 = ie.getWhen();



1165             } else if (e instanceof InputMethodEvent) {
1166                 InputMethodEvent ime = (InputMethodEvent)e;
1167                 mostRecentEventTime2 = ime.getWhen();
1168             } else if (e instanceof ActionEvent) {
1169                 ActionEvent ae = (ActionEvent)e;
1170                 mostRecentEventTime2 = ae.getWhen();
1171             } else if (e instanceof InvocationEvent) {
1172                 InvocationEvent ie = (InvocationEvent)e;
1173                 mostRecentEventTime2 = ie.getWhen();
1174             }
1175             mostRecentEventTime = Math.max(mostRecentEventTime, mostRecentEventTime2);
1176         } finally {
1177             pushPopLock.unlock();
1178         }
1179     }
1180 
1181     /**
1182      * Causes <code>runnable</code> to have its <code>run</code>
1183      * method called in the {@link #isDispatchThread dispatch thread} of
1184      * {@link Toolkit#getSystemEventQueue the system EventQueue}.




 146      * Dummy runnable to wake up EDT from getNextEvent() after
 147      push/pop is performed
 148      */
 149     private final static Runnable dummyRunnable = new Runnable() {
 150         public void run() {
 151         }
 152     };
 153 
 154     private EventDispatchThread dispatchThread;
 155 
 156     private final ThreadGroup threadGroup =
 157         Thread.currentThread().getThreadGroup();
 158     private final ClassLoader classLoader =
 159         Thread.currentThread().getContextClassLoader();
 160 
 161     /*
 162      * The time stamp of the last dispatched InputEvent or ActionEvent.
 163      */
 164     private long mostRecentEventTime = System.currentTimeMillis();
 165 
 166     /*
 167      * The time stamp of the last KeyEvent .
 168      */
 169     private long mostRecentKeyEventTime = System.currentTimeMillis();
 170 
 171     /**
 172      * The modifiers field of the current event, if the current event is an
 173      * InputEvent or ActionEvent.
 174      */
 175     private WeakReference currentEvent;
 176 
 177     /*
 178      * Non-zero if a thread is waiting in getNextEvent(int) for an event of
 179      * a particular ID to be posted to the queue.
 180      */
 181     private volatile int waitForID;
 182 
 183     private final String name = "AWT-EventQueue-" + threadInitNumber.getAndIncrement();
 184 
 185     private static final PlatformLogger eventLog = PlatformLogger.getLogger("java.awt.event.EventQueue");
 186 
 187     static {
 188         AWTAccessor.setEventQueueAccessor(
 189             new AWTAccessor.EventQueueAccessor() {
 190                 public Thread getDispatchThread(EventQueue eventQueue) {


1126                             ((SentEvent)entry.event).dispose();
1127                         }
1128                         if (prev == null) {
1129                             queues[i].head = entry.next;
1130                         } else {
1131                             prev.next = entry.next;
1132                         }
1133                         uncacheEQItem(entry);
1134                     } else {
1135                         prev = entry;
1136                     }
1137                     entry = entry.next;
1138                 }
1139                 queues[i].tail = prev;
1140             }
1141         } finally {
1142             pushPopLock.unlock();
1143         }
1144     }
1145 
1146     synchronized long getMostRecentKeyEventTime() {
1147         pushPopLock.lock();
1148         try {
1149             return mostRecentKeyEventTime;
1150         } finally {
1151             pushPopLock.unlock();
1152         }
1153     }
1154 
1155     static void setCurrentEventAndMostRecentTime(AWTEvent e) {
1156         Toolkit.getEventQueue().setCurrentEventAndMostRecentTimeImpl(e);
1157     }
1158     private void setCurrentEventAndMostRecentTimeImpl(AWTEvent e) {
1159         pushPopLock.lock();
1160         try {
1161             if (Thread.currentThread() != dispatchThread) {
1162                 return;
1163             }
1164 
1165             currentEvent = new WeakReference(e);
1166 
1167             // This series of 'instanceof' checks should be replaced with a
1168             // polymorphic type (for example, an interface which declares a
1169             // getWhen() method). However, this would require us to make such
1170             // a type public, or to place it in sun.awt. Both of these approaches
1171             // have been frowned upon. So for now, we hack.
1172             //
1173             // In tiger, we will probably give timestamps to all events, so this
1174             // will no longer be an issue.
1175             long mostRecentEventTime2 = Long.MIN_VALUE;
1176             if (e instanceof InputEvent) {
1177                 InputEvent ie = (InputEvent)e;
1178                 mostRecentEventTime2 = ie.getWhen();
1179                 if (e instanceof KeyEvent) {
1180                     mostRecentKeyEventTime = ie.getWhen();
1181                 }
1182             } else if (e instanceof InputMethodEvent) {
1183                 InputMethodEvent ime = (InputMethodEvent)e;
1184                 mostRecentEventTime2 = ime.getWhen();
1185             } else if (e instanceof ActionEvent) {
1186                 ActionEvent ae = (ActionEvent)e;
1187                 mostRecentEventTime2 = ae.getWhen();
1188             } else if (e instanceof InvocationEvent) {
1189                 InvocationEvent ie = (InvocationEvent)e;
1190                 mostRecentEventTime2 = ie.getWhen();
1191             }
1192             mostRecentEventTime = Math.max(mostRecentEventTime, mostRecentEventTime2);
1193         } finally {
1194             pushPopLock.unlock();
1195         }
1196     }
1197 
1198     /**
1199      * Causes <code>runnable</code> to have its <code>run</code>
1200      * method called in the {@link #isDispatchThread dispatch thread} of
1201      * {@link Toolkit#getSystemEventQueue the system EventQueue}.