src/java.desktop/share/classes/java/awt/SequencedEvent.java
author mbalao
Fri, 09 Nov 2018 10:34:19 -0300
changeset 52558 08a0bf1592bd
parent 49299 719064f540f3
child 58309 c6f8b2c3dc66
permissions -rw-r--r--
8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts Summary: Improvements on the synchronization of SequencedEvent events from different AppContexts Reviewed-by: serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20107
diff changeset
     2
 * Copyright (c) 2000, 2014, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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
47374
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
    28
import java.security.AccessController;
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
    29
import java.security.PrivilegedAction;
52558
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
    30
import java.util.Iterator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.LinkedList;
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 7668
diff changeset
    32
import sun.awt.AWTAccessor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.awt.AppContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.awt.SunToolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * A mechanism for ensuring that a series of AWTEvents are executed in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * precise order, even across multiple AppContexts. The nested events will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * dispatched in the order in which their wrapping SequencedEvents were
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * constructed. The only exception to this rule is if the peer of the target of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * the nested event was destroyed (with a call to Component.removeNotify)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * before the wrapping SequencedEvent was able to be dispatched. In this case,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * the nested event is never dispatched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author David Mendenhall
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
class SequencedEvent extends AWTEvent implements ActiveEvent {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final long serialVersionUID = 547742659238625067L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final int ID =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        java.awt.event.FocusEvent.FOCUS_LAST + 1;
20107
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 13652
diff changeset
    55
    private static final LinkedList<SequencedEvent> list = new LinkedList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private final AWTEvent nested;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private AppContext appContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private boolean disposed;
52558
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
    60
    private final LinkedList<AWTEvent> pendingEvents = new LinkedList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
47374
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
    62
    private static boolean fxAppThreadIsDispatchThread;
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
    63
    private Thread fxCheckSequenceThread;
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 7668
diff changeset
    64
    static {
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 7668
diff changeset
    65
        AWTAccessor.setSequencedEventAccessor(new AWTAccessor.SequencedEventAccessor() {
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 7668
diff changeset
    66
            public AWTEvent getNested(AWTEvent sequencedEvent) {
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 7668
diff changeset
    67
                return ((SequencedEvent)sequencedEvent).nested;
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 7668
diff changeset
    68
            }
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 7668
diff changeset
    69
            public boolean isSequencedEvent(AWTEvent event) {
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 7668
diff changeset
    70
                return event instanceof SequencedEvent;
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 7668
diff changeset
    71
            }
40719
4ae72a69bd3b 8129854: Remove reflection from AWT/Swing classes
alexsch
parents: 32865
diff changeset
    72
4ae72a69bd3b 8129854: Remove reflection from AWT/Swing classes
alexsch
parents: 32865
diff changeset
    73
            public AWTEvent create(AWTEvent event) {
4ae72a69bd3b 8129854: Remove reflection from AWT/Swing classes
alexsch
parents: 32865
diff changeset
    74
                return new SequencedEvent(event);
4ae72a69bd3b 8129854: Remove reflection from AWT/Swing classes
alexsch
parents: 32865
diff changeset
    75
            }
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 7668
diff changeset
    76
        });
47374
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
    77
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
    78
            public Object run() {
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
    79
                fxAppThreadIsDispatchThread =
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
    80
                        "true".equals(System.getProperty("javafx.embed.singleThread"));
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
    81
                return null;
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
    82
            }
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
    83
        });
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 7668
diff changeset
    84
    }
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 7668
diff changeset
    85
52558
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
    86
    private static final class SequencedEventsFilter implements EventFilter {
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
    87
        private final SequencedEvent currentSequencedEvent;
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
    88
        private SequencedEventsFilter(SequencedEvent currentSequencedEvent) {
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
    89
            this.currentSequencedEvent = currentSequencedEvent;
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
    90
        }
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
    91
        @Override
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
    92
        public FilterAction acceptEvent(AWTEvent ev) {
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
    93
            if (ev.getID() == ID) {
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
    94
                // Move forward dispatching only if the event is previous
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
    95
                // in SequencedEvent.list. Otherwise, hold it for reposting later.
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
    96
                synchronized (SequencedEvent.class) {
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
    97
                    Iterator<SequencedEvent> it = list.iterator();
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
    98
                    while (it.hasNext()) {
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
    99
                        SequencedEvent iev = it.next();
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
   100
                        if (iev.equals(currentSequencedEvent)) {
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
   101
                            break;
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
   102
                        } else if (iev.equals(ev)) {
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
   103
                            return FilterAction.ACCEPT;
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
   104
                        }
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
   105
                    }
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
   106
                }
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
   107
            } else if (ev.getID() == SentEvent.ID) {
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
   108
                return FilterAction.ACCEPT;
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
   109
            }
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
   110
            currentSequencedEvent.pendingEvents.add(ev);
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
   111
            return FilterAction.REJECT;
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
   112
        }
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
   113
    }
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
   114
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Constructs a new SequencedEvent which will dispatch the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * nested event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @param nested the AWTEvent which this SequencedEvent's dispatch()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *        method will dispatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public SequencedEvent(AWTEvent nested) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        super(nested.getSource(), ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        this.nested = nested;
6826
1fc6a05552f2 6991992: Need to forward-port AWT's part of the fix for 6691674
dcherepanov
parents: 5506
diff changeset
   125
        // All AWTEvents that are wrapped in SequencedEvents are (at
1fc6a05552f2 6991992: Need to forward-port AWT's part of the fix for 6691674
dcherepanov
parents: 5506
diff changeset
   126
        // least currently) implicitly generated by the system
1fc6a05552f2 6991992: Need to forward-port AWT's part of the fix for 6691674
dcherepanov
parents: 5506
diff changeset
   127
        SunToolkit.setSystemGenerated(nested);
47374
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
   128
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
   129
        if (fxAppThreadIsDispatchThread) {
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
   130
            fxCheckSequenceThread = new Thread() {
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
   131
                @Override
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
   132
                public void run() {
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
   133
                    while(!isFirstOrDisposed()) {
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
   134
                        try {
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
   135
                            Thread.sleep(100);
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
   136
                        } catch (InterruptedException e) {
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
   137
                            break;
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
   138
                        }
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
   139
                    }
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
   140
                }
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
   141
            };
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
   142
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        synchronized (SequencedEvent.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            list.add(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Dispatches the nested event after all previous nested events have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * dispatched or disposed. If this method is invoked before all previous nested events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * have been dispatched, then this method blocks until such a point is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * While waiting disposes nested events to disposed AppContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * NOTE: Locking protocol.  Since dispose() can get EventQueue lock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * dispatch() shall never call dispose() while holding the lock on the list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * as EventQueue lock is held during dispatching.  The locks should be acquired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * in the same order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public final void dispatch() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            appContext = AppContext.getAppContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            if (getFirst() != this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                if (EventQueue.isDispatchThread()) {
47374
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
   166
                    if (Thread.currentThread() instanceof EventDispatchThread) {
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
   167
                        EventDispatchThread edt = (EventDispatchThread)
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
   168
                                Thread.currentThread();
52558
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
   169
                        edt.pumpEventsForFilter(() -> !SequencedEvent.this.isFirstOrDisposed(),
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
   170
                                new SequencedEventsFilter(this));
47374
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
   171
                    } else {
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
   172
                        if (fxAppThreadIsDispatchThread) {
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
   173
                            fxCheckSequenceThread.start();
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
   174
                            try {
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
   175
                                // check if event is dispatched or disposed
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
   176
                                // but since this user app thread is same as
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
   177
                                // dispatch thread in fx when run with
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
   178
                                // javafx.embed.singleThread=true
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
   179
                                // we do not wait infinitely to avoid deadlock
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
   180
                                // as dispatch will ultimately be done by this thread
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
   181
                                fxCheckSequenceThread.join(500);
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
   182
                            } catch (InterruptedException e) {
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
   183
                            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                        }
47374
bf712ea57bb0 8088132: [Swing, singleThread] ClassCastException in nested event loop when showing multiple message dialogs in SwingNode
psadhukhan
parents: 47216
diff changeset
   185
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    while(!isFirstOrDisposed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                        synchronized (SequencedEvent.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                SequencedEvent.class.wait(1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            if (!disposed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                KeyboardFocusManager.getCurrentKeyboardFocusManager().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    setCurrentSequencedEvent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                Toolkit.getEventQueue().dispatchEvent(nested);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * true only if event exists and nested source appContext is disposed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   212
    private static final boolean isOwnerAppContextDisposed(SequencedEvent se) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (se != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            Object target = se.nested.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            if (target instanceof Component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                return ((Component)target).appContext.isDisposed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Sequenced events are dispatched in order, so we cannot dispatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * until we are the first sequenced event in the queue (i.e. it's our
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * turn).  But while we wait for our turn to dispatch, the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * could have been disposed for a number of reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public final boolean isFirstOrDisposed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (disposed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        // getFirstWithContext can dispose this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        return this == getFirstWithContext() || disposed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   236
    private static final synchronized SequencedEvent getFirst() {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20107
diff changeset
   237
        return list.getFirst();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /* Disposes all events from disposed AppContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * return first valid event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 25859
diff changeset
   243
    private static final SequencedEvent getFirstWithContext() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        SequencedEvent first = getFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        while(isOwnerAppContextDisposed(first)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            first.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            first = getFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        return first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * Disposes of this instance. This method is invoked once the nested event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * has been dispatched and handled, or when the peer of the target of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * nested event has been disposed with a call to Component.removeNotify.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * NOTE: Locking protocol.  Since SunToolkit.postEvent can get EventQueue lock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * it shall never be called while holding the lock on the list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * as EventQueue lock is held during dispatching and dispatch() will get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * lock on the list. The locks should be acquired in the same order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    final void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
      synchronized (SequencedEvent.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            if (disposed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            if (KeyboardFocusManager.getCurrentKeyboardFocusManager().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    getCurrentSequencedEvent() == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                KeyboardFocusManager.getCurrentKeyboardFocusManager().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    setCurrentSequencedEvent(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            disposed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        SequencedEvent next = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        synchronized (SequencedEvent.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
          SequencedEvent.class.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
          if (list.getFirst() == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
              list.removeFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
              if (!list.isEmpty()) {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20107
diff changeset
   284
                    next = list.getFirst();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
          } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
              list.remove(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        // Wake up waiting threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if (next != null && next.appContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            SunToolkit.postEvent(next.appContext, new SentEvent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
52558
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
   294
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
   295
        for(AWTEvent e : pendingEvents) {
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
   296
            SunToolkit.postEvent(appContext, e);
08a0bf1592bd 8204142: AWT hang occurs when sequenced events arrive out of sequence in multiple AppContexts
mbalao
parents: 49299
diff changeset
   297
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
}