jdk/src/java.desktop/share/classes/java/awt/EventDispatchThread.java
author azvegint
Thu, 31 Aug 2017 09:28:21 +0530
changeset 47192 d46b91465a81
parent 37550 c8252b8fea3d
permissions -rw-r--r--
8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true Reviewed-by: kcr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
31653
d88ff422c7fb 8080405: Exception in thread "AWT-EventQueue-1" java.security.AccessControlException
serb
parents: 29922
diff changeset
     2
 * Copyright (c) 1996, 2015, 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.MouseEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.ActionEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.event.WindowEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
10583
8bb208d39fb1 7081670: Disposing an AppContext can lead to a spinning EventDispatchThread
anthony
parents: 7668
diff changeset
    32
import java.util.ArrayList;
29922
7b9c1e1532cf 8027771: Enhance thread contexts
serb
parents: 26749
diff changeset
    33
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 1979
diff changeset
    34
import sun.util.logging.PlatformLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.awt.dnd.SunDragSourceContextPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * EventDispatchThread is a package-private AWT class which takes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * events off the EventQueue and dispatches them to the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * AWT components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * The Thread starts a "permanent" event pump with a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * pumpEvents(Conditional) in its run() method. Event handlers can choose to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * block this event pump at any time, but should start a new pump (<b>not</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * a new EventDispatchThread) by again calling pumpEvents(Conditional). This
26749
b6598aa90114 8055326: Fix typos in client-related packages
serb
parents: 25859
diff changeset
    47
 * secondary event pump will exit automatically as soon as the Conditional
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * evaluate()s to false and an additional Event is pumped and dispatched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author Tom Ball
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author Amy Fowler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @author Fred Ecks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @author David Mendenhall
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @since 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
37550
c8252b8fea3d 8147544: Remove sun.misc.ManagedLocalsThread from java.desktop
prr
parents: 31653
diff changeset
    57
class EventDispatchThread extends Thread {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
    58
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 1979
diff changeset
    59
    private static final PlatformLogger eventLog = PlatformLogger.getLogger("java.awt.event.EventDispatchThread");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private EventQueue theQueue;
21263
77142152d60f 2228674: Fix failed for CR 7162144
bagiras
parents: 18178
diff changeset
    62
    private volatile boolean doDispatch = true;
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
    63
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private static final int ANY_EVENT = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
10583
8bb208d39fb1 7081670: Disposing an AppContext can lead to a spinning EventDispatchThread
anthony
parents: 7668
diff changeset
    66
    private ArrayList<EventFilter> eventFilters = new ArrayList<EventFilter>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
37550
c8252b8fea3d 8147544: Remove sun.misc.ManagedLocalsThread from java.desktop
prr
parents: 31653
diff changeset
    68
   /**
c8252b8fea3d 8147544: Remove sun.misc.ManagedLocalsThread from java.desktop
prr
parents: 31653
diff changeset
    69
    * Must always call 5 args super-class constructor passing false
c8252b8fea3d 8147544: Remove sun.misc.ManagedLocalsThread from java.desktop
prr
parents: 31653
diff changeset
    70
    * to indicate not to inherit locals.
c8252b8fea3d 8147544: Remove sun.misc.ManagedLocalsThread from java.desktop
prr
parents: 31653
diff changeset
    71
    */
c8252b8fea3d 8147544: Remove sun.misc.ManagedLocalsThread from java.desktop
prr
parents: 31653
diff changeset
    72
    private EventDispatchThread() {
c8252b8fea3d 8147544: Remove sun.misc.ManagedLocalsThread from java.desktop
prr
parents: 31653
diff changeset
    73
        throw new UnsupportedOperationException("Must erase locals");
c8252b8fea3d 8147544: Remove sun.misc.ManagedLocalsThread from java.desktop
prr
parents: 31653
diff changeset
    74
    }
c8252b8fea3d 8147544: Remove sun.misc.ManagedLocalsThread from java.desktop
prr
parents: 31653
diff changeset
    75
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    EventDispatchThread(ThreadGroup group, String name, EventQueue queue) {
37550
c8252b8fea3d 8147544: Remove sun.misc.ManagedLocalsThread from java.desktop
prr
parents: 31653
diff changeset
    77
        super(group, null, name, 0, false);
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
    78
        setEventQueue(queue);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
    81
    /*
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
    82
     * Must be called on EDT only, that's why no synchronization
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
    83
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public void stopDispatching() {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
    85
        doDispatch = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public void run() {
21263
77142152d60f 2228674: Fix failed for CR 7162144
bagiras
parents: 18178
diff changeset
    89
        try {
77142152d60f 2228674: Fix failed for CR 7162144
bagiras
parents: 18178
diff changeset
    90
            pumpEvents(new Conditional() {
77142152d60f 2228674: Fix failed for CR 7162144
bagiras
parents: 18178
diff changeset
    91
                public boolean evaluate() {
77142152d60f 2228674: Fix failed for CR 7162144
bagiras
parents: 18178
diff changeset
    92
                    return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                }
21263
77142152d60f 2228674: Fix failed for CR 7162144
bagiras
parents: 18178
diff changeset
    94
            });
77142152d60f 2228674: Fix failed for CR 7162144
bagiras
parents: 18178
diff changeset
    95
        } finally {
77142152d60f 2228674: Fix failed for CR 7162144
bagiras
parents: 18178
diff changeset
    96
            getEventQueue().detachDispatchThread(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    void pumpEvents(Conditional cond) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        pumpEvents(ANY_EVENT, cond);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    void pumpEventsForHierarchy(Conditional cond, Component modalComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        pumpEventsForHierarchy(ANY_EVENT, cond, modalComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    void pumpEvents(int id, Conditional cond) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        pumpEventsForHierarchy(id, cond, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
6484
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   112
    void pumpEventsForHierarchy(int id, Conditional cond, Component modalComponent) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        pumpEventsForFilter(id, cond, new HierarchyEventFilter(modalComponent));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    void pumpEventsForFilter(Conditional cond, EventFilter filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        pumpEventsForFilter(ANY_EVENT, cond, filter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    void pumpEventsForFilter(int id, Conditional cond, EventFilter filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        addEventFilter(filter);
6484
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   122
        doDispatch = true;
21263
77142152d60f 2228674: Fix failed for CR 7162144
bagiras
parents: 18178
diff changeset
   123
        while (doDispatch && !isInterrupted() && cond.evaluate()) {
10583
8bb208d39fb1 7081670: Disposing an AppContext can lead to a spinning EventDispatchThread
anthony
parents: 7668
diff changeset
   124
            pumpOneEventForFilters(id);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        removeEventFilter(filter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    void addEventFilter(EventFilter filter) {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   130
        if (eventLog.isLoggable(PlatformLogger.Level.FINEST)) {
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 15639
diff changeset
   131
            eventLog.finest("adding the event filter: " + filter);
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 15639
diff changeset
   132
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        synchronized (eventFilters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            if (!eventFilters.contains(filter)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                if (filter instanceof ModalEventFilter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    ModalEventFilter newFilter = (ModalEventFilter)filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    int k = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    for (k = 0; k < eventFilters.size(); k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                        EventFilter f = eventFilters.get(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                        if (f instanceof ModalEventFilter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                            ModalEventFilter cf = (ModalEventFilter)f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                            if (cf.compareTo(newFilter) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    eventFilters.add(k, filter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    eventFilters.add(filter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    void removeEventFilter(EventFilter filter) {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   156
        if (eventLog.isLoggable(PlatformLogger.Level.FINEST)) {
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 15639
diff changeset
   157
            eventLog.finest("removing the event filter: " + filter);
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 15639
diff changeset
   158
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        synchronized (eventFilters) {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   160
            eventFilters.remove(filter);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
47192
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 37550
diff changeset
   164
    boolean filterAndCheckEvent(AWTEvent event) {
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 37550
diff changeset
   165
        boolean eventOK = true;
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 37550
diff changeset
   166
        synchronized (eventFilters) {
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 37550
diff changeset
   167
            for (int i = eventFilters.size() - 1; i >= 0; i--) {
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 37550
diff changeset
   168
                EventFilter f = eventFilters.get(i);
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 37550
diff changeset
   169
                EventFilter.FilterAction accept = f.acceptEvent(event);
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 37550
diff changeset
   170
                if (accept == EventFilter.FilterAction.REJECT) {
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 37550
diff changeset
   171
                    eventOK = false;
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 37550
diff changeset
   172
                    break;
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 37550
diff changeset
   173
                } else if (accept == EventFilter.FilterAction.ACCEPT_IMMEDIATELY) {
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 37550
diff changeset
   174
                    break;
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 37550
diff changeset
   175
                }
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 37550
diff changeset
   176
            }
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 37550
diff changeset
   177
        }
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 37550
diff changeset
   178
        return eventOK && SunDragSourceContextPeer.checkEvent(event);
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 37550
diff changeset
   179
    }
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 37550
diff changeset
   180
10583
8bb208d39fb1 7081670: Disposing an AppContext can lead to a spinning EventDispatchThread
anthony
parents: 7668
diff changeset
   181
    void pumpOneEventForFilters(int id) {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   182
        AWTEvent event = null;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   183
        boolean eventOK = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        try {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   185
            EventQueue eq = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            do {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   187
                // EventQueue may change during the dispatching
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   188
                eq = getEventQueue();
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   189
25206
f1ed7d27ec7f 8047798: Remove EventQueueDelegate
pchelko
parents: 21263
diff changeset
   190
                event = (id == ANY_EVENT) ? eq.getNextEvent() : eq.getNextEvent(id);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
47192
d46b91465a81 8181786: Extra runLater causes impossible states to be possible using javafx.embed.singleThread=true
azvegint
parents: 37550
diff changeset
   192
                eventOK = filterAndCheckEvent(event);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                if (!eventOK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    event.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            while (eventOK == false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   199
            if (eventLog.isLoggable(PlatformLogger.Level.FINEST)) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 1979
diff changeset
   200
                eventLog.finest("Dispatching: " + event);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   203
            eq.dispatchEvent(event);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        catch (ThreadDeath death) {
21263
77142152d60f 2228674: Fix failed for CR 7162144
bagiras
parents: 18178
diff changeset
   206
            doDispatch = false;
10583
8bb208d39fb1 7081670: Disposing an AppContext can lead to a spinning EventDispatchThread
anthony
parents: 7668
diff changeset
   207
            throw death;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        catch (InterruptedException interruptedException) {
21263
77142152d60f 2228674: Fix failed for CR 7162144
bagiras
parents: 18178
diff changeset
   210
            doDispatch = false; // AppContext.dispose() interrupts all
77142152d60f 2228674: Fix failed for CR 7162144
bagiras
parents: 18178
diff changeset
   211
                                // Threads in the AppContext
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   213
        catch (Throwable e) {
1961
436a5a828d9f 6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents: 2
diff changeset
   214
            processException(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
1961
436a5a828d9f 6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents: 2
diff changeset
   218
    private void processException(Throwable e) {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   219
        if (eventLog.isLoggable(PlatformLogger.Level.FINE)) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 1979
diff changeset
   220
            eventLog.fine("Processing exception: " + e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
1961
436a5a828d9f 6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents: 2
diff changeset
   222
        getUncaughtExceptionHandler().uncaughtException(this, e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   225
    public synchronized EventQueue getEventQueue() {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   226
        return theQueue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   228
    public synchronized void setEventQueue(EventQueue eq) {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   229
        theQueue = eq;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   230
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    private static class HierarchyEventFilter implements EventFilter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        private Component modalComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        public HierarchyEventFilter(Component modalComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            this.modalComponent = modalComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        public FilterAction acceptEvent(AWTEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            if (modalComponent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                int eventID = event.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                boolean mouseEvent = (eventID >= MouseEvent.MOUSE_FIRST) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                                     (eventID <= MouseEvent.MOUSE_LAST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                boolean actionEvent = (eventID >= ActionEvent.ACTION_FIRST) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                                      (eventID <= ActionEvent.ACTION_LAST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                boolean windowClosingEvent = (eventID == WindowEvent.WINDOW_CLOSING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                 * filter out MouseEvent and ActionEvent that's outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                 * the modalComponent hierarchy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                 * KeyEvent is handled by using enqueueKeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                 * in Dialog.show
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                if (Component.isInstanceOf(modalComponent, "javax.swing.JInternalFrame")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                     * Modal internal frames are handled separately. If event is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                     * for some component from another heavyweight than modalComp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                     * it is accepted. If heavyweight is the same - we still accept
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                     * event and perform further filtering in LightweightDispatcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    return windowClosingEvent ? FilterAction.REJECT : FilterAction.ACCEPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                if (mouseEvent || actionEvent || windowClosingEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    Object o = event.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    if (o instanceof sun.awt.ModalExclude) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                        // Exclude this object from modality and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                        // continue to pump it's events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                        return FilterAction.ACCEPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    } else if (o instanceof Component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                        Component c = (Component) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                        // 5.0u3 modal exclusion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                        boolean modalExcluded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                        if (modalComponent instanceof Container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                            while (c != modalComponent && c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                                if ((c instanceof Window) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                                    (sun.awt.SunToolkit.isModalExcluded((Window)c))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                                    // Exclude this window and all its children from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                                    //  modality and continue to pump it's events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                                    modalExcluded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                                c = c.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                        if (!modalExcluded && (c != modalComponent)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                            return FilterAction.REJECT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            return FilterAction.ACCEPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
}