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