jdk/src/share/classes/java/awt/EventQueue.java
author dav
Mon, 07 Jul 2008 16:09:39 +0400
changeset 1181 c5971dbeaaaa
parent 2 90ce3da70b43
child 1247 b4c26443dee5
permissions -rw-r--r--
6693974: Unify EventQueue$EventQueueItem and SunToolkit.$EventQueueItem classes Reviewed-by: volk, art
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1996-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.peer.ComponentPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.reflect.InvocationTargetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.EmptyStackException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.logging.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.awt.AppContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.awt.AWTAutoShutdown;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.awt.PeerEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.awt.SunToolkit;
1181
c5971dbeaaaa 6693974: Unify EventQueue$EventQueueItem and SunToolkit.$EventQueueItem classes
dav
parents: 2
diff changeset
    45
import sun.awt.EventQueueItem;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <code>EventQueue</code> is a platform-independent class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * that queues events, both from the underlying peer classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * and from trusted application classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * It encapsulates asynchronous event dispatch machinery which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * extracts events from the queue and dispatches them by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * {@link #dispatchEvent(AWTEvent) dispatchEvent(AWTEvent)} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * on this <code>EventQueue</code> with the event to be dispatched
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * as an argument.  The particular behavior of this machinery is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * implementation-dependent.  The only requirements are that events
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * which were actually enqueued to this queue (note that events
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * being posted to the <code>EventQueue</code> can be coalesced)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * are dispatched:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *   <dt> Sequentially.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *   <dd> That is, it is not permitted that several events from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *        this queue are dispatched simultaneously.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *   <dt> In the same order as they are enqueued.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *   <dd> That is, if <code>AWTEvent</code>&nbsp;A is enqueued
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *        to the <code>EventQueue</code> before
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *        <code>AWTEvent</code>&nbsp;B then event B will not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *        dispatched before event A.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * Some browsers partition applets in different code bases into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * separate contexts, and establish walls between these contexts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * In such a scenario, there will be one <code>EventQueue</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * per context. Other browsers place all applets into the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * context, implying that there will be only a single, global
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <code>EventQueue</code> for all applets. This behavior is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * implementation-dependent.  Consult your browser's documentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * For information on the threading issues of the event dispatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * machinery, see <a href="doc-files/AWTThreadIssues.html#Autoshutdown"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * >AWT Threading Issues</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * @author Thomas Ball
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * @author Fred Ecks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * @author David Mendenhall
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * @since       1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
public class EventQueue {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    // From Thread.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private static int threadInitNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private static synchronized int nextThreadNum() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        return threadInitNumber++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private static final int LOW_PRIORITY = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private static final int NORM_PRIORITY = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private static final int HIGH_PRIORITY = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private static final int ULTIMATE_PRIORITY = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private static final int NUM_PRIORITIES = ULTIMATE_PRIORITY + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * We maintain one Queue for each priority that the EventQueue supports.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * That is, the EventQueue object is actually implemented as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * NUM_PRIORITIES queues and all Events on a particular internal Queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * have identical priority. Events are pulled off the EventQueue starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * with the Queue of highest priority. We progress in decreasing order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * across all Queues.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private Queue[] queues = new Queue[NUM_PRIORITIES];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * The next EventQueue on the stack, or null if this EventQueue is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * on the top of the stack.  If nextQueue is non-null, requests to post
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * an event are forwarded to nextQueue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private EventQueue nextQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * The previous EventQueue on the stack, or null if this is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * "base" EventQueue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private EventQueue previousQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private EventDispatchThread dispatchThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private final ThreadGroup threadGroup =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        Thread.currentThread().getThreadGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private final ClassLoader classLoader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        Thread.currentThread().getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * The time stamp of the last dispatched InputEvent or ActionEvent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private long mostRecentEventTime = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * The modifiers field of the current event, if the current event is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * InputEvent or ActionEvent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private WeakReference currentEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Non-zero if a thread is waiting in getNextEvent(int) for an event of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * a particular ID to be posted to the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private int waitForID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private final String name = "AWT-EventQueue-" + nextThreadNum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private static final Logger eventLog = Logger.getLogger("java.awt.event.EventQueue");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public EventQueue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        for (int i = 0; i < NUM_PRIORITIES; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            queues[i] = new Queue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
         * NOTE: if you ever have to start the associated event dispatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
         * thread at this point, be aware of the following problem:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
         * If this EventQueue instance is created in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
         * SunToolkit.createNewAppContext() the started dispatch thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
         * may call AppContext.getAppContext() before createNewAppContext()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
         * completes thus causing mess in thread group to appcontext mapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Posts a 1.1-style event to the <code>EventQueue</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * If there is an existing event on the queue with the same ID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * and event source, the source <code>Component</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * <code>coalesceEvents</code> method will be called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param theEvent an instance of <code>java.awt.AWTEvent</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *          or a subclass of it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @throws NullPointerException if <code>theEvent</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public void postEvent(AWTEvent theEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        SunToolkit.flushPendingEvents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        postEventPrivate(theEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Posts a 1.1-style event to the <code>EventQueue</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * If there is an existing event on the queue with the same ID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * and event source, the source <code>Component</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * <code>coalesceEvents</code> method will be called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param theEvent an instance of <code>java.awt.AWTEvent</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *          or a subclass of it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    final void postEventPrivate(AWTEvent theEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        theEvent.isPosted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            if (dispatchThread == null && nextQueue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                if (theEvent.getSource() == AWTAutoShutdown.getInstance()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    initDispatchThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            if (nextQueue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                // Forward event to top of EventQueue stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                nextQueue.postEventPrivate(theEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            postEvent(theEvent, getPriority(theEvent));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    private static int getPriority(AWTEvent theEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (theEvent instanceof PeerEvent &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            (((PeerEvent)theEvent).getFlags() &
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                PeerEvent.ULTIMATE_PRIORITY_EVENT) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            return ULTIMATE_PRIORITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if (theEvent instanceof PeerEvent &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            (((PeerEvent)theEvent).getFlags() &
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                PeerEvent.PRIORITY_EVENT) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            return HIGH_PRIORITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (theEvent instanceof PeerEvent &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            (((PeerEvent)theEvent).getFlags() &
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                PeerEvent.LOW_PRIORITY_EVENT) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            return LOW_PRIORITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        int id = theEvent.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (id == PaintEvent.PAINT || id == PaintEvent.UPDATE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            return LOW_PRIORITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        return NORM_PRIORITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * Posts the event to the internal Queue of specified priority,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * coalescing as appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @param theEvent an instance of <code>java.awt.AWTEvent</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *          or a subclass of it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @param priority  the desired priority of the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    private void postEvent(AWTEvent theEvent, int priority) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (coalesceEvent(theEvent, priority)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        EventQueueItem newItem = new EventQueueItem(theEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        cacheEQItem(newItem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        boolean notifyID = (theEvent.getID() == this.waitForID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if (queues[priority].head == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            boolean shouldNotify = noEvents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            queues[priority].head = queues[priority].tail = newItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            if (shouldNotify) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                if (theEvent.getSource() != AWTAutoShutdown.getInstance()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    AWTAutoShutdown.getInstance().notifyThreadBusy(dispatchThread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            } else if (notifyID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            // The event was not coalesced or has non-Component source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            // Insert it at the end of the appropriate Queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            queues[priority].tail.next = newItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            queues[priority].tail = newItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            if (notifyID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    private boolean coalescePaintEvent(PaintEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        ComponentPeer sourcePeer = ((Component)e.getSource()).peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if (sourcePeer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            sourcePeer.coalescePaintEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        EventQueueItem[] cache = ((Component)e.getSource()).eventCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if (cache == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        int index = eventToCacheIndex(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if (index != -1 && cache[index] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            PaintEvent merged = mergePaintEvents(e, (PaintEvent)cache[index].event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            if (merged != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                cache[index].event = merged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    private PaintEvent mergePaintEvents(PaintEvent a, PaintEvent b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        Rectangle aRect = a.getUpdateRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        Rectangle bRect = b.getUpdateRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (bRect.contains(aRect)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        if (aRect.contains(bRect)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    private boolean coalesceMouseEvent(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        EventQueueItem[] cache = ((Component)e.getSource()).eventCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        if (cache == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        int index = eventToCacheIndex(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if (index != -1 && cache[index] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            cache[index].event = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    private boolean coalescePeerEvent(PeerEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        EventQueueItem[] cache = ((Component)e.getSource()).eventCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        if (cache == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        int index = eventToCacheIndex(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        if (index != -1 && cache[index] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            e = e.coalesceEvents((PeerEvent)cache[index].event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            if (e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                cache[index].event = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                cache[index] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * Should avoid of calling this method by any means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * as it's working time is dependant on EQ length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * In the wors case this method alone can slow down the entire application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * 10 times by stalling the Event processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * Only here by backward compatibility reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    private boolean coalesceOtherEvent(AWTEvent e, int priority) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        int id = e.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        Component source = (Component)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        for (EventQueueItem entry = queues[priority].head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            entry != null; entry = entry.next)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            // Give Component.coalesceEvents a chance
1181
c5971dbeaaaa 6693974: Unify EventQueue$EventQueueItem and SunToolkit.$EventQueueItem classes
dav
parents: 2
diff changeset
   363
            if (entry.event.getSource() == source && entry.event.getID() == id) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                AWTEvent coalescedEvent = source.coalesceEvents(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    entry.event, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                if (coalescedEvent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    entry.event = coalescedEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    private boolean coalesceEvent(AWTEvent e, int priority) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        if (!(e.getSource() instanceof Component)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        if (e instanceof PeerEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            return coalescePeerEvent((PeerEvent)e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        // The worst case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        if (((Component)e.getSource()).isCoalescingEnabled()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            && coalesceOtherEvent(e, priority))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        if (e instanceof PaintEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            return coalescePaintEvent((PaintEvent)e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        if (e instanceof MouseEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            return coalesceMouseEvent((MouseEvent)e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    private void cacheEQItem(EventQueueItem entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        int index = eventToCacheIndex(entry.event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        if (index != -1 && entry.event.getSource() instanceof Component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            Component source = (Component)entry.event.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            if (source.eventCache == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                source.eventCache = new EventQueueItem[CACHE_LENGTH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            source.eventCache[index] = entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    private void uncacheEQItem(EventQueueItem entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        int index = eventToCacheIndex(entry.event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        if (index != -1 && entry.event.getSource() instanceof Component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            Component source = (Component)entry.event.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            if (source.eventCache == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            source.eventCache[index] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    private static final int PAINT = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    private static final int UPDATE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    private static final int MOVE = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    private static final int DRAG = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    private static final int PEER = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    private static final int CACHE_LENGTH = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    private static int eventToCacheIndex(AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        switch(e.getID()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        case PaintEvent.PAINT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            return PAINT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        case PaintEvent.UPDATE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            return UPDATE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        case MouseEvent.MOUSE_MOVED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            return MOVE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        case MouseEvent.MOUSE_DRAGGED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            return DRAG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            return e instanceof PeerEvent ? PEER : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * Returns whether an event is pending on any of the separate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * Queues.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * @return whether an event is pending on any of the separate Queues
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    private boolean noEvents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        for (int i = 0; i < NUM_PRIORITIES; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            if (queues[i].head != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * Removes an event from the <code>EventQueue</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * returns it.  This method will block until an event has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * been posted by another thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @return the next <code>AWTEvent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * @exception InterruptedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     *            if any thread has interrupted this thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    public AWTEvent getNextEvent() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
             * SunToolkit.flushPendingEvents must be called outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
             * of the synchronized block to avoid deadlock when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
             * event queues are nested with push()/pop().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            SunToolkit.flushPendingEvents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                for (int i = NUM_PRIORITIES - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    if (queues[i].head != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                        EventQueueItem entry = queues[i].head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                        queues[i].head = entry.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                        if (entry.next == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                            queues[i].tail = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                        uncacheEQItem(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                        return entry.event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                AWTAutoShutdown.getInstance().notifyThreadFree(dispatchThread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        } while(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    AWTEvent getNextEvent(int id) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
             * SunToolkit.flushPendingEvents must be called outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
             * of the synchronized block to avoid deadlock when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
             * event queues are nested with push()/pop().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            SunToolkit.flushPendingEvents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                for (int i = 0; i < NUM_PRIORITIES; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                    for (EventQueueItem entry = queues[i].head, prev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                         entry != null; prev = entry, entry = entry.next)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                    {
1181
c5971dbeaaaa 6693974: Unify EventQueue$EventQueueItem and SunToolkit.$EventQueueItem classes
dav
parents: 2
diff changeset
   503
                        if (entry.event.getID() == id) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                            if (prev == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                                queues[i].head = entry.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                                prev.next = entry.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                            if (queues[i].tail == entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                                queues[i].tail = prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                            uncacheEQItem(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                            return entry.event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                this.waitForID = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                this.waitForID = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        } while(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * Returns the first event on the <code>EventQueue</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * without removing it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * @return the first event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    public synchronized AWTEvent peekEvent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        for (int i = NUM_PRIORITIES - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            if (queues[i].head != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                return queues[i].head.event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * Returns the first event with the specified id, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * @param id the id of the type of event desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * @return the first event of the specified id or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     *    if there is no such event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    public synchronized AWTEvent peekEvent(int id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        for (int i = NUM_PRIORITIES - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            EventQueueItem q = queues[i].head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            for (; q != null; q = q.next) {
1181
c5971dbeaaaa 6693974: Unify EventQueue$EventQueueItem and SunToolkit.$EventQueueItem classes
dav
parents: 2
diff changeset
   549
                if (q.event.getID() == id) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                    return q.event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * Dispatches an event. The manner in which the event is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * dispatched depends upon the type of the event and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * type of the event's source object:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * <p> </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * <table border=1 summary="Event types, source types, and dispatch methods">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     *     <th>Event Type</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *     <th>Source Type</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     *     <th>Dispatched To</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     *     <td>ActiveEvent</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *     <td>Any</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     *     <td>event.dispatch()</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     *     <td>Other</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     *     <td>Component</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     *     <td>source.dispatchEvent(AWTEvent)</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     *     <td>Other</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *     <td>MenuComponent</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     *     <td>source.dispatchEvent(AWTEvent)</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *     <td>Other</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     *     <td>Other</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *     <td>No action (ignored)</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * <p> </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * @param event an instance of <code>java.awt.AWTEvent</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     *          or a subclass of it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * @throws NullPointerException if <code>event</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * @since           1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    protected void dispatchEvent(AWTEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        event.isPosted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        Object src = event.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        if (event instanceof ActiveEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            // This could become the sole method of dispatching in time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            setCurrentEventAndMostRecentTimeImpl(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            ((ActiveEvent)event).dispatch();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        } else if (src instanceof Component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            ((Component)src).dispatchEvent(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            event.dispatched();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        } else if (src instanceof MenuComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            ((MenuComponent)src).dispatchEvent(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        } else if (src instanceof TrayIcon) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            ((TrayIcon)src).dispatchEvent(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        } else if (src instanceof AWTAutoShutdown) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            if (noEvents()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                dispatchThread.stopDispatching();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            System.err.println("unable to dispatch event: " + event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * Returns the timestamp of the most recent event that had a timestamp, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * that was dispatched from the <code>EventQueue</code> associated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * calling thread. If an event with a timestamp is currently being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * dispatched, its timestamp will be returned. If no events have yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * been dispatched, the EventQueue's initialization time will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * returned instead.In the current version of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * the JDK, only <code>InputEvent</code>s,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * <code>ActionEvent</code>s, and <code>InvocationEvent</code>s have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * timestamps; however, future versions of the JDK may add timestamps to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * additional event types. Note that this method should only be invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * from an application's {@link #isDispatchThread event dispatching thread}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * If this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * invoked from another thread, the current system time (as reported by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * <code>System.currentTimeMillis()</code>) will be returned instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * @return the timestamp of the last <code>InputEvent</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     *         <code>ActionEvent</code>, or <code>InvocationEvent</code> to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *         dispatched, or <code>System.currentTimeMillis()</code> if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     *         method is invoked on a thread other than an event dispatching
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     *         thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * @see java.awt.event.InputEvent#getWhen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * @see java.awt.event.ActionEvent#getWhen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * @see java.awt.event.InvocationEvent#getWhen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * @see #isDispatchThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    public static long getMostRecentEventTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        return Toolkit.getEventQueue().getMostRecentEventTimeImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    private synchronized long getMostRecentEventTimeImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        return (Thread.currentThread() == dispatchThread)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            ? mostRecentEventTime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            : System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * @return most recent event time on all threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    synchronized long getMostRecentEventTimeEx() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        return mostRecentEventTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * Returns the the event currently being dispatched by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * <code>EventQueue</code> associated with the calling thread. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * useful if a method needs access to the event, but was not designed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * receive a reference to it as an argument. Note that this method should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * only be invoked from an application's event dispatching thread. If this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * method is invoked from another thread, null will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * @return the event currently being dispatched, or null if this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     *         invoked on a thread other than an event dispatching thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    public static AWTEvent getCurrentEvent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        return Toolkit.getEventQueue().getCurrentEventImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    private synchronized AWTEvent getCurrentEventImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        return (Thread.currentThread() == dispatchThread)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            ? ((AWTEvent)currentEvent.get())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * Replaces the existing <code>EventQueue</code> with the specified one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * Any pending events are transferred to the new <code>EventQueue</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * for processing by it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * @param newEventQueue an <code>EventQueue</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     *          (or subclass thereof) instance to be use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * @see      java.awt.EventQueue#pop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * @throws NullPointerException if <code>newEventQueue</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * @since           1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    public synchronized void push(EventQueue newEventQueue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        if (eventLog.isLoggable(Level.FINE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            eventLog.log(Level.FINE, "EventQueue.push(" + newEventQueue + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        if (nextQueue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            nextQueue.push(newEventQueue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        synchronized (newEventQueue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            // Transfer all events forward to new EventQueue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            while (peekEvent() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                    newEventQueue.postEventPrivate(getNextEvent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                    if (eventLog.isLoggable(Level.FINE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                        eventLog.log(Level.FINE, "Interrupted push", ie);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            newEventQueue.previousQueue = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
         * Stop the event dispatch thread associated with the currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
         * active event queue, so that after the new queue is pushed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
         * on the top this event dispatch thread won't prevent AWT from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
         * being automatically shut down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
         * Use stopDispatchingLater() to avoid deadlock: stopDispatching()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
         * waits for the dispatch thread to exit, so if the dispatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
         * thread attempts to synchronize on this EventQueue object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
         * it will never exit since we already hold this lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        if (dispatchThread != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            dispatchThread.stopDispatchingLater();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        nextQueue = newEventQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        AppContext appContext = AppContext.getAppContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        if (appContext.get(AppContext.EVENT_QUEUE_KEY) == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            appContext.put(AppContext.EVENT_QUEUE_KEY, newEventQueue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * Stops dispatching events using this <code>EventQueue</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * Any pending events are transferred to the previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * <code>EventQueue</code> for processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * Warning: To avoid deadlock, do not declare this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * synchronized in a subclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * @exception EmptyStackException if no previous push was made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     *  on this <code>EventQueue</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * @see      java.awt.EventQueue#push
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * @since           1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    protected void pop() throws EmptyStackException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        if (eventLog.isLoggable(Level.FINE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            eventLog.log(Level.FINE, "EventQueue.pop(" + this + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        // To prevent deadlock, we lock on the previous EventQueue before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        // this one.  This uses the same locking order as everything else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        // in EventQueue.java, so deadlock isn't possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        EventQueue prev = previousQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        synchronized ((prev != null) ? prev : this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
          synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            if (nextQueue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                nextQueue.pop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            if (previousQueue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                throw new EmptyStackException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            // Transfer all events back to previous EventQueue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            previousQueue.nextQueue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            while (peekEvent() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                    previousQueue.postEventPrivate(getNextEvent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                    if (eventLog.isLoggable(Level.FINE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                        eventLog.log(Level.FINE, "Interrupted pop", ie);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            AppContext appContext = AppContext.getAppContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            if (appContext.get(AppContext.EVENT_QUEUE_KEY) == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                appContext.put(AppContext.EVENT_QUEUE_KEY, previousQueue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            previousQueue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        EventDispatchThread dt = this.dispatchThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        if (dt != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            dt.stopDispatching(); // Must be done outside synchronized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                                  // block to avoid possible deadlock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * Returns true if the calling thread is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * {@link Toolkit#getSystemEventQueue the current AWT EventQueue}'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * dispatch thread. Use this method to ensure that a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * task is being executed (or not being) there.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * Note: use the {@link #invokeLater} or {@link #invokeAndWait}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * methods to execute a task in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * {@link Toolkit#getSystemEventQueue the current AWT EventQueue}'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * dispatch thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * @return true if running in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * {@link Toolkit#getSystemEventQueue the current AWT EventQueue}'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * dispatch thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * @see             #invokeLater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * @see             #invokeAndWait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * @see             Toolkit#getSystemEventQueue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * @since           1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    public static boolean isDispatchThread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        EventQueue eq = Toolkit.getEventQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        EventQueue next = eq.nextQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        while (next != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            eq = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            next = eq.nextQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        return (Thread.currentThread() == eq.dispatchThread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    final void initDispatchThread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            if (dispatchThread == null && !threadGroup.isDestroyed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                dispatchThread = (EventDispatchThread)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                    AccessController.doPrivileged(new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                        public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                            EventDispatchThread t =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                                new EventDispatchThread(threadGroup,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                                                        name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                                                        EventQueue.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                            t.setContextClassLoader(classLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                            t.setPriority(Thread.NORM_PRIORITY + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                            t.setDaemon(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                            return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                AWTAutoShutdown.getInstance().notifyThreadBusy(dispatchThread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                dispatchThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    final void detachDispatchThread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        dispatchThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * Gets the <code>EventDispatchThread</code> for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * <code>EventQueue</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * @return the event dispatch thread associated with this event queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     *         or <code>null</code> if this event queue doesn't have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     *         working thread associated with it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * @see    java.awt.EventQueue#initDispatchThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * @see    java.awt.EventQueue#detachDispatchThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    final EventDispatchThread getDispatchThread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        return dispatchThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * Removes any pending events for the specified source object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * If removeAllEvents parameter is <code>true</code> then all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * events for the specified source object are removed, if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * is <code>false</code> then <code>SequencedEvent</code>, <code>SentEvent</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * <code>FocusEvent</code>, <code>WindowEvent</code>, <code>KeyEvent</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * and <code>InputMethodEvent</code> are kept in the queue, but all other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * events are removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * This method is normally called by the source's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * <code>removeNotify</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    final void removeSourceEvents(Object source, boolean removeAllEvents) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        SunToolkit.flushPendingEvents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            for (int i = 0; i < NUM_PRIORITIES; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                EventQueueItem entry = queues[i].head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                EventQueueItem prev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                while (entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                    if ((entry.event.getSource() == source)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                        && (removeAllEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                            || ! (entry.event instanceof SequencedEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                                  || entry.event instanceof SentEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                                  || entry.event instanceof FocusEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                                  || entry.event instanceof WindowEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                                  || entry.event instanceof KeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                                  || entry.event instanceof InputMethodEvent)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                        if (entry.event instanceof SequencedEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                            ((SequencedEvent)entry.event).dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                        if (entry.event instanceof SentEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                            ((SentEvent)entry.event).dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                        if (prev == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                            queues[i].head = entry.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                            prev.next = entry.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                        uncacheEQItem(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                        prev = entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                    entry = entry.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                queues[i].tail = prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    static void setCurrentEventAndMostRecentTime(AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        Toolkit.getEventQueue().setCurrentEventAndMostRecentTimeImpl(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    private synchronized void setCurrentEventAndMostRecentTimeImpl(AWTEvent e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        if (Thread.currentThread() != dispatchThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        currentEvent = new WeakReference(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        // This series of 'instanceof' checks should be replaced with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        // polymorphic type (for example, an interface which declares a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        // getWhen() method). However, this would require us to make such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        // a type public, or to place it in sun.awt. Both of these approaches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        // have been frowned upon. So for now, we hack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        // In tiger, we will probably give timestamps to all events, so this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        // will no longer be an issue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        long mostRecentEventTime2 = Long.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        if (e instanceof InputEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            InputEvent ie = (InputEvent)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            mostRecentEventTime2 = ie.getWhen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        } else if (e instanceof InputMethodEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            InputMethodEvent ime = (InputMethodEvent)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            mostRecentEventTime2 = ime.getWhen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        } else if (e instanceof ActionEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            ActionEvent ae = (ActionEvent)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            mostRecentEventTime2 = ae.getWhen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        } else if (e instanceof InvocationEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            InvocationEvent ie = (InvocationEvent)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            mostRecentEventTime2 = ie.getWhen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        mostRecentEventTime = Math.max(mostRecentEventTime, mostRecentEventTime2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * Causes <code>runnable</code> to have its <code>run</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * method called in the {@link #isDispatchThread dispatch thread} of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * {@link Toolkit#getSystemEventQueue the system EventQueue}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * This will happen after all pending events are processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * @param runnable  the <code>Runnable</code> whose <code>run</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     *                  method should be executed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     *                  asynchronously in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     *                  {@link #isDispatchThread event dispatch thread}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     *                  of {@link Toolkit#getSystemEventQueue the system EventQueue}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * @see             #invokeAndWait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * @see             Toolkit#getSystemEventQueue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * @see             #isDispatchThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * @since           1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    public static void invokeLater(Runnable runnable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        Toolkit.getEventQueue().postEvent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            new InvocationEvent(Toolkit.getDefaultToolkit(), runnable));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * Causes <code>runnable</code> to have its <code>run</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * method called in the {@link #isDispatchThread dispatch thread} of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * {@link Toolkit#getSystemEventQueue the system EventQueue}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * This will happen after all pending events are processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * The call blocks until this has happened.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * will throw an Error if called from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * {@link #isDispatchThread event dispatcher thread}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * @param runnable  the <code>Runnable</code> whose <code>run</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     *                  method should be executed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     *                  synchronously in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     *                  {@link #isDispatchThread event dispatch thread}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     *                  of {@link Toolkit#getSystemEventQueue the system EventQueue}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * @exception       InterruptedException  if any thread has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     *                  interrupted this thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * @exception       InvocationTargetException  if an throwable is thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     *                  when running <code>runnable</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     * @see             #invokeLater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * @see             Toolkit#getSystemEventQueue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * @see             #isDispatchThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * @since           1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    public static void invokeAndWait(Runnable runnable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
             throws InterruptedException, InvocationTargetException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        if (EventQueue.isDispatchThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            throw new Error("Cannot call invokeAndWait from the event dispatcher thread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        class AWTInvocationLock {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        Object lock = new AWTInvocationLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        InvocationEvent event =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            new InvocationEvent(Toolkit.getDefaultToolkit(), runnable, lock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                                true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            Toolkit.getEventQueue().postEvent(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            lock.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        Throwable eventThrowable = event.getThrowable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        if (eventThrowable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            throw new InvocationTargetException(eventThrowable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * Called from PostEventQueue.postEvent to notify that a new event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * appeared. First it proceeds to the EventQueue on the top of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * stack, then notifies the associated dispatch thread if it exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * or starts a new one otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    private void wakeup(boolean isShutdown) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            if (nextQueue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                // Forward call to the top of EventQueue stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                nextQueue.wakeup(isShutdown);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            } else if (dispatchThread != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            } else if (!isShutdown) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                initDispatchThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
 * The Queue object holds pointers to the beginning and end of one internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
 * queue. An EventQueue object is composed of multiple internal Queues, one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
 * for each priority supported by the EventQueue. All Events on a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
 * internal Queue have identical priority.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
class Queue {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    EventQueueItem head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    EventQueueItem tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
}