jdk/src/share/classes/java/awt/DefaultKeyboardFocusManager.java
author ant
Fri, 31 Aug 2012 16:31:29 +0400
changeset 13652 42544e68dc39
parent 12651 8c69dd24bf07
child 13656 776ac504ea9b
permissions -rw-r--r--
6981400: Tabbing between textfield do not work properly when ALT+TAB 7157015: [macosx] Situation when KeyEventDispatcher doesn't work on AWT but does on Swing. 7121442: Regression : Reopen CR 6458497 still reproducible using JDK 7. Reviewed-by: art, leonidr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4214
diff changeset
     2
 * Copyright (c) 2000, 2008, 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: 4214
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: 4214
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: 4214
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4214
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4214
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.event.FocusEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.event.KeyEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.WindowEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.peer.ComponentPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.peer.LightweightPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.LinkedList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.ListIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
    38
import sun.util.logging.PlatformLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.awt.AppContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.awt.SunToolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.awt.CausedFocusEvent;
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
    43
import sun.awt.TimedWindowEvent;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * The default KeyboardFocusManager for AWT applications. Focus traversal is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * done in response to a Component's focus traversal keys, and using a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Container's FocusTraversalPolicy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Please see
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * How to Use the Focus Subsystem</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * a section in <em>The Java Tutorial</em>, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <a href="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @author David Mendenhall
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @see FocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @see Component#setFocusTraversalKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @see Component#getFocusTraversalKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
public class DefaultKeyboardFocusManager extends KeyboardFocusManager {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
    65
    private static final PlatformLogger focusLog = PlatformLogger.getLogger("java.awt.focus.DefaultKeyboardFocusManager");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // null weak references to not create too many objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static final WeakReference<Window> NULL_WINDOW_WR =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        new WeakReference<Window>(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private static final WeakReference<Component> NULL_COMPONENT_WR =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        new WeakReference<Component>(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private WeakReference<Window> realOppositeWindowWR = NULL_WINDOW_WR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private WeakReference<Component> realOppositeComponentWR = NULL_COMPONENT_WR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private int inSendMessage;
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
    75
    private LinkedList<KeyEvent> enqueuedKeyEvents = new LinkedList<KeyEvent>();
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
    76
    private LinkedList<TypeAheadMarker> typeAheadMarkers = new LinkedList<TypeAheadMarker>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private boolean consumeNextKeyTyped;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private static class TypeAheadMarker {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        long after;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        Component untilFocused;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        TypeAheadMarker(long after, Component untilFocused) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            this.after = after;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            this.untilFocused = untilFocused;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
         * Returns string representation of the marker
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            return ">>> Marker after " + after + " on " + untilFocused;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private Window getOwningFrameDialog(Window window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        while (window != null && !(window instanceof Frame ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                   window instanceof Dialog)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            window = (Window)window.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return window;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * This series of restoreFocus methods is used for recovering from a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * rejected focus or activation change. Rejections typically occur when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * the user attempts to focus a non-focusable Component or Window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private void restoreFocus(FocusEvent fe, Window newFocusedWindow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        Component realOppositeComponent = this.realOppositeComponentWR.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        Component vetoedComponent = fe.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (newFocusedWindow != null && restoreFocus(newFocusedWindow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                                     vetoedComponent, false))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        } else if (realOppositeComponent != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                   doRestoreFocus(realOppositeComponent, vetoedComponent, false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        } else if (fe.getOppositeComponent() != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                   doRestoreFocus(fe.getOppositeComponent(), vetoedComponent, false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        } else {
12651
8c69dd24bf07 7110683: Issues with some KeyboardFocusManager method
ant
parents: 5506
diff changeset
   120
            clearGlobalFocusOwnerPriv();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private void restoreFocus(WindowEvent we) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        Window realOppositeWindow = this.realOppositeWindowWR.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (realOppositeWindow != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            && restoreFocus(realOppositeWindow, null, false))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            // do nothing, everything is done in restoreFocus()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        } else if (we.getOppositeWindow() != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                   restoreFocus(we.getOppositeWindow(), null, false))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            // do nothing, everything is done in restoreFocus()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        } else {
12651
8c69dd24bf07 7110683: Issues with some KeyboardFocusManager method
ant
parents: 5506
diff changeset
   134
            clearGlobalFocusOwnerPriv();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private boolean restoreFocus(Window aWindow, Component vetoedComponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                                 boolean clearOnFailure) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        Component toFocus =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            KeyboardFocusManager.getMostRecentFocusOwner(aWindow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (toFocus != null && toFocus != vetoedComponent && doRestoreFocus(toFocus, vetoedComponent, false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        } else if (clearOnFailure) {
12651
8c69dd24bf07 7110683: Issues with some KeyboardFocusManager method
ant
parents: 5506
diff changeset
   145
            clearGlobalFocusOwnerPriv();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private boolean restoreFocus(Component toFocus, boolean clearOnFailure) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return doRestoreFocus(toFocus, null, clearOnFailure);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    private boolean doRestoreFocus(Component toFocus, Component vetoedComponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                                   boolean clearOnFailure)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    {
1171
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 441
diff changeset
   157
        if (toFocus != vetoedComponent && toFocus.isShowing() && toFocus.canBeFocusOwner() &&
441
f5da1014ed23 6607170: Focus not set by requestFocus
ant
parents: 419
diff changeset
   158
            toFocus.requestFocus(false, CausedFocusEvent.Cause.ROLLBACK))
f5da1014ed23 6607170: Focus not set by requestFocus
ant
parents: 419
diff changeset
   159
        {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        } else {
441
f5da1014ed23 6607170: Focus not set by requestFocus
ant
parents: 419
diff changeset
   162
            Component nextFocus = toFocus.getNextFocusCandidate();
f5da1014ed23 6607170: Focus not set by requestFocus
ant
parents: 419
diff changeset
   163
            if (nextFocus != null && nextFocus != vetoedComponent &&
f5da1014ed23 6607170: Focus not set by requestFocus
ant
parents: 419
diff changeset
   164
                nextFocus.requestFocusInWindow(CausedFocusEvent.Cause.ROLLBACK))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            } else if (clearOnFailure) {
12651
8c69dd24bf07 7110683: Issues with some KeyboardFocusManager method
ant
parents: 5506
diff changeset
   168
                clearGlobalFocusOwnerPriv();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                return 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
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * A special type of SentEvent which updates a counter in the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * KeyboardFocusManager if it is an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * DefaultKeyboardFocusManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    private static class DefaultKeyboardFocusManagerSentEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        extends SentEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
         * serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        private static final long serialVersionUID = -2924743257508701758L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        public DefaultKeyboardFocusManagerSentEvent(AWTEvent nested,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                                    AppContext toNotify) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            super(nested, toNotify);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        public final void dispatch() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            KeyboardFocusManager manager =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                KeyboardFocusManager.getCurrentKeyboardFocusManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            DefaultKeyboardFocusManager defaultManager =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                (manager instanceof DefaultKeyboardFocusManager)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                ? (DefaultKeyboardFocusManager)manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            if (defaultManager != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                synchronized (defaultManager) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    defaultManager.inSendMessage++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            super.dispatch();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            if (defaultManager != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                synchronized (defaultManager) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    defaultManager.inSendMessage--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Sends a synthetic AWTEvent to a Component. If the Component is in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * the current AppContext, then the event is immediately dispatched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * If the Component is in a different AppContext, then the event is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * posted to the other AppContext's EventQueue, and this method blocks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * until the event is handled or target AppContext is disposed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Returns true if successfuly dispatched event, false if failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * to dispatch.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    static boolean sendMessage(Component target, AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        e.isPosted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        AppContext myAppContext = AppContext.getAppContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        final AppContext targetAppContext = target.appContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        final SentEvent se =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            new DefaultKeyboardFocusManagerSentEvent(e, myAppContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (myAppContext == targetAppContext) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            se.dispatch();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            if (targetAppContext.isDisposed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            SunToolkit.postEvent(targetAppContext, se);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            if (EventQueue.isDispatchThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                EventDispatchThread edt = (EventDispatchThread)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                edt.pumpEvents(SentEvent.ID, new Conditional() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                        public boolean evaluate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                            return !se.dispatched && !targetAppContext.isDisposed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                synchronized (se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    while (!se.dispatched && !targetAppContext.isDisposed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                            se.wait(1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                        } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        return se.dispatched;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   263
    /*
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   264
     * Checks if the focus window event follows key events waiting in the type-ahead
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   265
     * queue (if any). This may happen when a user types ahead in the window, the client
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   266
     * listeners hang EDT for a while, and the user switches b/w toplevels. In that
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   267
     * case the focus window events may be dispatched before the type-ahead events
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   268
     * get handled. This may lead to wrong focus behavior and in order to avoid it,
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   269
     * the focus window events are reposted to the end of the event queue. See 6981400.
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   270
     */
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   271
    private boolean repostIfFollowsKeyEvents(WindowEvent e) {
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   272
        if (!(e instanceof TimedWindowEvent)) {
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   273
            return false;
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   274
        }
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   275
        TimedWindowEvent we = (TimedWindowEvent)e;
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   276
        long time = we.getWhen();
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   277
        synchronized (this) {
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   278
            for (KeyEvent ke: enqueuedKeyEvents) {
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   279
                if (time >= ke.getWhen()) {
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   280
                    SunToolkit.postEvent(AppContext.getAppContext(), new SequencedEvent(e));
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   281
                    return true;
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   282
                }
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   283
            }
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   284
        }
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   285
        return false;
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   286
    }
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   287
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * This method is called by the AWT event dispatcher requesting that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * current KeyboardFocusManager dispatch the specified event on its behalf.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * DefaultKeyboardFocusManagers dispatch all FocusEvents, all WindowEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * related to focus, and all KeyEvents. These events are dispatched based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * on the KeyboardFocusManager's notion of the focus owner and the focused
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * and active Windows, sometimes overriding the source of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * AWTEvent. If this method returns <code>false</code>, then the AWT event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * dispatcher will attempt to dispatch the event itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @param e the AWTEvent to be dispatched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @return <code>true</code> if this method dispatched the event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *         <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public boolean dispatchEvent(AWTEvent e) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
   303
        if (focusLog.isLoggable(PlatformLogger.FINE) && (e instanceof WindowEvent || e instanceof FocusEvent)) focusLog.fine("" + e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        switch (e.getID()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            case WindowEvent.WINDOW_GAINED_FOCUS: {
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   306
                if (repostIfFollowsKeyEvents((WindowEvent)e)) {
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   307
                    break;
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   308
                }
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   309
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                WindowEvent we = (WindowEvent)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                Window oldFocusedWindow = getGlobalFocusedWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                Window newFocusedWindow = we.getWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                if (newFocusedWindow == oldFocusedWindow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                if (!(newFocusedWindow.isFocusableWindow()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                      && newFocusedWindow.isVisible()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                      && newFocusedWindow.isDisplayable()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    // we can not accept focus on such window, so reject it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    restoreFocus(we);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                // If there exists a current focused window, then notify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                // that it has lost focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                if (oldFocusedWindow != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    boolean isEventDispatched =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                        sendMessage(oldFocusedWindow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                                new WindowEvent(oldFocusedWindow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                                                WindowEvent.WINDOW_LOST_FOCUS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                                                newFocusedWindow));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    // Failed to dispatch, clear by ourselfves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    if (!isEventDispatched) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                        setGlobalFocusOwner(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                        setGlobalFocusedWindow(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                // Because the native libraries do not post WINDOW_ACTIVATED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                // events, we need to synthesize one if the active Window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                // changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                Window newActiveWindow =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    getOwningFrameDialog(newFocusedWindow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                Window currentActiveWindow = getGlobalActiveWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                if (newActiveWindow != currentActiveWindow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    sendMessage(newActiveWindow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                                new WindowEvent(newActiveWindow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                                                WindowEvent.WINDOW_ACTIVATED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                                                currentActiveWindow));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    if (newActiveWindow != getGlobalActiveWindow()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                        // Activation change was rejected. Unlikely, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                        // possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                        restoreFocus(we);
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
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                setGlobalFocusedWindow(newFocusedWindow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                if (newFocusedWindow != getGlobalFocusedWindow()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    // Focus change was rejected. Will happen if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    // newFocusedWindow is not a focusable Window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    restoreFocus(we);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                // Restore focus to the Component which last held it. We do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                // this here so that client code can override our choice in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                // a WINDOW_GAINED_FOCUS handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                // Make sure that the focus change request doesn't change the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                // focused Window in case we are no longer the focused Window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                // when the request is handled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                if (inSendMessage == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    // Identify which Component should initially gain focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    // in the Window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    // * If we're in SendMessage, then this is a synthetic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                    //   WINDOW_GAINED_FOCUS message which was generated by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    //   the FOCUS_GAINED handler. Allow the Component to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    //   which the FOCUS_GAINED message was targeted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    //   receive the focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    // * Otherwise, look up the correct Component here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    //   We don't use Window.getMostRecentFocusOwner because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    //   window is focused now and 'null' will be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                    // Calculating of most recent focus owner and focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                    // request should be synchronized on KeyboardFocusManager.class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                    // to prevent from thread race when user will request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    // focus between calculation and our request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    // But if focus transfer is synchronous, this synchronization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                    // may cause deadlock, thus we don't synchronize this block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    Component toFocus = KeyboardFocusManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                        getMostRecentFocusOwner(newFocusedWindow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    if ((toFocus == null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                        newFocusedWindow.isFocusableWindow())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                        toFocus = newFocusedWindow.getFocusTraversalPolicy().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                            getInitialComponent(newFocusedWindow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    Component tempLost = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    synchronized(KeyboardFocusManager.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                        tempLost = newFocusedWindow.setTemporaryLostComponent(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    // The component which last has the focus when this window was focused
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    // should receive focus first
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
   410
                    if (focusLog.isLoggable(PlatformLogger.FINER)) {
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
   411
                        focusLog.finer("tempLost {0}, toFocus {1}",
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
   412
                                       tempLost, toFocus);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                    if (tempLost != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                        tempLost.requestFocusInWindow(CausedFocusEvent.Cause.ACTIVATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    if (toFocus != null && toFocus != tempLost) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                        // If there is a component which requested focus when this window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                        // was inactive it expects to receive focus after activation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                        toFocus.requestFocusInWindow(CausedFocusEvent.Cause.ACTIVATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                Window realOppositeWindow = this.realOppositeWindowWR.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                if (realOppositeWindow != we.getOppositeWindow()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    we = new WindowEvent(newFocusedWindow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                                         WindowEvent.WINDOW_GAINED_FOCUS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                                         realOppositeWindow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                return typeAheadAssertions(newFocusedWindow, we);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            case WindowEvent.WINDOW_ACTIVATED: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                WindowEvent we = (WindowEvent)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                Window oldActiveWindow = getGlobalActiveWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                Window newActiveWindow = we.getWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                if (oldActiveWindow == newActiveWindow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                // If there exists a current active window, then notify it that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                // it has lost activation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                if (oldActiveWindow != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    boolean isEventDispatched =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                        sendMessage(oldActiveWindow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                                new WindowEvent(oldActiveWindow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                                                WindowEvent.WINDOW_DEACTIVATED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                                                newActiveWindow));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    // Failed to dispatch, clear by ourselfves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    if (!isEventDispatched) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                        setGlobalActiveWindow(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    if (getGlobalActiveWindow() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                        // Activation change was rejected. Unlikely, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                        // possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                setGlobalActiveWindow(newActiveWindow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                if (newActiveWindow != getGlobalActiveWindow()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    // Activation change was rejected. Unlikely, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    // possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                return typeAheadAssertions(newActiveWindow, we);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            case FocusEvent.FOCUS_GAINED: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                FocusEvent fe = (FocusEvent)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                CausedFocusEvent.Cause cause = (fe instanceof CausedFocusEvent) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                    ((CausedFocusEvent)fe).getCause() : CausedFocusEvent.Cause.UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                Component oldFocusOwner = getGlobalFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                Component newFocusOwner = fe.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                if (oldFocusOwner == newFocusOwner) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
   479
                    if (focusLog.isLoggable(PlatformLogger.FINE)) {
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
   480
                        focusLog.fine("Skipping {0} because focus owner is the same", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    // We can't just drop the event - there could be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                    // type-ahead markers associated with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                    dequeueKeyEvents(-1, newFocusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                // If there exists a current focus owner, then notify it that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                // it has lost focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                if (oldFocusOwner != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                    boolean isEventDispatched =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                        sendMessage(oldFocusOwner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                                    new CausedFocusEvent(oldFocusOwner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                                                   FocusEvent.FOCUS_LOST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                                                   fe.isTemporary(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                                                   newFocusOwner, cause));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    // Failed to dispatch, clear by ourselfves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    if (!isEventDispatched) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                        setGlobalFocusOwner(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                        if (!fe.isTemporary()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                            setGlobalPermanentFocusOwner(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                // Because the native windowing system has a different notion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                // of the current focus and activation states, it is possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                // that a Component outside of the focused Window receives a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                // FOCUS_GAINED event. We synthesize a WINDOW_GAINED_FOCUS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                // event in that case.
2451
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1188
diff changeset
   511
                final Window newFocusedWindow = SunToolkit.getContainingWindow(newFocusOwner);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                final Window currentFocusedWindow = getGlobalFocusedWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                if (newFocusedWindow != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                    newFocusedWindow != currentFocusedWindow)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                    sendMessage(newFocusedWindow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                                new WindowEvent(newFocusedWindow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                                        WindowEvent.WINDOW_GAINED_FOCUS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                                                currentFocusedWindow));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                    if (newFocusedWindow != getGlobalFocusedWindow()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                        // Focus change was rejected. Will happen if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                        // newFocusedWindow is not a focusable Window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                        // Need to recover type-ahead, but don't bother
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                        // restoring focus. That was done by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                        // WINDOW_GAINED_FOCUS handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                        dequeueKeyEvents(-1, newFocusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
1171
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 441
diff changeset
   532
                if (!(newFocusOwner.isFocusable() && newFocusOwner.isShowing() &&
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 441
diff changeset
   533
                    // Refuse focus on a disabled component if the focus event
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 441
diff changeset
   534
                    // isn't of UNKNOWN reason (i.e. not a result of a direct request
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 441
diff changeset
   535
                    // but traversal, activation or system generated).
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 441
diff changeset
   536
                    (newFocusOwner.isEnabled() || cause.equals(CausedFocusEvent.Cause.UNKNOWN))))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                    // we should not accept focus on such component, so reject it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                    dequeueKeyEvents(-1, newFocusOwner);
441
f5da1014ed23 6607170: Focus not set by requestFocus
ant
parents: 419
diff changeset
   540
                    if (KeyboardFocusManager.isAutoFocusTransferEnabled()) {
f5da1014ed23 6607170: Focus not set by requestFocus
ant
parents: 419
diff changeset
   541
                        // If FOCUS_GAINED is for a disposed component (however
f5da1014ed23 6607170: Focus not set by requestFocus
ant
parents: 419
diff changeset
   542
                        // it shouldn't happen) its toplevel parent is null. In this
f5da1014ed23 6607170: Focus not set by requestFocus
ant
parents: 419
diff changeset
   543
                        // case we have to try to restore focus in the current focused
f5da1014ed23 6607170: Focus not set by requestFocus
ant
parents: 419
diff changeset
   544
                        // window (for the details: 6607170).
f5da1014ed23 6607170: Focus not set by requestFocus
ant
parents: 419
diff changeset
   545
                        if (newFocusedWindow == null) {
f5da1014ed23 6607170: Focus not set by requestFocus
ant
parents: 419
diff changeset
   546
                            restoreFocus(fe, currentFocusedWindow);
f5da1014ed23 6607170: Focus not set by requestFocus
ant
parents: 419
diff changeset
   547
                        } else {
f5da1014ed23 6607170: Focus not set by requestFocus
ant
parents: 419
diff changeset
   548
                            restoreFocus(fe, newFocusedWindow);
f5da1014ed23 6607170: Focus not set by requestFocus
ant
parents: 419
diff changeset
   549
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                setGlobalFocusOwner(newFocusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                if (newFocusOwner != getGlobalFocusOwner()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                    // Focus change was rejected. Will happen if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                    // newFocusOwner is not focus traversable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                    dequeueKeyEvents(-1, newFocusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                    if (KeyboardFocusManager.isAutoFocusTransferEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                        restoreFocus(fe, (Window)newFocusedWindow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                if (!fe.isTemporary()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                    setGlobalPermanentFocusOwner(newFocusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                    if (newFocusOwner != getGlobalPermanentFocusOwner()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                        // Focus change was rejected. Unlikely, but possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                        dequeueKeyEvents(-1, newFocusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                        if (KeyboardFocusManager.isAutoFocusTransferEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                            restoreFocus(fe, (Window)newFocusedWindow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                setNativeFocusOwner(getHeavyweight(newFocusOwner));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                Component realOppositeComponent = this.realOppositeComponentWR.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                if (realOppositeComponent != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                    realOppositeComponent != fe.getOppositeComponent()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                    fe = new CausedFocusEvent(newFocusOwner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                                        FocusEvent.FOCUS_GAINED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                                        fe.isTemporary(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                                        realOppositeComponent, cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                    ((AWTEvent) fe).isPosted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                return typeAheadAssertions(newFocusOwner, fe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            case FocusEvent.FOCUS_LOST: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                FocusEvent fe = (FocusEvent)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                Component currentFocusOwner = getGlobalFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                if (currentFocusOwner == null) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
   597
                    if (focusLog.isLoggable(PlatformLogger.FINE))
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
   598
                        focusLog.fine("Skipping {0} because focus owner is null", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                // Ignore cases where a Component loses focus to itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                // If we make a mistake because of retargeting, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                // FOCUS_GAINED handler will correct it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                if (currentFocusOwner == fe.getOppositeComponent()) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
   605
                    if (focusLog.isLoggable(PlatformLogger.FINE))
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
   606
                        focusLog.fine("Skipping {0} because current focus owner is equal to opposite", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                setGlobalFocusOwner(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                if (getGlobalFocusOwner() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                    // Focus change was rejected. Unlikely, but possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                    restoreFocus(currentFocusOwner, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                if (!fe.isTemporary()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                    setGlobalPermanentFocusOwner(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                    if (getGlobalPermanentFocusOwner() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                        // Focus change was rejected. Unlikely, but possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                        restoreFocus(currentFocusOwner, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                    Window owningWindow = currentFocusOwner.getContainingWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                    if (owningWindow != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                        owningWindow.setTemporaryLostComponent(currentFocusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                setNativeFocusOwner(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                fe.setSource(currentFocusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                realOppositeComponentWR = (fe.getOppositeComponent() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                    ? new WeakReference<Component>(currentFocusOwner)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                    : NULL_COMPONENT_WR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                return typeAheadAssertions(currentFocusOwner, fe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            case WindowEvent.WINDOW_DEACTIVATED: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                WindowEvent we = (WindowEvent)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                Window currentActiveWindow = getGlobalActiveWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                if (currentActiveWindow == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                if (currentActiveWindow != e.getSource()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                    // The event is lost in time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                    // Allow listeners to precess the event but do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                    // change any global states
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                setGlobalActiveWindow(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                if (getGlobalActiveWindow() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                    // Activation change was rejected. Unlikely, but possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                we.setSource(currentActiveWindow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                return typeAheadAssertions(currentActiveWindow, we);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            case WindowEvent.WINDOW_LOST_FOCUS: {
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   669
                if (repostIfFollowsKeyEvents((WindowEvent)e)) {
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   670
                    break;
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   671
                }
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   672
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                WindowEvent we = (WindowEvent)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                Window currentFocusedWindow = getGlobalFocusedWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                Window losingFocusWindow = we.getWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                Window activeWindow = getGlobalActiveWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                Window oppositeWindow = we.getOppositeWindow();
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
   678
                if (focusLog.isLoggable(PlatformLogger.FINE))
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
   679
                    focusLog.fine("Active {0}, Current focused {1}, losing focus {2} opposite {3}",
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
   680
                                  activeWindow, currentFocusedWindow,
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
   681
                                  losingFocusWindow, oppositeWindow);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                if (currentFocusedWindow == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                // Special case -- if the native windowing system posts an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                // event claiming that the active Window has lost focus to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                // focused Window, then discard the event. This is an artifact
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                // of the native windowing system not knowing which Window is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                // really focused.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                if (inSendMessage == 0 && losingFocusWindow == activeWindow &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                    oppositeWindow == currentFocusedWindow)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                Component currentFocusOwner = getGlobalFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                if (currentFocusOwner != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                    // The focus owner should always receive a FOCUS_LOST event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                    // before the Window is defocused.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                    Component oppositeComp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                    if (oppositeWindow != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                        oppositeComp = oppositeWindow.getTemporaryLostComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                        if (oppositeComp == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                            oppositeComp = oppositeWindow.getMostRecentFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                    if (oppositeComp == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                        oppositeComp = oppositeWindow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                    sendMessage(currentFocusOwner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                                new CausedFocusEvent(currentFocusOwner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                                               FocusEvent.FOCUS_LOST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                                               true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                                               oppositeComp, CausedFocusEvent.Cause.ACTIVATION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                setGlobalFocusedWindow(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                if (getGlobalFocusedWindow() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                    // Focus change was rejected. Unlikely, but possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                    restoreFocus(currentFocusedWindow, null, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                we.setSource(currentFocusedWindow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                realOppositeWindowWR = (oppositeWindow != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                    ? new WeakReference<Window>(currentFocusedWindow)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                    : NULL_WINDOW_WR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                typeAheadAssertions(currentFocusedWindow, we);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                if (oppositeWindow == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                    // Then we need to deactive the active Window as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                    // No need to synthesize in other cases, because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                    // WINDOW_ACTIVATED will handle it if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                    sendMessage(activeWindow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                                new WindowEvent(activeWindow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                                                WindowEvent.WINDOW_DEACTIVATED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                                                null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                    if (getGlobalActiveWindow() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                        // Activation change was rejected. Unlikely,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                        // but possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                        restoreFocus(currentFocusedWindow, null, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            case KeyEvent.KEY_TYPED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            case KeyEvent.KEY_PRESSED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            case KeyEvent.KEY_RELEASED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                return typeAheadAssertions(null, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * Called by <code>dispatchEvent</code> if no other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * KeyEventDispatcher in the dispatcher chain dispatched the KeyEvent, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * if no other KeyEventDispatchers are registered. If the event has not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * been consumed, its target is enabled, and the focus owner is not null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * this method dispatches the event to its target. This method will also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * subsequently dispatch the event to all registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * KeyEventPostProcessors. After all this operations are finished,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * the event is passed to peers for processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * In all cases, this method returns <code>true</code>, since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * DefaultKeyboardFocusManager is designed so that neither
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * <code>dispatchEvent</code>, nor the AWT event dispatcher, should take
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * further action on the event in any situation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * @param e the KeyEvent to be dispatched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * @return <code>true</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * @see Component#dispatchEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    public boolean dispatchKeyEvent(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        Component focusOwner = (((AWTEvent)e).isPosted) ? getFocusOwner() : e.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
1171
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 441
diff changeset
   782
        if (focusOwner != null && focusOwner.isShowing() && focusOwner.canBeFocusOwner()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            if (!e.isConsumed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                Component comp = e.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                if (comp != null && comp.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                    redispatchEvent(comp, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        boolean stopPostProcessing = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        java.util.List processors = getKeyEventPostProcessors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        if (processors != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            for (java.util.Iterator iter = processors.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                 !stopPostProcessing && iter.hasNext(); )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                stopPostProcessing = (((KeyEventPostProcessor)(iter.next())).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                            postProcessKeyEvent(e));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        if (!stopPostProcessing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            postProcessKeyEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        // Allow the peer to process KeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        Component source = e.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        ComponentPeer peer = source.getPeer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        if (peer == null || peer instanceof LightweightPeer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            // if focus owner is lightweight then its native container
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            // processes event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            Container target = source.getNativeContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            if (target != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                peer = target.getPeer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            peer.handleEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * This method will be called by <code>dispatchKeyEvent</code>. It will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * handle any unconsumed KeyEvents that map to an AWT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * <code>MenuShortcut</code> by consuming the event and activating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * shortcut.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * @param e the KeyEvent to post-process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * @return <code>true</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * @see #dispatchKeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * @see MenuShortcut
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    public boolean postProcessKeyEvent(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        if (!e.isConsumed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            Component target = e.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            Container p = (Container)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                (target instanceof Container ? target : target.getParent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            if (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                p.postProcessKeyEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    private void pumpApprovedKeyEvents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        KeyEvent ke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            ke = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                if (enqueuedKeyEvents.size() != 0) {
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   852
                    ke = enqueuedKeyEvents.getFirst();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                    if (typeAheadMarkers.size() != 0) {
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   854
                        TypeAheadMarker marker = typeAheadMarkers.getFirst();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                        // Fixed 5064013: may appears that the events have the same time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                        // if (ke.getWhen() >= marker.after) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                        // The fix is rolled out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                        if (ke.getWhen() > marker.after) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                            ke = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                    if (ke != null) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
   864
                        focusLog.finer("Pumping approved event {0}", ke);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                        enqueuedKeyEvents.removeFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            if (ke != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                preDispatchKeyEvent(ke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        } while (ke != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * Dumps the list of type-ahead queue markers to stderr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    void dumpMarkers() {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
   879
        if (focusLog.isLoggable(PlatformLogger.FINEST)) {
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
   880
            focusLog.finest(">>> Markers dump, time: {0}", System.currentTimeMillis());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                if (typeAheadMarkers.size() != 0) {
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   883
                    Iterator<TypeAheadMarker> iter = typeAheadMarkers.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                    while (iter.hasNext()) {
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   885
                        TypeAheadMarker marker = iter.next();
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
   886
                        focusLog.finest("    {0}", marker);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    private boolean typeAheadAssertions(Component target, AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        // Clear any pending events here as well as in the FOCUS_GAINED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        // handler. We need this call here in case a marker was removed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        // response to a call to dequeueKeyEvents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        pumpApprovedKeyEvents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        switch (e.getID()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            case KeyEvent.KEY_TYPED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            case KeyEvent.KEY_PRESSED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            case KeyEvent.KEY_RELEASED: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                KeyEvent ke = (KeyEvent)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                    if (e.isPosted && typeAheadMarkers.size() != 0) {
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   907
                        TypeAheadMarker marker = typeAheadMarkers.getFirst();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                        // Fixed 5064013: may appears that the events have the same time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                        // if (ke.getWhen() >= marker.after) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                        // The fix is rolled out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                        if (ke.getWhen() > marker.after) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
   913
                            focusLog.finer("Storing event {0} because of marker {1}", ke, marker);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                            enqueuedKeyEvents.addLast(ke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                // KeyEvent was posted before focus change request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                return preDispatchKeyEvent(ke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            case FocusEvent.FOCUS_GAINED:
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
   925
                focusLog.finest("Markers before FOCUS_GAINED on {0}", target);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                dumpMarkers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                // Search the marker list for the first marker tied to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                // the Component which just gained focus. Then remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                // that marker, any markers which immediately follow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                // and are tied to the same component, and all markers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                // that preceed it. This handles the case where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                // multiple focus requests were made for the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                // Component in a row and when we lost some of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                // earlier requests. Since FOCUS_GAINED events will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                // not be generated for these additional requests, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                // need to clear those markers too.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                    boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                    if (hasMarker(target)) {
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   940
                        for (Iterator<TypeAheadMarker> iter = typeAheadMarkers.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                             iter.hasNext(); )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                        {
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   943
                            if (iter.next().untilFocused == target) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                                found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                            } else if (found) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                            iter.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                        // Exception condition - event without marker
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
   952
                        focusLog.finer("Event without marker {0}", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                }
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
   955
                focusLog.finest("Markers after FOCUS_GAINED");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                dumpMarkers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                redispatchEvent(target, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                // Now, dispatch any pending KeyEvents which have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                // released because of the FOCUS_GAINED event so that we don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                // have to wait for another event to be posted to the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                pumpApprovedKeyEvents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                redispatchEvent(target, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * Returns true if there are some marker associated with component <code>comp</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * in a markers' queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    private boolean hasMarker(Component comp) {
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   978
        for (Iterator<TypeAheadMarker> iter = typeAheadMarkers.iterator(); iter.hasNext(); ) {
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   979
            if (iter.next().untilFocused == comp) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * Clears markers queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
    void clearMarkers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            typeAheadMarkers.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    private boolean preDispatchKeyEvent(KeyEvent ke) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        if (((AWTEvent) ke).isPosted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            Component focusOwner = getFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            ke.setSource(((focusOwner != null) ? focusOwner : getFocusedWindow()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        if (ke.getSource() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
  1005
        // Explicitly set the key event timestamp here (not in Component.dispatchEventImpl):
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
  1006
        // - A key event is anyway passed to this method which starts its actual dispatching.
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
  1007
        // - If a key event is put to the type ahead queue, its time stamp should not be registered
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
  1008
        //   until its dispatching actually starts (by this method).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        EventQueue.setCurrentEventAndMostRecentTime(ke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
         * Fix for 4495473.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
         * This fix allows to correctly dispatch events when native
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
         * event proxying mechanism is active.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
         * If it is active we should redispatch key events after
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
         * we detected its correct target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        if (KeyboardFocusManager.isProxyActive(ke)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            Component source = (Component)ke.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            Container target = source.getNativeContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            if (target != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                ComponentPeer peer = target.getPeer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                    peer.handleEvent(ke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                     * Fix for 4478780 - consume event after it was dispatched by peer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                    ke.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        java.util.List dispatchers = getKeyEventDispatchers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        if (dispatchers != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            for (java.util.Iterator iter = dispatchers.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                 iter.hasNext(); )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
             {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                 if (((KeyEventDispatcher)(iter.next())).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                     dispatchKeyEvent(ke))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                     return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        return dispatchKeyEvent(ke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * @param e is a KEY_PRESSED event that can be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     *          to track the next KEY_TYPED related.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    private void consumeNextKeyTyped(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        consumeNextKeyTyped = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    private void consumeTraversalKey(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        e.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        consumeNextKeyTyped = (e.getID() == KeyEvent.KEY_PRESSED) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                              !e.isActionKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * return true if event was consumed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
    private boolean consumeProcessedKeyEvent(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        if ((e.getID() == KeyEvent.KEY_TYPED) && consumeNextKeyTyped) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            e.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            consumeNextKeyTyped = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * This method initiates a focus traversal operation if and only if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * KeyEvent represents a focus traversal key for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * focusedComponent. It is expected that focusedComponent is the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * focus owner, although this need not be the case. If it is not,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * focus traversal will nevertheless proceed as if focusedComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * were the focus owner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * @param focusedComponent the Component that is the basis for a focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     *        traversal operation if the specified event represents a focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     *        traversal key for the Component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * @param e the event that may represent a focus traversal key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
    public void processKeyEvent(Component focusedComponent, KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        // consume processed event if needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        if (consumeProcessedKeyEvent(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        // KEY_TYPED events cannot be focus traversal keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        if (e.getID() == KeyEvent.KEY_TYPED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        if (focusedComponent.getFocusTraversalKeysEnabled() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            !e.isConsumed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            AWTKeyStroke stroke = AWTKeyStroke.getAWTKeyStrokeForEvent(e),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                oppStroke = AWTKeyStroke.getAWTKeyStroke(stroke.getKeyCode(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                                                 stroke.getModifiers(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                                                 !stroke.isOnKeyRelease());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            Set toTest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            boolean contains, containsOpp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            toTest = focusedComponent.getFocusTraversalKeys(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            contains = toTest.contains(stroke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            containsOpp = toTest.contains(oppStroke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            if (contains || containsOpp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                consumeTraversalKey(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                if (contains) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                    focusNextComponent(focusedComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                return;
419
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents: 134
diff changeset
  1120
            } else if (e.getID() == KeyEvent.KEY_PRESSED) {
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents: 134
diff changeset
  1121
                // Fix for 6637607: consumeNextKeyTyped should be reset.
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents: 134
diff changeset
  1122
                consumeNextKeyTyped = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            toTest = focusedComponent.getFocusTraversalKeys(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            contains = toTest.contains(stroke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            containsOpp = toTest.contains(oppStroke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            if (contains || containsOpp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                consumeTraversalKey(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                if (contains) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                    focusPreviousComponent(focusedComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
            toTest = focusedComponent.getFocusTraversalKeys(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            contains = toTest.contains(stroke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            containsOpp = toTest.contains(oppStroke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            if (contains || containsOpp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                consumeTraversalKey(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                if (contains) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                    upFocusCycle(focusedComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            if (!((focusedComponent instanceof Container) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                  ((Container)focusedComponent).isFocusCycleRoot())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            toTest = focusedComponent.getFocusTraversalKeys(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
                KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            contains = toTest.contains(stroke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            containsOpp = toTest.contains(oppStroke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            if (contains || containsOpp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                consumeTraversalKey(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                if (contains) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                    downFocusCycle((Container)focusedComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * Delays dispatching of KeyEvents until the specified Component becomes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * the focus owner. KeyEvents with timestamps later than the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * timestamp will be enqueued until the specified Component receives a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * FOCUS_GAINED event, or the AWT cancels the delay request by invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * <code>dequeueKeyEvents</code> or <code>discardKeyEvents</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * @param after timestamp of current event, or the current, system time if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     *        the current event has no timestamp, or the AWT cannot determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     *        which event is currently being handled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * @param untilFocused Component which will receive a FOCUS_GAINED event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     *        before any pending KeyEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * @see #dequeueKeyEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     * @see #discardKeyEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
    protected synchronized void enqueueKeyEvents(long after,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
                                                 Component untilFocused) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        if (untilFocused == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
  1191
        focusLog.finer("Enqueue at {0} for {1}",
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
  1192
                       after, untilFocused);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        int insertionIndex = 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
            i = typeAheadMarkers.size();
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
  1196
        ListIterator<TypeAheadMarker> iter = typeAheadMarkers.listIterator(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        for (; i > 0; i--) {
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
  1199
            TypeAheadMarker marker = iter.previous();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            if (marker.after <= after) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                insertionIndex = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        typeAheadMarkers.add(insertionIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                             new TypeAheadMarker(after, untilFocused));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * Releases for normal dispatching to the current focus owner all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     * KeyEvents which were enqueued because of a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     * <code>enqueueKeyEvents</code> with the same timestamp and Component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     * If the given timestamp is less than zero, the outstanding enqueue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * request for the given Component with the <b>oldest</b> timestamp (if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * any) should be cancelled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * @param after the timestamp specified in the call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     *        <code>enqueueKeyEvents</code>, or any value < 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * @param untilFocused the Component specified in the call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     *        <code>enqueueKeyEvents</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     * @see #enqueueKeyEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     * @see #discardKeyEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
    protected synchronized void dequeueKeyEvents(long after,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                                                 Component untilFocused) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        if (untilFocused == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
  1231
        focusLog.finer("Dequeue at {0} for {1}",
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
  1232
                       after, untilFocused);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        TypeAheadMarker marker;
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
  1235
        ListIterator<TypeAheadMarker> iter = typeAheadMarkers.listIterator
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
            ((after >= 0) ? typeAheadMarkers.size() : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        if (after < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
            while (iter.hasNext()) {
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
  1240
                marker = iter.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                if (marker.untilFocused == untilFocused)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                    iter.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
            while (iter.hasPrevious()) {
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
  1249
                marker = iter.previous();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
                if (marker.untilFocused == untilFocused &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
                    marker.after == after)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                    iter.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * Discards all KeyEvents which were enqueued because of one or more calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     * to <code>enqueueKeyEvents</code> with the specified Component, or one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * its descendants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * @param comp the Component specified in one or more calls to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     *        <code>enqueueKeyEvents</code>, or a parent of such a Component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * @see #enqueueKeyEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * @see #dequeueKeyEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
    protected synchronized void discardKeyEvents(Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        if (comp == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        long start = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
  1277
        for (Iterator<TypeAheadMarker> iter = typeAheadMarkers.iterator(); iter.hasNext(); ) {
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
  1278
            TypeAheadMarker marker = iter.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
            Component toTest = marker.untilFocused;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
            boolean match = (toTest == comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
            while (!match && toTest != null && !(toTest instanceof Window)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                toTest = toTest.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                match = (toTest == comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
            if (match) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                if (start < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                    start = marker.after;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                iter.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
            } else if (start >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                purgeStampedEvents(start, marker.after);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                start = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        purgeStampedEvents(start, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
    // Notes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
    //   * must be called inside a synchronized block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
    //   * if 'start' is < 0, then this function does nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
    //   * if 'end' is < 0, then all KeyEvents from 'start' to the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
    //     queue will be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
    private void purgeStampedEvents(long start, long end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        if (start < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
  1309
        for (Iterator<KeyEvent> iter = enqueuedKeyEvents.iterator(); iter.hasNext(); ) {
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
  1310
            KeyEvent ke = iter.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            long time = ke.getWhen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            if (start < time && (end < 0 || time <= end)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                iter.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
            if (end >= 0 && time > end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     * Focuses the Component before aComponent, typically based on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     * FocusTraversalPolicy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     * @param aComponent the Component that is the basis for the focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     *        traversal operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     * @see FocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     * @see Component#transferFocusBackward
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
    public void focusPreviousComponent(Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
        if (aComponent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
            aComponent.transferFocusBackward();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     * Focuses the Component after aComponent, typically based on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     * FocusTraversalPolicy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
     * @param aComponent the Component that is the basis for the focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
     *        traversal operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     * @see FocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     * @see Component#transferFocus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
    public void focusNextComponent(Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
        if (aComponent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
            aComponent.transferFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
     * Moves the focus up one focus traversal cycle. Typically, the focus owner
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
     * is set to aComponent's focus cycle root, and the current focus cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     * root is set to the new focus owner's focus cycle root. If, however,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     * aComponent's focus cycle root is a Window, then the focus owner is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     * to the focus cycle root's default Component to focus, and the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     * focus cycle root is unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     * @param aComponent the Component that is the basis for the focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
     *        traversal operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     * @see Component#transferFocusUpCycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
    public void upFocusCycle(Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        if (aComponent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
            aComponent.transferFocusUpCycle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     * Moves the focus down one focus traversal cycle. If aContainer is a focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     * cycle root, then the focus owner is set to aContainer's default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     * Component to focus, and the current focus cycle root is set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     * aContainer. If aContainer is not a focus cycle root, then no focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     * traversal operation occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     * @param aContainer the Container that is the basis for the focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
     *        traversal operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
     * @see Container#transferFocusDownCycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
    public void downFocusCycle(Container aContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        if (aContainer != null && aContainer.isFocusCycleRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
            aContainer.transferFocusDownCycle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
}