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