jdk/src/share/classes/java/awt/EventDispatchThread.java
author henryjen
Tue, 10 Jun 2014 16:18:54 -0700
changeset 24865 09b1d992ca72
parent 21263 77142152d60f
child 25206 f1ed7d27ec7f
permissions -rw-r--r--
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo Reviewed-by: mduigou, lancea, alanb, mullan
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: 18178
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.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;
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 1979
diff changeset
    33
import sun.util.logging.PlatformLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.awt.dnd.SunDragSourceContextPeer;
1293
ef349b440fc9 6638195: need API for EventQueueDelegate
idk
parents: 2
diff changeset
    36
import sun.awt.EventQueueDelegate;
2
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * secondary event pump will exit automatically as soon as the Condtional
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
 */
90ce3da70b43 Initial load
duke
parents:
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    EventDispatchThread(ThreadGroup group, String name, EventQueue queue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        super(group, name);
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
    70
        setEventQueue(queue);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
    73
    /*
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
    74
     * 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
    75
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public void stopDispatching() {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
    77
        doDispatch = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public void run() {
21263
77142152d60f 2228674: Fix failed for CR 7162144
bagiras
parents: 18178
diff changeset
    81
        try {
77142152d60f 2228674: Fix failed for CR 7162144
bagiras
parents: 18178
diff changeset
    82
            pumpEvents(new Conditional() {
77142152d60f 2228674: Fix failed for CR 7162144
bagiras
parents: 18178
diff changeset
    83
                public boolean evaluate() {
77142152d60f 2228674: Fix failed for CR 7162144
bagiras
parents: 18178
diff changeset
    84
                    return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                }
21263
77142152d60f 2228674: Fix failed for CR 7162144
bagiras
parents: 18178
diff changeset
    86
            });
77142152d60f 2228674: Fix failed for CR 7162144
bagiras
parents: 18178
diff changeset
    87
        } finally {
77142152d60f 2228674: Fix failed for CR 7162144
bagiras
parents: 18178
diff changeset
    88
            getEventQueue().detachDispatchThread(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    void pumpEvents(Conditional cond) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        pumpEvents(ANY_EVENT, cond);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    void pumpEventsForHierarchy(Conditional cond, Component modalComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        pumpEventsForHierarchy(ANY_EVENT, cond, modalComponent);
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(int id, Conditional cond) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        pumpEventsForHierarchy(id, cond, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
6484
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   104
    void pumpEventsForHierarchy(int id, Conditional cond, Component modalComponent) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        pumpEventsForFilter(id, cond, new HierarchyEventFilter(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 pumpEventsForFilter(Conditional cond, EventFilter filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        pumpEventsForFilter(ANY_EVENT, cond, filter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    void pumpEventsForFilter(int id, Conditional cond, EventFilter filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        addEventFilter(filter);
6484
f5dbd940a640 6949936: Provide API for running nested events loops, similar to what modal dialogs do
art
parents: 5942
diff changeset
   114
        doDispatch = true;
21263
77142152d60f 2228674: Fix failed for CR 7162144
bagiras
parents: 18178
diff changeset
   115
        while (doDispatch && !isInterrupted() && cond.evaluate()) {
10583
8bb208d39fb1 7081670: Disposing an AppContext can lead to a spinning EventDispatchThread
anthony
parents: 7668
diff changeset
   116
            pumpOneEventForFilters(id);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        removeEventFilter(filter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    void addEventFilter(EventFilter filter) {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   122
        if (eventLog.isLoggable(PlatformLogger.Level.FINEST)) {
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 15639
diff changeset
   123
            eventLog.finest("adding the event filter: " + filter);
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 15639
diff changeset
   124
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        synchronized (eventFilters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (!eventFilters.contains(filter)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                if (filter instanceof ModalEventFilter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    ModalEventFilter newFilter = (ModalEventFilter)filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    int k = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    for (k = 0; k < eventFilters.size(); k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                        EventFilter f = eventFilters.get(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                        if (f instanceof ModalEventFilter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                            ModalEventFilter cf = (ModalEventFilter)f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                            if (cf.compareTo(newFilter) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    eventFilters.add(k, filter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    eventFilters.add(filter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            }
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
    void removeEventFilter(EventFilter filter) {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   148
        if (eventLog.isLoggable(PlatformLogger.Level.FINEST)) {
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 15639
diff changeset
   149
            eventLog.finest("removing the event filter: " + filter);
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 15639
diff changeset
   150
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        synchronized (eventFilters) {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   152
            eventFilters.remove(filter);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
10583
8bb208d39fb1 7081670: Disposing an AppContext can lead to a spinning EventDispatchThread
anthony
parents: 7668
diff changeset
   156
    void pumpOneEventForFilters(int id) {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   157
        AWTEvent event = null;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   158
        boolean eventOK = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        try {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   160
            EventQueue eq = null;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   161
            EventQueueDelegate.Delegate delegate = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            do {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   163
                // EventQueue may change during the dispatching
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   164
                eq = getEventQueue();
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   165
                delegate = EventQueueDelegate.getDelegate();
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   166
1293
ef349b440fc9 6638195: need API for EventQueueDelegate
idk
parents: 2
diff changeset
   167
                if (delegate != null && id == ANY_EVENT) {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   168
                    event = delegate.getNextEvent(eq);
1293
ef349b440fc9 6638195: need API for EventQueueDelegate
idk
parents: 2
diff changeset
   169
                } else {
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   170
                    event = (id == ANY_EVENT) ? eq.getNextEvent() : eq.getNextEvent(id);
1293
ef349b440fc9 6638195: need API for EventQueueDelegate
idk
parents: 2
diff changeset
   171
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                eventOK = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                synchronized (eventFilters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    for (int i = eventFilters.size() - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        EventFilter f = eventFilters.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                        EventFilter.FilterAction accept = f.acceptEvent(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        if (accept == EventFilter.FilterAction.REJECT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                            eventOK = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                        } else if (accept == EventFilter.FilterAction.ACCEPT_IMMEDIATELY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                eventOK = eventOK && SunDragSourceContextPeer.checkEvent(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                if (!eventOK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    event.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            while (eventOK == false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   193
            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
   194
                eventLog.finest("Dispatching: " + event);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
1293
ef349b440fc9 6638195: need API for EventQueueDelegate
idk
parents: 2
diff changeset
   197
            Object handle = null;
ef349b440fc9 6638195: need API for EventQueueDelegate
idk
parents: 2
diff changeset
   198
            if (delegate != null) {
ef349b440fc9 6638195: need API for EventQueueDelegate
idk
parents: 2
diff changeset
   199
                handle = delegate.beforeDispatch(event);
ef349b440fc9 6638195: need API for EventQueueDelegate
idk
parents: 2
diff changeset
   200
            }
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   201
            eq.dispatchEvent(event);
1293
ef349b440fc9 6638195: need API for EventQueueDelegate
idk
parents: 2
diff changeset
   202
            if (delegate != null) {
ef349b440fc9 6638195: need API for EventQueueDelegate
idk
parents: 2
diff changeset
   203
                delegate.afterDispatch(event, handle);
ef349b440fc9 6638195: need API for EventQueueDelegate
idk
parents: 2
diff changeset
   204
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        catch (ThreadDeath death) {
21263
77142152d60f 2228674: Fix failed for CR 7162144
bagiras
parents: 18178
diff changeset
   207
            doDispatch = false;
10583
8bb208d39fb1 7081670: Disposing an AppContext can lead to a spinning EventDispatchThread
anthony
parents: 7668
diff changeset
   208
            throw death;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        catch (InterruptedException interruptedException) {
21263
77142152d60f 2228674: Fix failed for CR 7162144
bagiras
parents: 18178
diff changeset
   211
            doDispatch = false; // AppContext.dispose() interrupts all
77142152d60f 2228674: Fix failed for CR 7162144
bagiras
parents: 18178
diff changeset
   212
                                // Threads in the AppContext
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   214
        catch (Throwable e) {
1961
436a5a828d9f 6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents: 2
diff changeset
   215
            processException(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
1961
436a5a828d9f 6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents: 2
diff changeset
   219
    private void processException(Throwable e) {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   220
        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
   221
            eventLog.fine("Processing exception: " + e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
1961
436a5a828d9f 6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents: 2
diff changeset
   223
        getUncaughtExceptionHandler().uncaughtException(this, e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   226
    public synchronized EventQueue getEventQueue() {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   227
        return theQueue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
5942
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   229
    public synchronized void setEventQueue(EventQueue eq) {
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   230
        theQueue = eq;
287c421fb9b2 6424157: java.awt.EventQueue push/pop might cause threading issues
art
parents: 5506
diff changeset
   231
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    private static class HierarchyEventFilter implements EventFilter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        private Component modalComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        public HierarchyEventFilter(Component modalComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            this.modalComponent = modalComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        public FilterAction acceptEvent(AWTEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            if (modalComponent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                int eventID = event.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                boolean mouseEvent = (eventID >= MouseEvent.MOUSE_FIRST) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                                     (eventID <= MouseEvent.MOUSE_LAST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                boolean actionEvent = (eventID >= ActionEvent.ACTION_FIRST) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                                      (eventID <= ActionEvent.ACTION_LAST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                boolean windowClosingEvent = (eventID == WindowEvent.WINDOW_CLOSING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                 * filter out MouseEvent and ActionEvent that's outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                 * the modalComponent hierarchy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                 * KeyEvent is handled by using enqueueKeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                 * in Dialog.show
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                if (Component.isInstanceOf(modalComponent, "javax.swing.JInternalFrame")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                     * Modal internal frames are handled separately. If event is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                     * for some component from another heavyweight than modalComp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                     * it is accepted. If heavyweight is the same - we still accept
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                     * event and perform further filtering in LightweightDispatcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    return windowClosingEvent ? FilterAction.REJECT : FilterAction.ACCEPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                if (mouseEvent || actionEvent || windowClosingEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    Object o = event.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    if (o instanceof sun.awt.ModalExclude) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                        // Exclude this object from modality and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                        // continue to pump it's events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                        return FilterAction.ACCEPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    } else if (o instanceof Component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                        Component c = (Component) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                        // 5.0u3 modal exclusion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                        boolean modalExcluded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                        if (modalComponent instanceof Container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                            while (c != modalComponent && c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                                if ((c instanceof Window) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                                    (sun.awt.SunToolkit.isModalExcluded((Window)c))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                                    // Exclude this window and all its children from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                                    //  modality and continue to pump it's events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                                    modalExcluded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                                c = c.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                        if (!modalExcluded && (c != modalComponent)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                            return FilterAction.REJECT;
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
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            return FilterAction.ACCEPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
}