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

Print this page




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





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


1111                             ((SentEvent)entry.event).dispose();
1112                         }
1113                         if (prev == null) {
1114                             queues[i].head = entry.next;
1115                         } else {
1116                             prev.next = entry.next;
1117                         }
1118                         uncacheEQItem(entry);
1119                     } else {
1120                         prev = entry;
1121                     }
1122                     entry = entry.next;
1123                 }
1124                 queues[i].tail = prev;
1125             }
1126         } finally {
1127             pushPopLock.unlock();
1128         }
1129     }
1130 









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



1155             } else if (e instanceof InputMethodEvent) {
1156                 InputMethodEvent ime = (InputMethodEvent)e;
1157                 mostRecentEventTime2 = ime.getWhen();
1158             } else if (e instanceof ActionEvent) {
1159                 ActionEvent ae = (ActionEvent)e;
1160                 mostRecentEventTime2 = ae.getWhen();
1161             } else if (e instanceof InvocationEvent) {
1162                 InvocationEvent ie = (InvocationEvent)e;
1163                 mostRecentEventTime2 = ie.getWhen();
1164             }
1165             mostRecentEventTime = Math.max(mostRecentEventTime, mostRecentEventTime2);
1166         } finally {
1167             pushPopLock.unlock();
1168         }
1169     }
1170 
1171     /**
1172      * Causes <code>runnable</code> to have its <code>run</code>
1173      * method called in the {@link #isDispatchThread dispatch thread} of
1174      * {@link Toolkit#getSystemEventQueue the system EventQueue}.




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


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