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