jdk/src/share/classes/java/awt/EventDispatchThread.java
author mchung
Tue, 08 Dec 2009 09:02:09 -0800
changeset 4374 f672d9cf521e
parent 4365 4ac67034e98b
child 5506 202f599c92aa
permissions -rw-r--r--
6907568: java/awt/KeyboardFocusManager.java inproperly merged and lost a changeset Summary: Reapply fix for 6879044 in java.awt.KeyboardFocusManager Reviewed-by: dcherepanov, asaha
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
1639
a97859015238 6785258: Update copyright year
xdono
parents: 1293
diff changeset
     2
 * Copyright 1996-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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.InputEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.MouseEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.event.ActionEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.event.WindowEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.action.GetPropertyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.awt.AWTAutoShutdown;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.awt.SunToolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Vector;
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 1979
diff changeset
    39
import sun.util.logging.PlatformLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.awt.dnd.SunDragSourceContextPeer;
1293
ef349b440fc9 6638195: need API for EventQueueDelegate
idk
parents: 2
diff changeset
    42
import sun.awt.EventQueueDelegate;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * EventDispatchThread is a package-private AWT class which takes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * events off the EventQueue and dispatches them to the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * AWT components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * The Thread starts a "permanent" event pump with a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * pumpEvents(Conditional) in its run() method. Event handlers can choose to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * block this event pump at any time, but should start a new pump (<b>not</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * a new EventDispatchThread) by again calling pumpEvents(Conditional). This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * secondary event pump will exit automatically as soon as the Condtional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * evaluate()s to false and an additional Event is pumped and dispatched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @author Tom Ball
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @author Amy Fowler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @author Fred Ecks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @author David Mendenhall
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @since 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
class EventDispatchThread extends Thread {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 1979
diff changeset
    64
    private static final PlatformLogger eventLog = PlatformLogger.getLogger("java.awt.event.EventDispatchThread");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private EventQueue theQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private boolean doDispatch = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static final int ANY_EVENT = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private Vector<EventFilter> eventFilters = new Vector<EventFilter>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // used in handleException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private int modalFiltersCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    EventDispatchThread(ThreadGroup group, String name, EventQueue queue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        super(group, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        theQueue = queue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    void stopDispatchingImpl(boolean wait) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        // Note: We stop dispatching via a flag rather than using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        // Thread.interrupt() because we can't guarantee that the wait()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // we interrupt will be EventQueue.getNextEvent()'s.  -fredx 8-11-98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        StopDispatchEvent stopEvent = new StopDispatchEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        // wait for the dispatcher to complete
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (Thread.currentThread() != this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            // fix 4122683, 4128923
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            // Post an empty event to ensure getNextEvent is unblocked
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            // We have to use postEventPrivate instead of postEvent because
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            // EventQueue.pop calls EventDispatchThread.stopDispatching.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            // Calling SunToolkit.flushPendingEvents in this case could
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            // lead to deadlock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            theQueue.postEventPrivate(stopEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            if (wait) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                } catch(InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            stopEvent.dispatch();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 3938
diff changeset
   107
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 3938
diff changeset
   108
        theQueue.detachDispatchThread(this, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public void stopDispatching() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        stopDispatchingImpl(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public void stopDispatchingLater() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        stopDispatchingImpl(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    class StopDispatchEvent extends AWTEvent implements ActiveEvent {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
         * serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        static final long serialVersionUID = -3692158172100730735L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        public StopDispatchEvent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            super(EventDispatchThread.this,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        public void dispatch() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            doDispatch = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            pumpEvents(new Conditional() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                public boolean evaluate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        } finally {
4365
4ac67034e98b 4913324: Deadlock when using two event queues
art
parents: 3938
diff changeset
   142
            theQueue.detachDispatchThread(this, true);
2
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
    void pumpEvents(Conditional cond) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        pumpEvents(ANY_EVENT, cond);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    void pumpEventsForHierarchy(Conditional cond, Component modalComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        pumpEventsForHierarchy(ANY_EVENT, cond, modalComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    void pumpEvents(int id, Conditional cond) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        pumpEventsForHierarchy(id, cond, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    void pumpEventsForHierarchy(int id, Conditional cond, Component modalComponent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        pumpEventsForFilter(id, cond, new HierarchyEventFilter(modalComponent));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    void pumpEventsForFilter(Conditional cond, EventFilter filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        pumpEventsForFilter(ANY_EVENT, cond, filter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    void pumpEventsForFilter(int id, Conditional cond, EventFilter filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        addEventFilter(filter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        while (doDispatch && cond.evaluate()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            if (isInterrupted() || !pumpOneEventForFilters(id)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                doDispatch = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        removeEventFilter(filter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    void addEventFilter(EventFilter filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        synchronized (eventFilters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            if (!eventFilters.contains(filter)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                if (filter instanceof ModalEventFilter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    ModalEventFilter newFilter = (ModalEventFilter)filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    int k = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    for (k = 0; k < eventFilters.size(); k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                        EventFilter f = eventFilters.get(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                        if (f instanceof ModalEventFilter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                            ModalEventFilter cf = (ModalEventFilter)f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                            if (cf.compareTo(newFilter) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    eventFilters.add(k, filter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    modalFiltersCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    eventFilters.add(filter);
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
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    void removeEventFilter(EventFilter filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        synchronized (eventFilters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            if (eventFilters.contains(filter)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                if (filter instanceof ModalEventFilter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                    modalFiltersCount--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                eventFilters.remove(filter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    boolean pumpOneEventForFilters(int id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            AWTEvent event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            boolean eventOK;
1293
ef349b440fc9 6638195: need API for EventQueueDelegate
idk
parents: 2
diff changeset
   216
            EventQueueDelegate.Delegate delegate =
ef349b440fc9 6638195: need API for EventQueueDelegate
idk
parents: 2
diff changeset
   217
                EventQueueDelegate.getDelegate();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            do {
1293
ef349b440fc9 6638195: need API for EventQueueDelegate
idk
parents: 2
diff changeset
   219
                if (delegate != null && id == ANY_EVENT) {
ef349b440fc9 6638195: need API for EventQueueDelegate
idk
parents: 2
diff changeset
   220
                    event = delegate.getNextEvent(theQueue);
ef349b440fc9 6638195: need API for EventQueueDelegate
idk
parents: 2
diff changeset
   221
                } else {
ef349b440fc9 6638195: need API for EventQueueDelegate
idk
parents: 2
diff changeset
   222
                    event = (id == ANY_EVENT)
ef349b440fc9 6638195: need API for EventQueueDelegate
idk
parents: 2
diff changeset
   223
                        ? theQueue.getNextEvent()
ef349b440fc9 6638195: need API for EventQueueDelegate
idk
parents: 2
diff changeset
   224
                        : theQueue.getNextEvent(id);
ef349b440fc9 6638195: need API for EventQueueDelegate
idk
parents: 2
diff changeset
   225
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                eventOK = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                synchronized (eventFilters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    for (int i = eventFilters.size() - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                        EventFilter f = eventFilters.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                        EventFilter.FilterAction accept = f.acceptEvent(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                        if (accept == EventFilter.FilterAction.REJECT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                            eventOK = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                        } else if (accept == EventFilter.FilterAction.ACCEPT_IMMEDIATELY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                eventOK = eventOK && SunDragSourceContextPeer.checkEvent(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                if (!eventOK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    event.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            while (eventOK == false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 1979
diff changeset
   247
            if (eventLog.isLoggable(PlatformLogger.FINEST)) {
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 1979
diff changeset
   248
                eventLog.finest("Dispatching: " + event);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
1293
ef349b440fc9 6638195: need API for EventQueueDelegate
idk
parents: 2
diff changeset
   251
            Object handle = null;
ef349b440fc9 6638195: need API for EventQueueDelegate
idk
parents: 2
diff changeset
   252
            if (delegate != null) {
ef349b440fc9 6638195: need API for EventQueueDelegate
idk
parents: 2
diff changeset
   253
                handle = delegate.beforeDispatch(event);
ef349b440fc9 6638195: need API for EventQueueDelegate
idk
parents: 2
diff changeset
   254
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            theQueue.dispatchEvent(event);
1293
ef349b440fc9 6638195: need API for EventQueueDelegate
idk
parents: 2
diff changeset
   256
            if (delegate != null) {
ef349b440fc9 6638195: need API for EventQueueDelegate
idk
parents: 2
diff changeset
   257
                delegate.afterDispatch(event, handle);
ef349b440fc9 6638195: need API for EventQueueDelegate
idk
parents: 2
diff changeset
   258
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        catch (ThreadDeath death) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        catch (InterruptedException interruptedException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            return false; // AppContext.dispose() interrupts all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                          // Threads in the AppContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        // Can get and throw only unchecked exceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        catch (RuntimeException e) {
1961
436a5a828d9f 6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents: 2
diff changeset
   272
            processException(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        } catch (Error e) {
1961
436a5a828d9f 6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents: 2
diff changeset
   274
            processException(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
1961
436a5a828d9f 6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents: 2
diff changeset
   279
    private void processException(Throwable e) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 1979
diff changeset
   280
        if (eventLog.isLoggable(PlatformLogger.FINE)) {
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 1979
diff changeset
   281
            eventLog.fine("Processing exception: " + e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
1961
436a5a828d9f 6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents: 2
diff changeset
   283
        getUncaughtExceptionHandler().uncaughtException(this, e);
436a5a828d9f 6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents: 2
diff changeset
   284
        // don't rethrow the exception to avoid EDT recreation
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    boolean isDispatching(EventQueue eq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        return theQueue.equals(eq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    EventQueue getEventQueue() { return theQueue; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    private static class HierarchyEventFilter implements EventFilter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        private Component modalComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        public HierarchyEventFilter(Component modalComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            this.modalComponent = modalComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        public FilterAction acceptEvent(AWTEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            if (modalComponent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                int eventID = event.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                boolean mouseEvent = (eventID >= MouseEvent.MOUSE_FIRST) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                                     (eventID <= MouseEvent.MOUSE_LAST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                boolean actionEvent = (eventID >= ActionEvent.ACTION_FIRST) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                                      (eventID <= ActionEvent.ACTION_LAST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                boolean windowClosingEvent = (eventID == WindowEvent.WINDOW_CLOSING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                 * filter out MouseEvent and ActionEvent that's outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                 * the modalComponent hierarchy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                 * KeyEvent is handled by using enqueueKeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                 * in Dialog.show
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                if (Component.isInstanceOf(modalComponent, "javax.swing.JInternalFrame")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                     * Modal internal frames are handled separately. If event is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                     * for some component from another heavyweight than modalComp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                     * it is accepted. If heavyweight is the same - we still accept
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                     * event and perform further filtering in LightweightDispatcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    return windowClosingEvent ? FilterAction.REJECT : FilterAction.ACCEPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                if (mouseEvent || actionEvent || windowClosingEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    Object o = event.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    if (o instanceof sun.awt.ModalExclude) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                        // Exclude this object from modality and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                        // continue to pump it's events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                        return FilterAction.ACCEPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    } else if (o instanceof Component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                        Component c = (Component) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                        // 5.0u3 modal exclusion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                        boolean modalExcluded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                        if (modalComponent instanceof Container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                            while (c != modalComponent && c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                                if ((c instanceof Window) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                                    (sun.awt.SunToolkit.isModalExcluded((Window)c))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                                    // Exclude this window and all its children from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                                    //  modality and continue to pump it's events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                                    modalExcluded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                                c = c.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                        if (!modalExcluded && (c != modalComponent)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                            return FilterAction.REJECT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            return FilterAction.ACCEPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
}