jdk/src/java.desktop/share/classes/java/awt/DefaultKeyboardFocusManager.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 23276 jdk/src/share/classes/java/awt/DefaultKeyboardFocusManager.java@add6f5c93bc6
child 26749 b6598aa90114
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20455
diff changeset
     2
 * Copyright (c) 2000, 2014, 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
20455
f6f9a0c2796b 8020688: Broken links in documentation at http://docs.oracle.com/javase/6/docs/api/index.
mcherkas
parents: 20172
diff changeset
    52
 * <a href="http://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html">
2
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) {
18128
3492cc60d273 8015454: java/awt/Focus/TypeAhead/TestFocusFreeze.java hangs with jdk8 since b56
ant
parents: 17893
diff changeset
   288
            KeyEvent ke = enqueuedKeyEvents.isEmpty() ? null : enqueuedKeyEvents.getFirst();
3492cc60d273 8015454: java/awt/Focus/TypeAhead/TestFocusFreeze.java hangs with jdk8 since b56
ant
parents: 17893
diff changeset
   289
            if (ke != null && time >= ke.getWhen()) {
23239
b9f47a000ebd 8023148: [macosx] java.util.NoSuchElementException at java.util.LinkedList.getFirst
pchelko
parents: 20455
diff changeset
   290
                TypeAheadMarker marker = typeAheadMarkers.isEmpty() ? null : typeAheadMarkers.getFirst();
18128
3492cc60d273 8015454: java/awt/Focus/TypeAhead/TestFocusFreeze.java hangs with jdk8 since b56
ant
parents: 17893
diff changeset
   291
                if (marker != null) {
3492cc60d273 8015454: java/awt/Focus/TypeAhead/TestFocusFreeze.java hangs with jdk8 since b56
ant
parents: 17893
diff changeset
   292
                    Window toplevel = marker.untilFocused.getContainingWindow();
3492cc60d273 8015454: java/awt/Focus/TypeAhead/TestFocusFreeze.java hangs with jdk8 since b56
ant
parents: 17893
diff changeset
   293
                    // Check that the component awaiting focus belongs to
3492cc60d273 8015454: java/awt/Focus/TypeAhead/TestFocusFreeze.java hangs with jdk8 since b56
ant
parents: 17893
diff changeset
   294
                    // the current focused window. See 8015454.
3492cc60d273 8015454: java/awt/Focus/TypeAhead/TestFocusFreeze.java hangs with jdk8 since b56
ant
parents: 17893
diff changeset
   295
                    if (toplevel != null && toplevel.isFocused()) {
3492cc60d273 8015454: java/awt/Focus/TypeAhead/TestFocusFreeze.java hangs with jdk8 since b56
ant
parents: 17893
diff changeset
   296
                        SunToolkit.postEvent(AppContext.getAppContext(), new SequencedEvent(e));
3492cc60d273 8015454: java/awt/Focus/TypeAhead/TestFocusFreeze.java hangs with jdk8 since b56
ant
parents: 17893
diff changeset
   297
                        return true;
3492cc60d273 8015454: java/awt/Focus/TypeAhead/TestFocusFreeze.java hangs with jdk8 since b56
ant
parents: 17893
diff changeset
   298
                    }
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   299
                }
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   300
            }
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   301
        }
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   302
        return false;
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   303
    }
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   304
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * This method is called by the AWT event dispatcher requesting that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * current KeyboardFocusManager dispatch the specified event on its behalf.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * DefaultKeyboardFocusManagers dispatch all FocusEvents, all WindowEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * related to focus, and all KeyEvents. These events are dispatched based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * on the KeyboardFocusManager's notion of the focus owner and the focused
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * and active Windows, sometimes overriding the source of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * AWTEvent. If this method returns <code>false</code>, then the AWT event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * dispatcher will attempt to dispatch the event itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @param e the AWTEvent to be dispatched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @return <code>true</code> if this method dispatched the event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *         <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    public boolean dispatchEvent(AWTEvent e) {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 17893
diff changeset
   320
        if (focusLog.isLoggable(PlatformLogger.Level.FINE) && (e instanceof WindowEvent || e instanceof FocusEvent)) {
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 13656
diff changeset
   321
            focusLog.fine("" + e);
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 13656
diff changeset
   322
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        switch (e.getID()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            case WindowEvent.WINDOW_GAINED_FOCUS: {
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   325
                if (repostIfFollowsKeyEvents((WindowEvent)e)) {
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   326
                    break;
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   327
                }
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   328
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                WindowEvent we = (WindowEvent)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                Window oldFocusedWindow = getGlobalFocusedWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                Window newFocusedWindow = we.getWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                if (newFocusedWindow == oldFocusedWindow) {
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                if (!(newFocusedWindow.isFocusableWindow()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                      && newFocusedWindow.isVisible()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                      && newFocusedWindow.isDisplayable()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    // we can not accept focus on such window, so reject it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    restoreFocus(we);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                // If there exists a current focused window, then notify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                // that it has lost focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                if (oldFocusedWindow != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    boolean isEventDispatched =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                        sendMessage(oldFocusedWindow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                                new WindowEvent(oldFocusedWindow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                                                WindowEvent.WINDOW_LOST_FOCUS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                                                newFocusedWindow));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    // Failed to dispatch, clear by ourselfves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    if (!isEventDispatched) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                        setGlobalFocusOwner(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                        setGlobalFocusedWindow(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                // Because the native libraries do not post WINDOW_ACTIVATED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                // events, we need to synthesize one if the active Window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                // changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                Window newActiveWindow =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    getOwningFrameDialog(newFocusedWindow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                Window currentActiveWindow = getGlobalActiveWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                if (newActiveWindow != currentActiveWindow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    sendMessage(newActiveWindow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                                new WindowEvent(newActiveWindow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                                                WindowEvent.WINDOW_ACTIVATED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                                                currentActiveWindow));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    if (newActiveWindow != getGlobalActiveWindow()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                        // Activation change was rejected. Unlikely, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                        // possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                        restoreFocus(we);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                setGlobalFocusedWindow(newFocusedWindow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                if (newFocusedWindow != getGlobalFocusedWindow()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    // Focus change was rejected. Will happen if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    // newFocusedWindow is not a focusable Window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    restoreFocus(we);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                // Restore focus to the Component which last held it. We do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                // this here so that client code can override our choice in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                // a WINDOW_GAINED_FOCUS handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                // Make sure that the focus change request doesn't change the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                // focused Window in case we are no longer the focused Window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                // when the request is handled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                if (inSendMessage == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    // Identify which Component should initially gain focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    // in the Window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                    // * If we're in SendMessage, then this is a synthetic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    //   WINDOW_GAINED_FOCUS message which was generated by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    //   the FOCUS_GAINED handler. Allow the Component to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    //   which the FOCUS_GAINED message was targeted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                    //   receive the focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    // * Otherwise, look up the correct Component here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    //   We don't use Window.getMostRecentFocusOwner because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    //   window is focused now and 'null' will be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    // Calculating of most recent focus owner and focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    // request should be synchronized on KeyboardFocusManager.class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    // to prevent from thread race when user will request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    // focus between calculation and our request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    // But if focus transfer is synchronous, this synchronization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    // may cause deadlock, thus we don't synchronize this block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                    Component toFocus = KeyboardFocusManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                        getMostRecentFocusOwner(newFocusedWindow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    if ((toFocus == null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                        newFocusedWindow.isFocusableWindow())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                        toFocus = newFocusedWindow.getFocusTraversalPolicy().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                            getInitialComponent(newFocusedWindow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    Component tempLost = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    synchronized(KeyboardFocusManager.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                        tempLost = newFocusedWindow.setTemporaryLostComponent(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    // The component which last has the focus when this window was focused
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    // should receive focus first
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 17893
diff changeset
   429
                    if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
   430
                        focusLog.finer("tempLost {0}, toFocus {1}",
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
   431
                                       tempLost, toFocus);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    if (tempLost != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                        tempLost.requestFocusInWindow(CausedFocusEvent.Cause.ACTIVATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    if (toFocus != null && toFocus != tempLost) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                        // If there is a component which requested focus when this window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                        // was inactive it expects to receive focus after activation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                        toFocus.requestFocusInWindow(CausedFocusEvent.Cause.ACTIVATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                Window realOppositeWindow = this.realOppositeWindowWR.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                if (realOppositeWindow != we.getOppositeWindow()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    we = new WindowEvent(newFocusedWindow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                                         WindowEvent.WINDOW_GAINED_FOCUS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                                         realOppositeWindow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                return typeAheadAssertions(newFocusedWindow, we);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            case WindowEvent.WINDOW_ACTIVATED: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                WindowEvent we = (WindowEvent)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                Window oldActiveWindow = getGlobalActiveWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                Window newActiveWindow = we.getWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                if (oldActiveWindow == newActiveWindow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                // If there exists a current active window, then notify it that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                // it has lost activation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                if (oldActiveWindow != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    boolean isEventDispatched =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                        sendMessage(oldActiveWindow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                                new WindowEvent(oldActiveWindow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                                                WindowEvent.WINDOW_DEACTIVATED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                                                newActiveWindow));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    // Failed to dispatch, clear by ourselfves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    if (!isEventDispatched) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                        setGlobalActiveWindow(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    if (getGlobalActiveWindow() != null) {
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                setGlobalActiveWindow(newActiveWindow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                if (newActiveWindow != getGlobalActiveWindow()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                    // Activation change was rejected. Unlikely, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                    // possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                return typeAheadAssertions(newActiveWindow, we);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            case FocusEvent.FOCUS_GAINED: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                FocusEvent fe = (FocusEvent)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                CausedFocusEvent.Cause cause = (fe instanceof CausedFocusEvent) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                    ((CausedFocusEvent)fe).getCause() : CausedFocusEvent.Cause.UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                Component oldFocusOwner = getGlobalFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                Component newFocusOwner = fe.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                if (oldFocusOwner == newFocusOwner) {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 17893
diff changeset
   498
                    if (focusLog.isLoggable(PlatformLogger.Level.FINE)) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
   499
                        focusLog.fine("Skipping {0} because focus owner is the same", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    // We can't just drop the event - there could be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                    // type-ahead markers associated with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    dequeueKeyEvents(-1, newFocusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                // If there exists a current focus owner, then notify it that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                // it has lost focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                if (oldFocusOwner != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    boolean isEventDispatched =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                        sendMessage(oldFocusOwner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                                    new CausedFocusEvent(oldFocusOwner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                                                   FocusEvent.FOCUS_LOST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                                                   fe.isTemporary(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                                                   newFocusOwner, cause));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                    // Failed to dispatch, clear by ourselfves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    if (!isEventDispatched) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                        setGlobalFocusOwner(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                        if (!fe.isTemporary()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                            setGlobalPermanentFocusOwner(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                // Because the native windowing system has a different notion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                // of the current focus and activation states, it is possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                // that a Component outside of the focused Window receives a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                // FOCUS_GAINED event. We synthesize a WINDOW_GAINED_FOCUS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                // event in that case.
2451
597df8e1d786 6633275: Need to support shaped/translucent windows
art
parents: 1188
diff changeset
   530
                final Window newFocusedWindow = SunToolkit.getContainingWindow(newFocusOwner);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                final Window currentFocusedWindow = getGlobalFocusedWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                if (newFocusedWindow != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    newFocusedWindow != currentFocusedWindow)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                    sendMessage(newFocusedWindow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                                new WindowEvent(newFocusedWindow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                                        WindowEvent.WINDOW_GAINED_FOCUS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                                                currentFocusedWindow));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                    if (newFocusedWindow != getGlobalFocusedWindow()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                        // Focus change was rejected. Will happen if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                        // newFocusedWindow is not a focusable Window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                        // Need to recover type-ahead, but don't bother
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                        // restoring focus. That was done by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                        // WINDOW_GAINED_FOCUS handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                        dequeueKeyEvents(-1, newFocusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
1171
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 441
diff changeset
   551
                if (!(newFocusOwner.isFocusable() && newFocusOwner.isShowing() &&
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 441
diff changeset
   552
                    // 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
   553
                    // 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
   554
                    // 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
   555
                    (newFocusOwner.isEnabled() || cause.equals(CausedFocusEvent.Cause.UNKNOWN))))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                    // we should not accept focus on such component, so reject it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                    dequeueKeyEvents(-1, newFocusOwner);
441
f5da1014ed23 6607170: Focus not set by requestFocus
ant
parents: 419
diff changeset
   559
                    if (KeyboardFocusManager.isAutoFocusTransferEnabled()) {
f5da1014ed23 6607170: Focus not set by requestFocus
ant
parents: 419
diff changeset
   560
                        // If FOCUS_GAINED is for a disposed component (however
f5da1014ed23 6607170: Focus not set by requestFocus
ant
parents: 419
diff changeset
   561
                        // it shouldn't happen) its toplevel parent is null. In this
f5da1014ed23 6607170: Focus not set by requestFocus
ant
parents: 419
diff changeset
   562
                        // case we have to try to restore focus in the current focused
f5da1014ed23 6607170: Focus not set by requestFocus
ant
parents: 419
diff changeset
   563
                        // window (for the details: 6607170).
f5da1014ed23 6607170: Focus not set by requestFocus
ant
parents: 419
diff changeset
   564
                        if (newFocusedWindow == null) {
f5da1014ed23 6607170: Focus not set by requestFocus
ant
parents: 419
diff changeset
   565
                            restoreFocus(fe, currentFocusedWindow);
f5da1014ed23 6607170: Focus not set by requestFocus
ant
parents: 419
diff changeset
   566
                        } else {
f5da1014ed23 6607170: Focus not set by requestFocus
ant
parents: 419
diff changeset
   567
                            restoreFocus(fe, newFocusedWindow);
f5da1014ed23 6607170: Focus not set by requestFocus
ant
parents: 419
diff changeset
   568
                        }
17893
a34977112c8b 8013773: requestFocusInWindow to a disabled component prevents window of getting focused
ant
parents: 16839
diff changeset
   569
                        setMostRecentFocusOwner(newFocusedWindow, null); // see: 8013773
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                setGlobalFocusOwner(newFocusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                if (newFocusOwner != getGlobalFocusOwner()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                    // Focus change was rejected. Will happen if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                    // newFocusOwner is not focus traversable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                    dequeueKeyEvents(-1, newFocusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                    if (KeyboardFocusManager.isAutoFocusTransferEnabled()) {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20455
diff changeset
   581
                        restoreFocus(fe, newFocusedWindow);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                if (!fe.isTemporary()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    setGlobalPermanentFocusOwner(newFocusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                    if (newFocusOwner != getGlobalPermanentFocusOwner()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                        // Focus change was rejected. Unlikely, but possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                        dequeueKeyEvents(-1, newFocusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                        if (KeyboardFocusManager.isAutoFocusTransferEnabled()) {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 20455
diff changeset
   593
                            restoreFocus(fe, newFocusedWindow);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                setNativeFocusOwner(getHeavyweight(newFocusOwner));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                Component realOppositeComponent = this.realOppositeComponentWR.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                if (realOppositeComponent != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                    realOppositeComponent != fe.getOppositeComponent()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                    fe = new CausedFocusEvent(newFocusOwner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                                        FocusEvent.FOCUS_GAINED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                                        fe.isTemporary(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                                        realOppositeComponent, cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                    ((AWTEvent) fe).isPosted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                return typeAheadAssertions(newFocusOwner, fe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            case FocusEvent.FOCUS_LOST: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                FocusEvent fe = (FocusEvent)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                Component currentFocusOwner = getGlobalFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                if (currentFocusOwner == null) {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 17893
diff changeset
   617
                    if (focusLog.isLoggable(PlatformLogger.Level.FINE))
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
   618
                        focusLog.fine("Skipping {0} because focus owner is null", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                // Ignore cases where a Component loses focus to itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                // If we make a mistake because of retargeting, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                // FOCUS_GAINED handler will correct it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                if (currentFocusOwner == fe.getOppositeComponent()) {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 17893
diff changeset
   625
                    if (focusLog.isLoggable(PlatformLogger.Level.FINE))
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
   626
                        focusLog.fine("Skipping {0} because current focus owner is equal to opposite", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                setGlobalFocusOwner(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                if (getGlobalFocusOwner() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                    // Focus change was rejected. Unlikely, but possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                    restoreFocus(currentFocusOwner, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                if (!fe.isTemporary()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                    setGlobalPermanentFocusOwner(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                    if (getGlobalPermanentFocusOwner() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                        // Focus change was rejected. Unlikely, but possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                        restoreFocus(currentFocusOwner, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                    Window owningWindow = currentFocusOwner.getContainingWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                    if (owningWindow != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                        owningWindow.setTemporaryLostComponent(currentFocusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                setNativeFocusOwner(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                fe.setSource(currentFocusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                realOppositeComponentWR = (fe.getOppositeComponent() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                    ? new WeakReference<Component>(currentFocusOwner)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                    : NULL_COMPONENT_WR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                return typeAheadAssertions(currentFocusOwner, fe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            case WindowEvent.WINDOW_DEACTIVATED: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                WindowEvent we = (WindowEvent)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                Window currentActiveWindow = getGlobalActiveWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                if (currentActiveWindow == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                if (currentActiveWindow != e.getSource()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                    // The event is lost in time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                    // Allow listeners to precess the event but do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                    // change any global states
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                setGlobalActiveWindow(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                if (getGlobalActiveWindow() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                    // Activation change was rejected. Unlikely, but possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                we.setSource(currentActiveWindow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                return typeAheadAssertions(currentActiveWindow, we);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            case WindowEvent.WINDOW_LOST_FOCUS: {
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   689
                if (repostIfFollowsKeyEvents((WindowEvent)e)) {
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   690
                    break;
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   691
                }
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   692
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                WindowEvent we = (WindowEvent)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                Window currentFocusedWindow = getGlobalFocusedWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                Window losingFocusWindow = we.getWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                Window activeWindow = getGlobalActiveWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                Window oppositeWindow = we.getOppositeWindow();
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 17893
diff changeset
   698
                if (focusLog.isLoggable(PlatformLogger.Level.FINE))
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
   699
                    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
   700
                                  activeWindow, currentFocusedWindow,
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
   701
                                  losingFocusWindow, oppositeWindow);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                if (currentFocusedWindow == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                // Special case -- if the native windowing system posts an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                // event claiming that the active Window has lost focus to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                // focused Window, then discard the event. This is an artifact
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                // of the native windowing system not knowing which Window is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                // really focused.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                if (inSendMessage == 0 && losingFocusWindow == activeWindow &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                    oppositeWindow == currentFocusedWindow)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                Component currentFocusOwner = getGlobalFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                if (currentFocusOwner != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                    // The focus owner should always receive a FOCUS_LOST event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                    // before the Window is defocused.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                    Component oppositeComp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                    if (oppositeWindow != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                        oppositeComp = oppositeWindow.getTemporaryLostComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                        if (oppositeComp == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                            oppositeComp = oppositeWindow.getMostRecentFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                    if (oppositeComp == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                        oppositeComp = oppositeWindow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                    sendMessage(currentFocusOwner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                                new CausedFocusEvent(currentFocusOwner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                                               FocusEvent.FOCUS_LOST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                                               true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                                               oppositeComp, CausedFocusEvent.Cause.ACTIVATION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                setGlobalFocusedWindow(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                if (getGlobalFocusedWindow() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                    // Focus change was rejected. Unlikely, but possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                    restoreFocus(currentFocusedWindow, null, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                we.setSource(currentFocusedWindow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                realOppositeWindowWR = (oppositeWindow != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                    ? new WeakReference<Window>(currentFocusedWindow)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                    : NULL_WINDOW_WR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                typeAheadAssertions(currentFocusedWindow, we);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                if (oppositeWindow == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                    // Then we need to deactive the active Window as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                    // No need to synthesize in other cases, because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                    // WINDOW_ACTIVATED will handle it if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                    sendMessage(activeWindow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                                new WindowEvent(activeWindow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                                                WindowEvent.WINDOW_DEACTIVATED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                                                null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                    if (getGlobalActiveWindow() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                        // Activation change was rejected. Unlikely,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                        // but possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                        restoreFocus(currentFocusedWindow, null, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            case KeyEvent.KEY_TYPED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            case KeyEvent.KEY_PRESSED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            case KeyEvent.KEY_RELEASED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                return typeAheadAssertions(null, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * Called by <code>dispatchEvent</code> if no other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * KeyEventDispatcher in the dispatcher chain dispatched the KeyEvent, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * if no other KeyEventDispatchers are registered. If the event has not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * been consumed, its target is enabled, and the focus owner is not null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * this method dispatches the event to its target. This method will also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * subsequently dispatch the event to all registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * KeyEventPostProcessors. After all this operations are finished,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * the event is passed to peers for processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * In all cases, this method returns <code>true</code>, since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * DefaultKeyboardFocusManager is designed so that neither
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * <code>dispatchEvent</code>, nor the AWT event dispatcher, should take
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * further action on the event in any situation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * @param e the KeyEvent to be dispatched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * @return <code>true</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * @see Component#dispatchEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
    public boolean dispatchKeyEvent(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        Component focusOwner = (((AWTEvent)e).isPosted) ? getFocusOwner() : e.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
1171
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 441
diff changeset
   802
        if (focusOwner != null && focusOwner.isShowing() && focusOwner.canBeFocusOwner()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            if (!e.isConsumed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                Component comp = e.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                if (comp != null && comp.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                    redispatchEvent(comp, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        boolean stopPostProcessing = false;
20107
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 18295
diff changeset
   811
        java.util.List<KeyEventPostProcessor> processors = getKeyEventPostProcessors();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        if (processors != null) {
20107
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 18295
diff changeset
   813
            for (java.util.Iterator<KeyEventPostProcessor> iter = processors.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                 !stopPostProcessing && iter.hasNext(); )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            {
20107
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 18295
diff changeset
   816
                stopPostProcessing = iter.next().
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 18295
diff changeset
   817
                            postProcessKeyEvent(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        if (!stopPostProcessing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            postProcessKeyEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        // Allow the peer to process KeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        Component source = e.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        ComponentPeer peer = source.getPeer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        if (peer == null || peer instanceof LightweightPeer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            // if focus owner is lightweight then its native container
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            // processes event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            Container target = source.getNativeContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            if (target != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                peer = target.getPeer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            peer.handleEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * This method will be called by <code>dispatchKeyEvent</code>. It will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * handle any unconsumed KeyEvents that map to an AWT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * <code>MenuShortcut</code> by consuming the event and activating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * shortcut.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * @param e the KeyEvent to post-process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * @return <code>true</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * @see #dispatchKeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * @see MenuShortcut
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    public boolean postProcessKeyEvent(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        if (!e.isConsumed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            Component target = e.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            Container p = (Container)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                (target instanceof Container ? target : target.getParent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            if (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                p.postProcessKeyEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    private void pumpApprovedKeyEvents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        KeyEvent ke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            ke = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                if (enqueuedKeyEvents.size() != 0) {
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   872
                    ke = enqueuedKeyEvents.getFirst();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                    if (typeAheadMarkers.size() != 0) {
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   874
                        TypeAheadMarker marker = typeAheadMarkers.getFirst();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                        // Fixed 5064013: may appears that the events have the same time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                        // if (ke.getWhen() >= marker.after) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                        // The fix is rolled out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                        if (ke.getWhen() > marker.after) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                            ke = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                    if (ke != null) {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 17893
diff changeset
   884
                        if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 13656
diff changeset
   885
                            focusLog.finer("Pumping approved event {0}", ke);
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 13656
diff changeset
   886
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                        enqueuedKeyEvents.removeFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            if (ke != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                preDispatchKeyEvent(ke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        } while (ke != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * Dumps the list of type-ahead queue markers to stderr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    void dumpMarkers() {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 17893
diff changeset
   901
        if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
   902
            focusLog.finest(">>> Markers dump, time: {0}", System.currentTimeMillis());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                if (typeAheadMarkers.size() != 0) {
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   905
                    Iterator<TypeAheadMarker> iter = typeAheadMarkers.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                    while (iter.hasNext()) {
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   907
                        TypeAheadMarker marker = iter.next();
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
   908
                        focusLog.finest("    {0}", marker);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    private boolean typeAheadAssertions(Component target, AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        // Clear any pending events here as well as in the FOCUS_GAINED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        // handler. We need this call here in case a marker was removed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        // response to a call to dequeueKeyEvents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        pumpApprovedKeyEvents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        switch (e.getID()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            case KeyEvent.KEY_TYPED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            case KeyEvent.KEY_PRESSED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            case KeyEvent.KEY_RELEASED: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                KeyEvent ke = (KeyEvent)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                    if (e.isPosted && typeAheadMarkers.size() != 0) {
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   929
                        TypeAheadMarker marker = typeAheadMarkers.getFirst();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                        // Fixed 5064013: may appears that the events have the same time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                        // if (ke.getWhen() >= marker.after) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                        // The fix is rolled out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                        if (ke.getWhen() > marker.after) {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 17893
diff changeset
   935
                            if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 13656
diff changeset
   936
                                focusLog.finer("Storing event {0} because of marker {1}", ke, marker);
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 13656
diff changeset
   937
                            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                            enqueuedKeyEvents.addLast(ke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                // KeyEvent was posted before focus change request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                return preDispatchKeyEvent(ke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            case FocusEvent.FOCUS_GAINED:
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 17893
diff changeset
   949
                if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 13656
diff changeset
   950
                    focusLog.finest("Markers before FOCUS_GAINED on {0}", target);
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 13656
diff changeset
   951
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                dumpMarkers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                // Search the marker list for the first marker tied to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                // the Component which just gained focus. Then remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                // that marker, any markers which immediately follow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                // and are tied to the same component, and all markers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                // that preceed it. This handles the case where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                // multiple focus requests were made for the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                // Component in a row and when we lost some of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                // earlier requests. Since FOCUS_GAINED events will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                // not be generated for these additional requests, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                // need to clear those markers too.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                    boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                    if (hasMarker(target)) {
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   966
                        for (Iterator<TypeAheadMarker> iter = typeAheadMarkers.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                             iter.hasNext(); )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                        {
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
   969
                            if (iter.next().untilFocused == target) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                                found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                            } else if (found) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                            iter.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                        // Exception condition - event without marker
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 17893
diff changeset
   978
                        if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 13656
diff changeset
   979
                            focusLog.finer("Event without marker {0}", e);
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 13656
diff changeset
   980
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                }
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
   983
                focusLog.finest("Markers after FOCUS_GAINED");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                dumpMarkers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                redispatchEvent(target, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                // Now, dispatch any pending KeyEvents which have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                // released because of the FOCUS_GAINED event so that we don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                // have to wait for another event to be posted to the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                pumpApprovedKeyEvents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                redispatchEvent(target, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * Returns true if there are some marker associated with component <code>comp</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * in a markers' queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    private boolean hasMarker(Component comp) {
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
  1006
        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
  1007
            if (iter.next().untilFocused == comp) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * Clears markers queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    void clearMarkers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            typeAheadMarkers.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    private boolean preDispatchKeyEvent(KeyEvent ke) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        if (((AWTEvent) ke).isPosted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            Component focusOwner = getFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            ke.setSource(((focusOwner != null) ? focusOwner : getFocusedWindow()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        if (ke.getSource() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
  1033
        // 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
  1034
        // - 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
  1035
        // - 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
  1036
        //   until its dispatching actually starts (by this method).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        EventQueue.setCurrentEventAndMostRecentTime(ke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
         * Fix for 4495473.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
         * This fix allows to correctly dispatch events when native
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
         * event proxying mechanism is active.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
         * If it is active we should redispatch key events after
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
         * we detected its correct target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        if (KeyboardFocusManager.isProxyActive(ke)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            Component source = (Component)ke.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            Container target = source.getNativeContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            if (target != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                ComponentPeer peer = target.getPeer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                    peer.handleEvent(ke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                     * Fix for 4478780 - consume event after it was dispatched by peer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                    ke.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
20107
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 18295
diff changeset
  1062
        java.util.List<KeyEventDispatcher> dispatchers = getKeyEventDispatchers();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        if (dispatchers != null) {
20107
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 18295
diff changeset
  1064
            for (java.util.Iterator<KeyEventDispatcher> iter = dispatchers.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                 iter.hasNext(); )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
             {
20107
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 18295
diff changeset
  1067
                 if (iter.next().
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                     dispatchKeyEvent(ke))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                     return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        return dispatchKeyEvent(ke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * @param e is a KEY_PRESSED event that can be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     *          to track the next KEY_TYPED related.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
    private void consumeNextKeyTyped(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        consumeNextKeyTyped = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    private void consumeTraversalKey(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        e.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        consumeNextKeyTyped = (e.getID() == KeyEvent.KEY_PRESSED) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                              !e.isActionKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * return true if event was consumed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
    private boolean consumeProcessedKeyEvent(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        if ((e.getID() == KeyEvent.KEY_TYPED) && consumeNextKeyTyped) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            e.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            consumeNextKeyTyped = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * This method initiates a focus traversal operation if and only if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * KeyEvent represents a focus traversal key for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * focusedComponent. It is expected that focusedComponent is the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * focus owner, although this need not be the case. If it is not,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * focus traversal will nevertheless proceed as if focusedComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     * were the focus owner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     * @param focusedComponent the Component that is the basis for a focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     *        traversal operation if the specified event represents a focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     *        traversal key for the Component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     * @param e the event that may represent a focus traversal key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    public void processKeyEvent(Component focusedComponent, KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        // consume processed event if needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        if (consumeProcessedKeyEvent(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        // KEY_TYPED events cannot be focus traversal keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        if (e.getID() == KeyEvent.KEY_TYPED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        if (focusedComponent.getFocusTraversalKeysEnabled() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            !e.isConsumed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            AWTKeyStroke stroke = AWTKeyStroke.getAWTKeyStrokeForEvent(e),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                oppStroke = AWTKeyStroke.getAWTKeyStroke(stroke.getKeyCode(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                                                 stroke.getModifiers(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                                                 !stroke.isOnKeyRelease());
20107
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 18295
diff changeset
  1134
            Set<AWTKeyStroke> toTest;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            boolean contains, containsOpp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            toTest = focusedComponent.getFocusTraversalKeys(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
            contains = toTest.contains(stroke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            containsOpp = toTest.contains(oppStroke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
            if (contains || containsOpp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                consumeTraversalKey(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                if (contains) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                    focusNextComponent(focusedComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                return;
419
2d7b6a4f8002 6637607: 1st char. is discarded after a modal dialogue shows up and disappears
ant
parents: 134
diff changeset
  1148
            } 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
  1149
                // 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
  1150
                consumeNextKeyTyped = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            toTest = focusedComponent.getFocusTraversalKeys(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
                KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
            contains = toTest.contains(stroke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            containsOpp = toTest.contains(oppStroke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            if (contains || containsOpp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                consumeTraversalKey(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                if (contains) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                    focusPreviousComponent(focusedComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                }
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.UP_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
                    upFocusCycle(focusedComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
            if (!((focusedComponent instanceof Container) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
                  ((Container)focusedComponent).isFocusCycleRoot())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            toTest = focusedComponent.getFocusTraversalKeys(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
                KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            contains = toTest.contains(stroke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
            containsOpp = toTest.contains(oppStroke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
            if (contains || containsOpp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                consumeTraversalKey(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                if (contains) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                    downFocusCycle((Container)focusedComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     * Delays dispatching of KeyEvents until the specified Component becomes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * the focus owner. KeyEvents with timestamps later than the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * timestamp will be enqueued until the specified Component receives a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * FOCUS_GAINED event, or the AWT cancels the delay request by invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * <code>dequeueKeyEvents</code> or <code>discardKeyEvents</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * @param after timestamp of current event, or the current, system time if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     *        the current event has no timestamp, or the AWT cannot determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     *        which event is currently being handled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * @param untilFocused Component which will receive a FOCUS_GAINED event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     *        before any pending KeyEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * @see #dequeueKeyEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * @see #discardKeyEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
    protected synchronized void enqueueKeyEvents(long after,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                                                 Component untilFocused) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        if (untilFocused == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 17893
diff changeset
  1219
        if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 13656
diff changeset
  1220
            focusLog.finer("Enqueue at {0} for {1}",
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
  1221
                       after, untilFocused);
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 13656
diff changeset
  1222
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        int insertionIndex = 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            i = typeAheadMarkers.size();
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
  1226
        ListIterator<TypeAheadMarker> iter = typeAheadMarkers.listIterator(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        for (; i > 0; i--) {
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
  1229
            TypeAheadMarker marker = iter.previous();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
            if (marker.after <= after) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                insertionIndex = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        typeAheadMarkers.add(insertionIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                             new TypeAheadMarker(after, untilFocused));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     * Releases for normal dispatching to the current focus owner all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     * KeyEvents which were enqueued because of a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     * <code>enqueueKeyEvents</code> with the same timestamp and Component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     * If the given timestamp is less than zero, the outstanding enqueue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     * request for the given Component with the <b>oldest</b> timestamp (if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
     * any) should be cancelled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
     * @param after the timestamp specified in the call to
20172
f48935a247ec 8025218: [javadoc] some errors in java/awt classes
yan
parents: 20107
diff changeset
  1249
     *        <code>enqueueKeyEvents</code>, or any value &lt; 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     * @param untilFocused the Component specified in the call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     *        <code>enqueueKeyEvents</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     * @see #enqueueKeyEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     * @see #discardKeyEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
    protected synchronized void dequeueKeyEvents(long after,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                                                 Component untilFocused) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        if (untilFocused == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 17893
diff changeset
  1261
        if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 13656
diff changeset
  1262
            focusLog.finer("Dequeue at {0} for {1}",
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 2451
diff changeset
  1263
                       after, untilFocused);
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 13656
diff changeset
  1264
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        TypeAheadMarker marker;
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
  1267
        ListIterator<TypeAheadMarker> iter = typeAheadMarkers.listIterator
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
            ((after >= 0) ? typeAheadMarkers.size() : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        if (after < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
            while (iter.hasNext()) {
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
  1272
                marker = iter.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                if (marker.untilFocused == untilFocused)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                    iter.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
            while (iter.hasPrevious()) {
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
  1281
                marker = iter.previous();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                if (marker.untilFocused == untilFocused &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                    marker.after == after)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                    iter.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     * Discards all KeyEvents which were enqueued because of one or more calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     * to <code>enqueueKeyEvents</code> with the specified Component, or one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     * its descendants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     * @param comp the Component specified in one or more calls to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     *        <code>enqueueKeyEvents</code>, or a parent of such a Component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     * @see #enqueueKeyEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     * @see #dequeueKeyEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
    protected synchronized void discardKeyEvents(Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        if (comp == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        long start = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
  1309
        for (Iterator<TypeAheadMarker> iter = typeAheadMarkers.iterator(); iter.hasNext(); ) {
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
  1310
            TypeAheadMarker marker = iter.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            Component toTest = marker.untilFocused;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            boolean match = (toTest == comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            while (!match && toTest != null && !(toTest instanceof Window)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                toTest = toTest.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                match = (toTest == comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
            if (match) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                if (start < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                    start = marker.after;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                iter.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
            } else if (start >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                purgeStampedEvents(start, marker.after);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                start = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
        purgeStampedEvents(start, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
    // Notes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
    //   * must be called inside a synchronized block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
    //   * if 'start' is < 0, then this function does nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
    //   * if 'end' is < 0, then all KeyEvents from 'start' to the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
    //     queue will be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
    private void purgeStampedEvents(long start, long end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        if (start < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
13652
42544e68dc39 6981400: Tabbing between textfield do not work properly when ALT+TAB
ant
parents: 12651
diff changeset
  1341
        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
  1342
            KeyEvent ke = iter.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
            long time = ke.getWhen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
            if (start < time && (end < 0 || time <= end)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                iter.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
            if (end >= 0 && time > end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     * Focuses the Component before aComponent, typically based on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     * FocusTraversalPolicy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     * @param aComponent the Component that is the basis for the focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     *        traversal operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     * @see FocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
     * @see Component#transferFocusBackward
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
    public void focusPreviousComponent(Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
        if (aComponent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
            aComponent.transferFocusBackward();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     * Focuses the Component after aComponent, typically based on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     * FocusTraversalPolicy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     * @param aComponent the Component that is the basis for the focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     *        traversal operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     * @see FocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     * @see Component#transferFocus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
    public void focusNextComponent(Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        if (aComponent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
            aComponent.transferFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     * Moves the focus up one focus traversal cycle. Typically, the focus owner
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     * is set to aComponent's focus cycle root, and the current focus cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     * root is set to the new focus owner's focus cycle root. If, however,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     * aComponent's focus cycle root is a Window, then the focus owner is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     * to the focus cycle root's default Component to focus, and the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * focus cycle root is unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * @param aComponent the Component that is the basis for the focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     *        traversal operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * @see Component#transferFocusUpCycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
    public void upFocusCycle(Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        if (aComponent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
            aComponent.transferFocusUpCycle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     * Moves the focus down one focus traversal cycle. If aContainer is a focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
     * cycle root, then the focus owner is set to aContainer's default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
     * Component to focus, and the current focus cycle root is set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
     * aContainer. If aContainer is not a focus cycle root, then no focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
     * traversal operation occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
     * @param aContainer the Container that is the basis for the focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
     *        traversal operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
     * @see Container#transferFocusDownCycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
    public void downFocusCycle(Container aContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        if (aContainer != null && aContainer.isFocusCycleRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
            aContainer.transferFocusDownCycle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
}