jdk/src/share/classes/java/awt/EventDispatchThread.java
author art
Thu, 11 Sep 2008 10:38:00 +0400
changeset 1961 436a5a828d9f
parent 2 90ce3da70b43
child 1971 553b81ac1756
permissions -rw-r--r--
6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers Reviewed-by: anthony, dav
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1996-2007 Sun Microsystems, Inc.  All Rights Reserved.
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.logging.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.awt.dnd.SunDragSourceContextPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * EventDispatchThread is a package-private AWT class which takes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * events off the EventQueue and dispatches them to the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * AWT components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * The Thread starts a "permanent" event pump with a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * pumpEvents(Conditional) in its run() method. Event handlers can choose to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * block this event pump at any time, but should start a new pump (<b>not</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * a new EventDispatchThread) by again calling pumpEvents(Conditional). This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * secondary event pump will exit automatically as soon as the Condtional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * evaluate()s to false and an additional Event is pumped and dispatched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @author Tom Ball
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @author Amy Fowler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @author Fred Ecks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @author David Mendenhall
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @since 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
class EventDispatchThread extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static final Logger eventLog = Logger.getLogger("java.awt.event.EventDispatchThread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private EventQueue theQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private boolean doDispatch = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static final int ANY_EVENT = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private Vector<EventFilter> eventFilters = new Vector<EventFilter>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // used in handleException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private int modalFiltersCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    EventDispatchThread(ThreadGroup group, String name, EventQueue queue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        super(group, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        theQueue = queue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    void stopDispatchingImpl(boolean wait) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        // Note: We stop dispatching via a flag rather than using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        // Thread.interrupt() because we can't guarantee that the wait()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        // we interrupt will be EventQueue.getNextEvent()'s.  -fredx 8-11-98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        StopDispatchEvent stopEvent = new StopDispatchEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        // wait for the dispatcher to complete
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (Thread.currentThread() != this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            // fix 4122683, 4128923
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            // Post an empty event to ensure getNextEvent is unblocked
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            // We have to use postEventPrivate instead of postEvent because
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            // EventQueue.pop calls EventDispatchThread.stopDispatching.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            // Calling SunToolkit.flushPendingEvents in this case could
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            // lead to deadlock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            theQueue.postEventPrivate(stopEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            if (wait) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                } catch(InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            stopEvent.dispatch();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        synchronized (theQueue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            if (theQueue.getDispatchThread() == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                theQueue.detachDispatchThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public void stopDispatching() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        stopDispatchingImpl(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public void stopDispatchingLater() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        stopDispatchingImpl(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    class StopDispatchEvent extends AWTEvent implements ActiveEvent {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
         * serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        static final long serialVersionUID = -3692158172100730735L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        public StopDispatchEvent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            super(EventDispatchThread.this,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        public void dispatch() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            doDispatch = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            pumpEvents(new Conditional() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                public boolean evaluate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
             * This synchronized block is to secure that the event dispatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
             * thread won't die in the middle of posting a new event to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
             * associated event queue. It is important because we notify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
             * that the event dispatch thread is busy after posting a new event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
             * to its queue, so the EventQueue.dispatchThread reference must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
             * be valid at that point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            synchronized (theQueue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                if (theQueue.getDispatchThread() == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    theQueue.detachDispatchThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                 * Event dispatch thread dies in case of an uncaught exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                 * A new event dispatch thread for this queue will be started
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                 * only if a new event is posted to it. In case if no more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                 * events are posted after this thread died all events that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                 * currently are in the queue will never be dispatched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                 * Fix for 4648733. Check both the associated java event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                 * queue and the PostEventQueue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                if (theQueue.peekEvent() != null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    !SunToolkit.isPostEventQueueEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    theQueue.initDispatchThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                AWTAutoShutdown.getInstance().notifyThreadFree(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    void pumpEvents(Conditional cond) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        pumpEvents(ANY_EVENT, cond);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    void pumpEventsForHierarchy(Conditional cond, Component modalComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        pumpEventsForHierarchy(ANY_EVENT, cond, modalComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    void pumpEvents(int id, Conditional cond) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        pumpEventsForHierarchy(id, cond, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    void pumpEventsForHierarchy(int id, Conditional cond, Component modalComponent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        pumpEventsForFilter(id, cond, new HierarchyEventFilter(modalComponent));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    void pumpEventsForFilter(Conditional cond, EventFilter filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        pumpEventsForFilter(ANY_EVENT, cond, filter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    void pumpEventsForFilter(int id, Conditional cond, EventFilter filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        addEventFilter(filter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        while (doDispatch && cond.evaluate()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            if (isInterrupted() || !pumpOneEventForFilters(id)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                doDispatch = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        removeEventFilter(filter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    void addEventFilter(EventFilter filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        synchronized (eventFilters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            if (!eventFilters.contains(filter)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                if (filter instanceof ModalEventFilter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    ModalEventFilter newFilter = (ModalEventFilter)filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    int k = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    for (k = 0; k < eventFilters.size(); k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                        EventFilter f = eventFilters.get(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                        if (f instanceof ModalEventFilter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                            ModalEventFilter cf = (ModalEventFilter)f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                            if (cf.compareTo(newFilter) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    eventFilters.add(k, filter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    modalFiltersCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    eventFilters.add(filter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    void removeEventFilter(EventFilter filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        synchronized (eventFilters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            if (eventFilters.contains(filter)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                if (filter instanceof ModalEventFilter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    modalFiltersCount--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                eventFilters.remove(filter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    boolean pumpOneEventForFilters(int id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            AWTEvent event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            boolean eventOK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                event = (id == ANY_EVENT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    ? theQueue.getNextEvent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    : theQueue.getNextEvent(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                eventOK = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                synchronized (eventFilters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    for (int i = eventFilters.size() - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                        EventFilter f = eventFilters.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                        EventFilter.FilterAction accept = f.acceptEvent(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                        if (accept == EventFilter.FilterAction.REJECT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                            eventOK = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                        } else if (accept == EventFilter.FilterAction.ACCEPT_IMMEDIATELY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                eventOK = eventOK && SunDragSourceContextPeer.checkEvent(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                if (!eventOK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    event.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            while (eventOK == false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            if (eventLog.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                eventLog.log(Level.FINEST, "Dispatching: " + event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            theQueue.dispatchEvent(event);
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
        catch (ThreadDeath death) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        catch (InterruptedException interruptedException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            return false; // AppContext.dispose() interrupts all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                          // Threads in the AppContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        // Can get and throw only unchecked exceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        catch (RuntimeException e) {
1961
436a5a828d9f 6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents: 2
diff changeset
   289
            processException(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        } catch (Error e) {
1961
436a5a828d9f 6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents: 2
diff changeset
   291
            processException(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
1961
436a5a828d9f 6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents: 2
diff changeset
   296
    private void processException(Throwable e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if (eventLog.isLoggable(Level.FINE)) {
1961
436a5a828d9f 6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents: 2
diff changeset
   298
            eventLog.log(Level.FINE, "Processing exception: " + e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
1961
436a5a828d9f 6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents: 2
diff changeset
   300
        getUncaughtExceptionHandler().uncaughtException(this, e);
436a5a828d9f 6727884: Some Uncaught Exceptions are no longer getting sent to the Uncaught Exception Handlers
art
parents: 2
diff changeset
   301
        // don't rethrow the exception to avoid EDT recreation
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    boolean isDispatching(EventQueue eq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        return theQueue.equals(eq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    EventQueue getEventQueue() { return theQueue; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    private static class HierarchyEventFilter implements EventFilter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        private Component modalComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        public HierarchyEventFilter(Component modalComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            this.modalComponent = modalComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        public FilterAction acceptEvent(AWTEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            if (modalComponent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                int eventID = event.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                boolean mouseEvent = (eventID >= MouseEvent.MOUSE_FIRST) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                                     (eventID <= MouseEvent.MOUSE_LAST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                boolean actionEvent = (eventID >= ActionEvent.ACTION_FIRST) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                                      (eventID <= ActionEvent.ACTION_LAST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                boolean windowClosingEvent = (eventID == WindowEvent.WINDOW_CLOSING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                 * filter out MouseEvent and ActionEvent that's outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                 * the modalComponent hierarchy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                 * KeyEvent is handled by using enqueueKeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                 * in Dialog.show
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                if (Component.isInstanceOf(modalComponent, "javax.swing.JInternalFrame")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                     * Modal internal frames are handled separately. If event is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                     * for some component from another heavyweight than modalComp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                     * it is accepted. If heavyweight is the same - we still accept
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                     * event and perform further filtering in LightweightDispatcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    return windowClosingEvent ? FilterAction.REJECT : FilterAction.ACCEPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                if (mouseEvent || actionEvent || windowClosingEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    Object o = event.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    if (o instanceof sun.awt.ModalExclude) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                        // Exclude this object from modality and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                        // continue to pump it's events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                        return FilterAction.ACCEPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    } else if (o instanceof Component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                        Component c = (Component) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                        // 5.0u3 modal exclusion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                        boolean modalExcluded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                        if (modalComponent instanceof Container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                            while (c != modalComponent && c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                                if ((c instanceof Window) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                                    (sun.awt.SunToolkit.isModalExcluded((Window)c))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                                    // Exclude this window and all its children from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                                    //  modality and continue to pump it's events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                                    modalExcluded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                                c = c.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                        if (!modalExcluded && (c != modalComponent)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                            return FilterAction.REJECT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            return FilterAction.ACCEPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
}