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

Print this page




 154 
 155     private final ThreadGroup threadGroup =
 156         Thread.currentThread().getThreadGroup();
 157     private final ClassLoader classLoader =
 158         Thread.currentThread().getContextClassLoader();
 159 
 160     /*
 161      * The time stamp of the last dispatched InputEvent or ActionEvent.
 162      */
 163     private long mostRecentEventTime = System.currentTimeMillis();
 164 
 165     /*
 166      * The time stamp of the last KeyEvent .
 167      */
 168     private long mostRecentKeyEventTime = System.currentTimeMillis();
 169 
 170     /**
 171      * The modifiers field of the current event, if the current event is an
 172      * InputEvent or ActionEvent.
 173      */
 174     private WeakReference currentEvent;
 175 
 176     /*
 177      * Non-zero if a thread is waiting in getNextEvent(int) for an event of
 178      * a particular ID to be posted to the queue.
 179      */
 180     private volatile int waitForID;
 181 
 182     private final String name = "AWT-EventQueue-" + threadInitNumber.getAndIncrement();
 183 
 184     private static final PlatformLogger eventLog = PlatformLogger.getLogger("java.awt.event.EventQueue");
 185 
 186     static {
 187         AWTAccessor.setEventQueueAccessor(
 188             new AWTAccessor.EventQueueAccessor() {
 189                 public Thread getDispatchThread(EventQueue eventQueue) {
 190                     return eventQueue.getDispatchThread();
 191                 }
 192                 public boolean isDispatchThreadImpl(EventQueue eventQueue) {
 193                     return eventQueue.isDispatchThreadImpl();
 194                 }


 792 
 793     /**
 794      * Returns the the event currently being dispatched by the
 795      * <code>EventQueue</code> associated with the calling thread. This is
 796      * useful if a method needs access to the event, but was not designed to
 797      * receive a reference to it as an argument. Note that this method should
 798      * only be invoked from an application's event dispatching thread. If this
 799      * method is invoked from another thread, null will be returned.
 800      *
 801      * @return the event currently being dispatched, or null if this method is
 802      *         invoked on a thread other than an event dispatching thread
 803      * @since 1.4
 804      */
 805     public static AWTEvent getCurrentEvent() {
 806         return Toolkit.getEventQueue().getCurrentEventImpl();
 807     }
 808     private AWTEvent getCurrentEventImpl() {
 809         pushPopLock.lock();
 810         try {
 811                 return (Thread.currentThread() == dispatchThread)
 812                 ? ((AWTEvent)currentEvent.get())
 813                 : null;
 814         } finally {
 815             pushPopLock.unlock();
 816         }
 817     }
 818 
 819     /**
 820      * Replaces the existing <code>EventQueue</code> with the specified one.
 821      * Any pending events are transferred to the new <code>EventQueue</code>
 822      * for processing by it.
 823      *
 824      * @param newEventQueue an <code>EventQueue</code>
 825      *          (or subclass thereof) instance to be use
 826      * @see      java.awt.EventQueue#pop
 827      * @throws NullPointerException if <code>newEventQueue</code> is <code>null</code>
 828      * @since           1.2
 829      */
 830     public void push(EventQueue newEventQueue) {
 831         if (eventLog.isLoggable(PlatformLogger.FINE)) {
 832             eventLog.fine("EventQueue.push(" + newEventQueue + ")");


1150 
1151     synchronized long getMostRecentKeyEventTime() {
1152         pushPopLock.lock();
1153         try {
1154             return mostRecentKeyEventTime;
1155         } finally {
1156             pushPopLock.unlock();
1157         }
1158     }
1159 
1160     static void setCurrentEventAndMostRecentTime(AWTEvent e) {
1161         Toolkit.getEventQueue().setCurrentEventAndMostRecentTimeImpl(e);
1162     }
1163     private void setCurrentEventAndMostRecentTimeImpl(AWTEvent e) {
1164         pushPopLock.lock();
1165         try {
1166             if (Thread.currentThread() != dispatchThread) {
1167                 return;
1168             }
1169 
1170             currentEvent = new WeakReference(e);
1171 
1172             // This series of 'instanceof' checks should be replaced with a
1173             // polymorphic type (for example, an interface which declares a
1174             // getWhen() method). However, this would require us to make such
1175             // a type public, or to place it in sun.awt. Both of these approaches
1176             // have been frowned upon. So for now, we hack.
1177             //
1178             // In tiger, we will probably give timestamps to all events, so this
1179             // will no longer be an issue.
1180             long mostRecentEventTime2 = Long.MIN_VALUE;
1181             if (e instanceof InputEvent) {
1182                 InputEvent ie = (InputEvent)e;
1183                 mostRecentEventTime2 = ie.getWhen();
1184                 if (e instanceof KeyEvent) {
1185                     mostRecentKeyEventTime = ie.getWhen();
1186                 }
1187             } else if (e instanceof InputMethodEvent) {
1188                 InputMethodEvent ime = (InputMethodEvent)e;
1189                 mostRecentEventTime2 = ime.getWhen();
1190             } else if (e instanceof ActionEvent) {




 154 
 155     private final ThreadGroup threadGroup =
 156         Thread.currentThread().getThreadGroup();
 157     private final ClassLoader classLoader =
 158         Thread.currentThread().getContextClassLoader();
 159 
 160     /*
 161      * The time stamp of the last dispatched InputEvent or ActionEvent.
 162      */
 163     private long mostRecentEventTime = System.currentTimeMillis();
 164 
 165     /*
 166      * The time stamp of the last KeyEvent .
 167      */
 168     private long mostRecentKeyEventTime = System.currentTimeMillis();
 169 
 170     /**
 171      * The modifiers field of the current event, if the current event is an
 172      * InputEvent or ActionEvent.
 173      */
 174     private WeakReference<AWTEvent> currentEvent;
 175 
 176     /*
 177      * Non-zero if a thread is waiting in getNextEvent(int) for an event of
 178      * a particular ID to be posted to the queue.
 179      */
 180     private volatile int waitForID;
 181 
 182     private final String name = "AWT-EventQueue-" + threadInitNumber.getAndIncrement();
 183 
 184     private static final PlatformLogger eventLog = PlatformLogger.getLogger("java.awt.event.EventQueue");
 185 
 186     static {
 187         AWTAccessor.setEventQueueAccessor(
 188             new AWTAccessor.EventQueueAccessor() {
 189                 public Thread getDispatchThread(EventQueue eventQueue) {
 190                     return eventQueue.getDispatchThread();
 191                 }
 192                 public boolean isDispatchThreadImpl(EventQueue eventQueue) {
 193                     return eventQueue.isDispatchThreadImpl();
 194                 }


 792 
 793     /**
 794      * Returns the the event currently being dispatched by the
 795      * <code>EventQueue</code> associated with the calling thread. This is
 796      * useful if a method needs access to the event, but was not designed to
 797      * receive a reference to it as an argument. Note that this method should
 798      * only be invoked from an application's event dispatching thread. If this
 799      * method is invoked from another thread, null will be returned.
 800      *
 801      * @return the event currently being dispatched, or null if this method is
 802      *         invoked on a thread other than an event dispatching thread
 803      * @since 1.4
 804      */
 805     public static AWTEvent getCurrentEvent() {
 806         return Toolkit.getEventQueue().getCurrentEventImpl();
 807     }
 808     private AWTEvent getCurrentEventImpl() {
 809         pushPopLock.lock();
 810         try {
 811                 return (Thread.currentThread() == dispatchThread)
 812                 ? currentEvent.get()
 813                 : null;
 814         } finally {
 815             pushPopLock.unlock();
 816         }
 817     }
 818 
 819     /**
 820      * Replaces the existing <code>EventQueue</code> with the specified one.
 821      * Any pending events are transferred to the new <code>EventQueue</code>
 822      * for processing by it.
 823      *
 824      * @param newEventQueue an <code>EventQueue</code>
 825      *          (or subclass thereof) instance to be use
 826      * @see      java.awt.EventQueue#pop
 827      * @throws NullPointerException if <code>newEventQueue</code> is <code>null</code>
 828      * @since           1.2
 829      */
 830     public void push(EventQueue newEventQueue) {
 831         if (eventLog.isLoggable(PlatformLogger.FINE)) {
 832             eventLog.fine("EventQueue.push(" + newEventQueue + ")");


1150 
1151     synchronized long getMostRecentKeyEventTime() {
1152         pushPopLock.lock();
1153         try {
1154             return mostRecentKeyEventTime;
1155         } finally {
1156             pushPopLock.unlock();
1157         }
1158     }
1159 
1160     static void setCurrentEventAndMostRecentTime(AWTEvent e) {
1161         Toolkit.getEventQueue().setCurrentEventAndMostRecentTimeImpl(e);
1162     }
1163     private void setCurrentEventAndMostRecentTimeImpl(AWTEvent e) {
1164         pushPopLock.lock();
1165         try {
1166             if (Thread.currentThread() != dispatchThread) {
1167                 return;
1168             }
1169 
1170             currentEvent = new WeakReference<>(e);
1171 
1172             // This series of 'instanceof' checks should be replaced with a
1173             // polymorphic type (for example, an interface which declares a
1174             // getWhen() method). However, this would require us to make such
1175             // a type public, or to place it in sun.awt. Both of these approaches
1176             // have been frowned upon. So for now, we hack.
1177             //
1178             // In tiger, we will probably give timestamps to all events, so this
1179             // will no longer be an issue.
1180             long mostRecentEventTime2 = Long.MIN_VALUE;
1181             if (e instanceof InputEvent) {
1182                 InputEvent ie = (InputEvent)e;
1183                 mostRecentEventTime2 = ie.getWhen();
1184                 if (e instanceof KeyEvent) {
1185                     mostRecentKeyEventTime = ie.getWhen();
1186                 }
1187             } else if (e instanceof InputMethodEvent) {
1188                 InputMethodEvent ime = (InputMethodEvent)e;
1189                 mostRecentEventTime2 = ime.getWhen();
1190             } else if (e instanceof ActionEvent) {