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