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