jdk/src/share/classes/java/awt/KeyboardFocusManager.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 441 f5da1014ed23
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
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.InputEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.KeyEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.event.WindowEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.peer.KeyboardFocusManagerPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.peer.LightweightPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.beans.PropertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.beans.PropertyChangeSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.beans.PropertyVetoException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.beans.VetoableChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.beans.VetoableChangeSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.lang.reflect.Field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.util.LinkedList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.util.WeakHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import java.util.logging.Level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import java.util.logging.Logger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import sun.awt.AppContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import sun.awt.HeadlessToolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import sun.awt.SunToolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import sun.awt.CausedFocusEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import sun.awt.KeyboardFocusManagerPeerProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * The KeyboardFocusManager is responsible for managing the active and focused
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * Windows, and the current focus owner. The focus owner is defined as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * Component in an application that will typically receive all KeyEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * generated by the user. The focused Window is the Window that is, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * contains, the focus owner. Only a Frame or a Dialog can be the active
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * Window. The native windowing system may denote the active Window or its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * children with special decorations, such as a highlighted title bar. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * active Window is always either the focused Window, or the first Frame or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * Dialog that is an owner of the focused Window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * The KeyboardFocusManager is both a centralized location for client code to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * query for the focus owner and initiate focus changes, and an event
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * dispatcher for all FocusEvents, WindowEvents related to focus, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * KeyEvents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * Some browsers partition applets in different code bases into separate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * contexts, and establish walls between these contexts. In such a scenario,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * there will be one KeyboardFocusManager per context. Other browsers place all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * applets into the same context, implying that there will be only a single,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * global KeyboardFocusManager for all applets. This behavior is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * implementation-dependent. Consult your browser's documentation for more
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * information. No matter how many contexts there may be, however, there can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * never be more than one focus owner, focused Window, or active Window, per
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * ClassLoader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * Please see
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * How to Use the Focus Subsystem</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * a section in <em>The Java Tutorial</em>, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <a href="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * @author David Mendenhall
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * @see Window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * @see Frame
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * @see Dialog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * @see java.awt.event.FocusEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * @see java.awt.event.WindowEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * @see java.awt.event.KeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
public abstract class KeyboardFocusManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    implements KeyEventDispatcher, KeyEventPostProcessor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    // Shared focus engine logger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private static final Logger focusLog = Logger.getLogger("java.awt.focus.KeyboardFocusManager");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        /* ensure that the necessary native libraries are loaded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        Toolkit.loadLibraries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (!GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    transient KeyboardFocusManagerPeer peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Initialize JNI field and method IDs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private static final Logger log = Logger.getLogger("java.awt.KeyboardFocusManager");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * The identifier for the Forward focus traversal keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @see #setDefaultFocusTraversalKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @see #getDefaultFocusTraversalKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @see Component#setFocusTraversalKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @see Component#getFocusTraversalKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public static final int FORWARD_TRAVERSAL_KEYS = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * The identifier for the Backward focus traversal keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @see #setDefaultFocusTraversalKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @see #getDefaultFocusTraversalKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @see Component#setFocusTraversalKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @see Component#getFocusTraversalKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public static final int BACKWARD_TRAVERSAL_KEYS = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * The identifier for the Up Cycle focus traversal keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @see #setDefaultFocusTraversalKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @see #getDefaultFocusTraversalKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @see Component#setFocusTraversalKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @see Component#getFocusTraversalKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public static final int UP_CYCLE_TRAVERSAL_KEYS = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * The identifier for the Down Cycle focus traversal keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @see #setDefaultFocusTraversalKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @see #getDefaultFocusTraversalKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @see Component#setFocusTraversalKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @see Component#getFocusTraversalKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public static final int DOWN_CYCLE_TRAVERSAL_KEYS = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    static final int TRAVERSAL_KEY_LENGTH = DOWN_CYCLE_TRAVERSAL_KEYS + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Returns the current KeyboardFocusManager instance for the calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * thread's context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @return this thread's context's KeyboardFocusManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @see #setCurrentKeyboardFocusManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public static KeyboardFocusManager getCurrentKeyboardFocusManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return getCurrentKeyboardFocusManager(AppContext.getAppContext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    synchronized static KeyboardFocusManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        getCurrentKeyboardFocusManager(AppContext appcontext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        KeyboardFocusManager manager = (KeyboardFocusManager)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            appcontext.get(KeyboardFocusManager.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (manager == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            manager = new DefaultKeyboardFocusManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            appcontext.put(KeyboardFocusManager.class, manager);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return manager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Sets the current KeyboardFocusManager instance for the calling thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * context. If null is specified, then the current KeyboardFocusManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * is replaced with a new instance of DefaultKeyboardFocusManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * If a SecurityManager is installed, the calling thread must be granted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * the AWTPermission "replaceKeyboardFocusManager" in order to replace the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * the current KeyboardFocusManager. If this permission is not granted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * this method will throw a SecurityException, and the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * KeyboardFocusManager will be unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @param newManager the new KeyboardFocusManager for this thread's context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @see #getCurrentKeyboardFocusManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @see DefaultKeyboardFocusManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @throws SecurityException if the calling thread does not have permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *         to replace the current KeyboardFocusManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public static void setCurrentKeyboardFocusManager(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        KeyboardFocusManager newManager) throws SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            if (replaceKeyboardFocusManagerPermission == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                replaceKeyboardFocusManagerPermission =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    new AWTPermission("replaceKeyboardFocusManager");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            security.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                checkPermission(replaceKeyboardFocusManagerPermission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        KeyboardFocusManager oldManager = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        synchronized (KeyboardFocusManager.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            AppContext appcontext = AppContext.getAppContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            if (newManager != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                oldManager = getCurrentKeyboardFocusManager(appcontext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                appcontext.put(KeyboardFocusManager.class, newManager);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                oldManager = getCurrentKeyboardFocusManager(appcontext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                appcontext.remove(KeyboardFocusManager.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (oldManager != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            oldManager.firePropertyChange("managingFocus",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                                          Boolean.TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                                          Boolean.FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (newManager != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            newManager.firePropertyChange("managingFocus",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                                          Boolean.FALSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                                          Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * The Component in an application that will typically receive all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * KeyEvents generated by the user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    private static Component focusOwner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * The Component in an application that will regain focus when an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * outstanding temporary focus transfer has completed, or the focus owner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * if no outstanding temporary transfer exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    private static Component permanentFocusOwner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * The Window which is, or contains, the focus owner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    private static Window focusedWindow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * Only a Frame or a Dialog can be the active Window. The native windowing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * system may denote the active Window with a special decoration, such as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * highlighted title bar. The active Window is always either the focused
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * Window, or the first Frame or Dialog which is an owner of the focused
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * Window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    private static Window activeWindow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * The default FocusTraversalPolicy for all Windows that have no policy of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * their own set. If those Windows have focus-cycle-root children that have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * no keyboard-traversal policy of their own, then those children will also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * inherit this policy (as will, recursively, their focus-cycle-root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * children).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    private FocusTraversalPolicy defaultPolicy =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        new DefaultFocusTraversalPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * The bound property names of each focus traversal key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    private static final String[] defaultFocusTraversalKeyPropertyNames = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        "forwardDefaultFocusTraversalKeys",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        "backwardDefaultFocusTraversalKeys",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        "upCycleDefaultFocusTraversalKeys",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        "downCycleDefaultFocusTraversalKeys"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * The default strokes for initializing the default focus traversal keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    private static final AWTKeyStroke[][] defaultFocusTraversalKeyStrokes = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB, 0, false),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB, InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK, false),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK, false),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                                         InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK | InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                                         false),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        {},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        {},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
      };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * The default focus traversal keys. Each array of traversal keys will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * in effect on all Windows that have no such array of their own explicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * set. Each array will also be inherited, recursively, by any child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * Component of those Windows that has no such array of its own explicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    private Set[] defaultFocusTraversalKeys = new Set[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * The current focus cycle root. If the focus owner is itself a focus cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * root, then it may be ambiguous as to which Components represent the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * and previous Components to focus during normal focus traversal. In that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * case, the current focus cycle root is used to differentiate among the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * possibilities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    private static Container currentFocusCycleRoot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * A description of any VetoableChangeListeners which have been registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    private VetoableChangeSupport vetoableSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * A description of any PropertyChangeListeners which have been registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    private PropertyChangeSupport changeSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * This KeyboardFocusManager's KeyEventDispatcher chain. The List does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * include this KeyboardFocusManager unless it was explicitly re-registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * via a call to <code>addKeyEventDispatcher</code>. If no other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * KeyEventDispatchers are registered, this field may be null or refer to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * a List of length 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    private java.util.LinkedList keyEventDispatchers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * This KeyboardFocusManager's KeyEventPostProcessor chain. The List does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * not include this KeyboardFocusManager unless it was explicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * re-registered via a call to <code>addKeyEventPostProcessor</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * If no other KeyEventPostProcessors are registered, this field may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * null or refer to a List of length 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    private java.util.LinkedList keyEventPostProcessors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * Maps Windows to those Windows' most recent focus owners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    private static java.util.Map mostRecentFocusOwners = new WeakHashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * Error String for initializing SecurityExceptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    private static final String notPrivileged = "this KeyboardFocusManager is not installed in the current thread's context";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * We cache the permission used to verify that the calling thread is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * permitted to access the global focus state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    private static AWTPermission replaceKeyboardFocusManagerPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * SequencedEvent which is currently dispatched in AppContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    transient SequencedEvent currentSequencedEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    final void setCurrentSequencedEvent(SequencedEvent current) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        synchronized (SequencedEvent.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            assert(current == null || currentSequencedEvent == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            currentSequencedEvent = current;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    final SequencedEvent getCurrentSequencedEvent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        synchronized (SequencedEvent.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            return currentSequencedEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    static Set initFocusTraversalKeysSet(String value, Set targetSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        StringTokenizer tokens = new StringTokenizer(value, ",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        while (tokens.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            targetSet.add(AWTKeyStroke.getAWTKeyStroke(tokens.nextToken()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        return (targetSet.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            ? Collections.EMPTY_SET
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            : Collections.unmodifiableSet(targetSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * Initializes a KeyboardFocusManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    public KeyboardFocusManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        for (int i = 0; i < TRAVERSAL_KEY_LENGTH; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            Set work_set = new HashSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            for (int j = 0; j < defaultFocusTraversalKeyStrokes[i].length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                work_set.add(defaultFocusTraversalKeyStrokes[i][j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            defaultFocusTraversalKeys[i] = (work_set.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                ? Collections.EMPTY_SET
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                : Collections.unmodifiableSet(work_set);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        initPeer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    private void initPeer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        Toolkit tk = Toolkit.getDefaultToolkit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        KeyboardFocusManagerPeerProvider peerProvider = (KeyboardFocusManagerPeerProvider)tk;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        peer = peerProvider.createKeyboardFocusManagerPeer(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * Returns the focus owner, if the focus owner is in the same context as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * the calling thread. The focus owner is defined as the Component in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * application that will typically receive all KeyEvents generated by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * user. KeyEvents which map to the focus owner's focus traversal keys will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * not be delivered if focus traversal keys are enabled for the focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * owner. In addition, KeyEventDispatchers may retarget or consume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * KeyEvents before they reach the focus owner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @return the focus owner, or null if the focus owner is not a member of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *         the calling thread's context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @see #getGlobalFocusOwner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @see #setGlobalFocusOwner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    public Component getFocusOwner() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        synchronized (KeyboardFocusManager.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            if (focusOwner == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            return (focusOwner.appContext == AppContext.getAppContext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                ? focusOwner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * Returns the focus owner, even if the calling thread is in a different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * context than the focus owner. The focus owner is defined as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * Component in an application that will typically receive all KeyEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * generated by the user. KeyEvents which map to the focus owner's focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * traversal keys will not be delivered if focus traversal keys are enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * for the focus owner. In addition, KeyEventDispatchers may retarget or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * consume KeyEvents before they reach the focus owner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * This method will throw a SecurityException if this KeyboardFocusManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * is not the current KeyboardFocusManager for the calling thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @return the focus owner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @see #getFocusOwner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @see #setGlobalFocusOwner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * @throws SecurityException if this KeyboardFocusManager is not the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *         current KeyboardFocusManager for the calling thread's context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    protected Component getGlobalFocusOwner() throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        synchronized (KeyboardFocusManager.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            if (this == getCurrentKeyboardFocusManager()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                return focusOwner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                if (focusLog.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                    focusLog.log(Level.FINER, "This manager is " + this + ", current is " + getCurrentKeyboardFocusManager());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                throw new SecurityException(notPrivileged);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * Sets the focus owner. The operation will be cancelled if the Component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * is not focusable. The focus owner is defined as the Component in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * application that will typically receive all KeyEvents generated by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * user. KeyEvents which map to the focus owner's focus traversal keys will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * not be delivered if focus traversal keys are enabled for the focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * owner. In addition, KeyEventDispatchers may retarget or consume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * KeyEvents before they reach the focus owner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * This method does not actually set the focus to the specified Component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * It merely stores the value to be subsequently returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * <code>getFocusOwner()</code>. Use <code>Component.requestFocus()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * or <code>Component.requestFocusInWindow()</code> to change the focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * owner, subject to platform limitations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * @param focusOwner the focus owner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * @see #getFocusOwner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * @see #getGlobalFocusOwner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * @see Component#requestFocus()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * @see Component#requestFocusInWindow()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * @see Component#isFocusable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    protected void setGlobalFocusOwner(Component focusOwner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        Component oldFocusOwner = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        boolean shouldFire = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        if (focusOwner == null || focusOwner.isFocusable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            synchronized (KeyboardFocusManager.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                oldFocusOwner = getFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                    fireVetoableChange("focusOwner", oldFocusOwner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                                       focusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                } catch (PropertyVetoException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                    // rejected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                KeyboardFocusManager.focusOwner = focusOwner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                if (focusOwner != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                    (getCurrentFocusCycleRoot() == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                     !focusOwner.isFocusCycleRoot(getCurrentFocusCycleRoot())))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    Container rootAncestor =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                        focusOwner.getFocusCycleRootAncestor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                    if (rootAncestor == null && (focusOwner instanceof Window))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                        rootAncestor = (Container)focusOwner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                    if (rootAncestor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                        setGlobalCurrentFocusCycleRoot(rootAncestor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                shouldFire = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        if (shouldFire) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            firePropertyChange("focusOwner", oldFocusOwner, focusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * Clears the global focus owner at both the Java and native levels. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * there exists a focus owner, that Component will receive a permanent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * FOCUS_LOST event. After this operation completes, the native windowing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * system will discard all user-generated KeyEvents until the user selects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * a new Component to receive focus, or a Component is given focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * explicitly via a call to <code>requestFocus()</code>. This operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * does not change the focused or active Windows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * @see Component#requestFocus()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * @see java.awt.event.FocusEvent#FOCUS_LOST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    public void clearGlobalFocusOwner() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        if (!GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            // Toolkit must be fully initialized, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            // _clearGlobalFocusOwner will crash or throw an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            Toolkit.getDefaultToolkit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            _clearGlobalFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    private void _clearGlobalFocusOwner() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        Window activeWindow = markClearGlobalFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        peer.clearGlobalFocusOwner(activeWindow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    Component getNativeFocusOwner() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        return peer.getCurrentFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    void setNativeFocusOwner(Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        if (focusLog.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            focusLog.log(Level.FINEST, "Calling peer {0} setCurrentFocusOwner for {1}",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                         new Object[] {peer, comp});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        peer.setCurrentFocusOwner(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    Window getNativeFocusedWindow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        return peer.getCurrentFocusedWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * Returns the permanent focus owner, if the permanent focus owner is in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * the same context as the calling thread. The permanent focus owner is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * defined as the last Component in an application to receive a permanent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * FOCUS_GAINED event. The focus owner and permanent focus owner are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * equivalent unless a temporary focus change is currently in effect. In
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * such a situation, the permanent focus owner will again be the focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * owner when the temporary focus change ends.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * @return the permanent focus owner, or null if the permanent focus owner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     *         is not a member of the calling thread's context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * @see #getGlobalPermanentFocusOwner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * @see #setGlobalPermanentFocusOwner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    public Component getPermanentFocusOwner() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        synchronized (KeyboardFocusManager.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            if (permanentFocusOwner == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            return (permanentFocusOwner.appContext ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                    AppContext.getAppContext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                ? permanentFocusOwner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * Returns the permanent focus owner, even if the calling thread is in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * different context than the permanent focus owner. The permanent focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * owner is defined as the last Component in an application to receive a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * permanent FOCUS_GAINED event. The focus owner and permanent focus owner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * are equivalent unless a temporary focus change is currently in effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * In such a situation, the permanent focus owner will again be the focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * owner when the temporary focus change ends.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * This method will throw a SecurityException if this KeyboardFocusManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * is not the current KeyboardFocusManager for the calling thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * @return the permanent focus owner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * @see #getPermanentFocusOwner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * @see #setGlobalPermanentFocusOwner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * @throws SecurityException if this KeyboardFocusManager is not the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     *         current KeyboardFocusManager for the calling thread's context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    protected Component getGlobalPermanentFocusOwner()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        throws SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        synchronized (KeyboardFocusManager.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            if (this == getCurrentKeyboardFocusManager()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                return permanentFocusOwner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                if (focusLog.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    focusLog.log(Level.FINER, "This manager is " + this + ", current is " + getCurrentKeyboardFocusManager());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                throw new SecurityException(notPrivileged);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * Sets the permanent focus owner. The operation will be cancelled if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * Component is not focusable. The permanent focus owner is defined as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * last Component in an application to receive a permanent FOCUS_GAINED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * event. The focus owner and permanent focus owner are equivalent unless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * a temporary focus change is currently in effect. In such a situation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * the permanent focus owner will again be the focus owner when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * temporary focus change ends.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * This method does not actually set the focus to the specified Component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * It merely stores the value to be subsequently returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * <code>getPermanentFocusOwner()</code>. Use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * <code>Component.requestFocus()</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * <code>Component.requestFocusInWindow()</code> to change the focus owner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * subject to platform limitations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * @param permanentFocusOwner the permanent focus owner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * @see #getPermanentFocusOwner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * @see #getGlobalPermanentFocusOwner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * @see Component#requestFocus()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * @see Component#requestFocusInWindow()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * @see Component#isFocusable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    protected void setGlobalPermanentFocusOwner(Component permanentFocusOwner)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        Component oldPermanentFocusOwner = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        boolean shouldFire = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        if (permanentFocusOwner == null || permanentFocusOwner.isFocusable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            synchronized (KeyboardFocusManager.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                oldPermanentFocusOwner = getPermanentFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                    fireVetoableChange("permanentFocusOwner",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                                       oldPermanentFocusOwner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                                       permanentFocusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                } catch (PropertyVetoException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                    // rejected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                KeyboardFocusManager.permanentFocusOwner = permanentFocusOwner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                KeyboardFocusManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                    setMostRecentFocusOwner(permanentFocusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                shouldFire = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        if (shouldFire) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            firePropertyChange("permanentFocusOwner", oldPermanentFocusOwner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                               permanentFocusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * Returns the focused Window, if the focused Window is in the same context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * as the calling thread. The focused Window is the Window that is or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * contains the focus owner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * @return the focused Window, or null if the focused Window is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     *         member of the calling thread's context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * @see #getGlobalFocusedWindow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * @see #setGlobalFocusedWindow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    public Window getFocusedWindow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        synchronized (KeyboardFocusManager.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            if (focusedWindow == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            return (focusedWindow.appContext == AppContext.getAppContext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                ? focusedWindow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * Returns the focused Window, even if the calling thread is in a different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * context than the focused Window. The focused Window is the Window that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * is or contains the focus owner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * This method will throw a SecurityException if this KeyboardFocusManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * is not the current KeyboardFocusManager for the calling thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * @return the focused Window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * @see #getFocusedWindow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * @see #setGlobalFocusedWindow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * @throws SecurityException if this KeyboardFocusManager is not the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     *         current KeyboardFocusManager for the calling thread's context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    protected Window getGlobalFocusedWindow() throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        synchronized (KeyboardFocusManager.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            if (this == getCurrentKeyboardFocusManager()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
               return focusedWindow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                if (focusLog.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                    focusLog.log(Level.FINER, "This manager is " + this + ", current is " + getCurrentKeyboardFocusManager());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                throw new SecurityException(notPrivileged);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * Sets the focused Window. The focused Window is the Window that is or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * contains the focus owner. The operation will be cancelled if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * specified Window to focus is not a focusable Window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * This method does not actually change the focused Window as far as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * native windowing system is concerned. It merely stores the value to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * subsequently returned by <code>getFocusedWindow()</code>. Use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * <code>Component.requestFocus()</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * <code>Component.requestFocusInWindow()</code> to change the focused
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * Window, subject to platform limitations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * @param focusedWindow the focused Window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * @see #getFocusedWindow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * @see #getGlobalFocusedWindow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * @see Component#requestFocus()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * @see Component#requestFocusInWindow()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * @see Window#isFocusableWindow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    protected void setGlobalFocusedWindow(Window focusedWindow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        Window oldFocusedWindow = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        boolean shouldFire = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        if (focusedWindow == null || focusedWindow.isFocusableWindow()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            synchronized (KeyboardFocusManager.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                oldFocusedWindow = getFocusedWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                    fireVetoableChange("focusedWindow", oldFocusedWindow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                                       focusedWindow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                } catch (PropertyVetoException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                    // rejected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                KeyboardFocusManager.focusedWindow = focusedWindow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                shouldFire = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        if (shouldFire) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            firePropertyChange("focusedWindow", oldFocusedWindow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                               focusedWindow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * Returns the active Window, if the active Window is in the same context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * as the calling thread. Only a Frame or a Dialog can be the active
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * Window. The native windowing system may denote the active Window or its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * children with special decorations, such as a highlighted title bar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * The active Window is always either the focused Window, or the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * Frame or Dialog that is an owner of the focused Window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * @return the active Window, or null if the active Window is not a member
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     *         of the calling thread's context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * @see #getGlobalActiveWindow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * @see #setGlobalActiveWindow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    public Window getActiveWindow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        synchronized (KeyboardFocusManager.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            if (activeWindow == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            return (activeWindow.appContext == AppContext.getAppContext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                ? activeWindow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * Returns the active Window, even if the calling thread is in a different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * context than the active Window. Only a Frame or a Dialog can be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * active Window. The native windowing system may denote the active Window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * or its children with special decorations, such as a highlighted title
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * bar. The active Window is always either the focused Window, or the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * Frame or Dialog that is an owner of the focused Window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * This method will throw a SecurityException if this KeyboardFocusManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * is not the current KeyboardFocusManager for the calling thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * @return the active Window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * @see #getActiveWindow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * @see #setGlobalActiveWindow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * @throws SecurityException if this KeyboardFocusManager is not the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     *         current KeyboardFocusManager for the calling thread's context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    protected Window getGlobalActiveWindow() throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        synchronized (KeyboardFocusManager.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            if (this == getCurrentKeyboardFocusManager()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
               return activeWindow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                if (focusLog.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                    focusLog.log(Level.FINER, "This manager is " + this + ", current is " + getCurrentKeyboardFocusManager());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                throw new SecurityException(notPrivileged);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * Sets the active Window. Only a Frame or a Dialog can be the active
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * Window. The native windowing system may denote the active Window or its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * children with special decorations, such as a highlighted title bar. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * active Window is always either the focused Window, or the first Frame or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * Dialog that is an owner of the focused Window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * This method does not actually change the active Window as far as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * native windowing system is concerned. It merely stores the value to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * subsequently returned by <code>getActiveWindow()</code>. Use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * <code>Component.requestFocus()</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * <code>Component.requestFocusInWindow()</code>to change the active
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * Window, subject to platform limitations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * @param activeWindow the active Window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * @see #getActiveWindow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * @see #getGlobalActiveWindow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * @see Component#requestFocus()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * @see Component#requestFocusInWindow()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    protected void setGlobalActiveWindow(Window activeWindow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        Window oldActiveWindow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        synchronized (KeyboardFocusManager.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            oldActiveWindow = getActiveWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            if (focusLog.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                focusLog.log(Level.FINER, "Setting global active window to " + activeWindow + ", old active " + oldActiveWindow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                fireVetoableChange("activeWindow", oldActiveWindow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                                   activeWindow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            } catch (PropertyVetoException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                // rejected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            KeyboardFocusManager.activeWindow = activeWindow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        firePropertyChange("activeWindow", oldActiveWindow, activeWindow);
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
     * Returns the default FocusTraversalPolicy. Top-level components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * use this value on their creation to initialize their own focus traversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * policy by explicit call to Container.setFocusTraversalPolicy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * @return the default FocusTraversalPolicy. null will never be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * @see #setDefaultFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * @see Container#setFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * @see Container#getFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    public synchronized FocusTraversalPolicy getDefaultFocusTraversalPolicy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        return defaultPolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * Sets the default FocusTraversalPolicy. Top-level components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * use this value on their creation to initialize their own focus traversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * policy by explicit call to Container.setFocusTraversalPolicy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * Note: this call doesn't affect already created components as they have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * their policy initialized. Only new components will use this policy as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * their default policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * @param defaultPolicy the new, default FocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * @see #getDefaultFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * @see Container#setFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * @see Container#getFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * @throws IllegalArgumentException if defaultPolicy is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    public void setDefaultFocusTraversalPolicy(FocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                                               defaultPolicy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        if (defaultPolicy == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            throw new IllegalArgumentException("default focus traversal policy cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        FocusTraversalPolicy oldPolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            oldPolicy = this.defaultPolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            this.defaultPolicy = defaultPolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        firePropertyChange("defaultFocusTraversalPolicy", oldPolicy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                           defaultPolicy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * Sets the default focus traversal keys for a given traversal operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * This traversal key <code>Set</code> will be in effect on all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * <code>Window</code>s that have no such <code>Set</code> of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * their own explicitly defined. This <code>Set</code> will also be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * inherited, recursively, by any child <code>Component</code> of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * those <code>Windows</code> that has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * no such <code>Set</code> of its own explicitly defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * The default values for the default focus traversal keys are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * implementation-dependent. Sun recommends that all implementations for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * particular native platform use the same default values. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * recommendations for Windows and Unix are listed below. These
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * recommendations are used in the Sun AWT implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * <table border=1 summary="Recommended default values for focus traversal keys">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     *    <th>Identifier</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     *    <th>Meaning</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     *    <th>Default</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     *    <td><code>KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS</code></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     *    <td>Normal forward keyboard traversal</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     *    <td><code>TAB</code> on <code>KEY_PRESSED</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     *        <code>CTRL-TAB</code> on <code>KEY_PRESSED</code></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     *    <td><code>KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS</code></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     *    <td>Normal reverse keyboard traversal</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     *    <td><code>SHIFT-TAB</code> on <code>KEY_PRESSED</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     *        <code>CTRL-SHIFT-TAB</code> on <code>KEY_PRESSED</code></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     *    <td><code>KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS</code></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     *    <td>Go up one focus traversal cycle</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     *    <td>none</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     *    <td><code>KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS</code></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     *    <td>Go down one focus traversal cycle</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     *    <td>none</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * To disable a traversal key, use an empty <code>Set</code>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * <code>Collections.EMPTY_SET</code> is recommended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * Using the <code>AWTKeyStroke</code> API, client code can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * specify on which of two
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * specific <code>KeyEvent</code>s, <code>KEY_PRESSED</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * <code>KEY_RELEASED</code>, the focus traversal operation will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * occur. Regardless of which <code>KeyEvent</code> is specified,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * however, all <code>KeyEvent</code>s related to the focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * traversal key, including the associated <code>KEY_TYPED</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * event, will be consumed, and will not be dispatched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * to any <code>Component</code>. It is a runtime error to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * specify a <code>KEY_TYPED</code> event as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * mapping to a focus traversal operation, or to map the same event to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * multiple default focus traversal operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * @param id one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     *        <code>KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     *        <code>KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     *        <code>KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS</code>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     *        <code>KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * @param keystrokes the Set of <code>AWTKeyStroke</code>s for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     *        specified operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * @see #getDefaultFocusTraversalKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * @see Component#setFocusTraversalKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * @see Component#getFocusTraversalKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * @throws IllegalArgumentException if id is not one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     *         <code>KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     *         <code>KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     *         <code>KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS</code>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     *         <code>KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     *         or if keystrokes is <code>null</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     *         or if keystrokes contains <code>null</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     *         or if any <code>Object</code> in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     *         keystrokes is not an <code>AWTKeyStroke</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     *         or if any keystroke
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     *         represents a <code>KEY_TYPED</code> event,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     *         or if any keystroke already maps
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     *         to another default focus traversal operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    public void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        setDefaultFocusTraversalKeys(int id,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                                     Set<? extends AWTKeyStroke> keystrokes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        if (id < 0 || id >= TRAVERSAL_KEY_LENGTH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            throw new IllegalArgumentException("invalid focus traversal key identifier");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        if (keystrokes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            throw new IllegalArgumentException("cannot set null Set of default focus traversal keys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        Set oldKeys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            for (Iterator iter = keystrokes.iterator(); iter.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                Object obj = iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                    throw new IllegalArgumentException("cannot set null focus traversal key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                // Fix for 6195831:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                //According to javadoc this method should throw IAE instead of ClassCastException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                if (!(obj instanceof AWTKeyStroke)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                    throw new IllegalArgumentException("object is expected to be AWTKeyStroke");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                AWTKeyStroke keystroke = (AWTKeyStroke)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                if (keystroke.getKeyChar() != KeyEvent.CHAR_UNDEFINED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                    throw new IllegalArgumentException("focus traversal keys cannot map to KEY_TYPED events");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                // Check to see if key already maps to another traversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                // operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                for (int i = 0; i < TRAVERSAL_KEY_LENGTH; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
                    if (i == id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                    if (defaultFocusTraversalKeys[i].contains(keystroke)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                        throw new IllegalArgumentException("focus traversal keys must be unique for a Component");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                    }
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
            oldKeys = defaultFocusTraversalKeys[id];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            defaultFocusTraversalKeys[id] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                Collections.unmodifiableSet(new HashSet(keystrokes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        firePropertyChange(defaultFocusTraversalKeyPropertyNames[id],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                           oldKeys, keystrokes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * Returns a Set of default focus traversal keys for a given traversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * operation. This traversal key Set will be in effect on all Windows that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * have no such Set of their own explicitly defined. This Set will also be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * inherited, recursively, by any child Component of those Windows that has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * no such Set of its own explicitly defined. (See
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * <code>setDefaultFocusTraversalKeys</code> for a full description of each
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * operation.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * @param id one of KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     *        KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     *        KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     *        KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     * @return the <code>Set</code> of <code>AWTKeyStroke</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     *         for the specified operation; the <code>Set</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     *         will be unmodifiable, and may be empty; <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     *         will never be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     * @see #setDefaultFocusTraversalKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     * @see Component#setFocusTraversalKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * @see Component#getFocusTraversalKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     * @throws IllegalArgumentException if id is not one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     *         KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     *         KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     *         KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     *         KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
    public Set<AWTKeyStroke> getDefaultFocusTraversalKeys(int id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        if (id < 0 || id >= TRAVERSAL_KEY_LENGTH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            throw new IllegalArgumentException("invalid focus traversal key identifier");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        // Okay to return Set directly because it is an unmodifiable view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        return defaultFocusTraversalKeys[id];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * Returns the current focus cycle root, if the current focus cycle root is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * in the same context as the calling thread. If the focus owner is itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     * a focus cycle root, then it may be ambiguous as to which Components
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     * represent the next and previous Components to focus during normal focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     * traversal. In that case, the current focus cycle root is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     * differentiate among the possibilities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * This method is intended to be used only by KeyboardFocusManagers and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * focus implementations. It is not for general client use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     * @return the current focus cycle root, or null if the current focus cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     *         root is not a member of the calling thread's context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * @see #getGlobalCurrentFocusCycleRoot
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * @see #setGlobalCurrentFocusCycleRoot
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
    public Container getCurrentFocusCycleRoot() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        synchronized (KeyboardFocusManager.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            if (currentFocusCycleRoot == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            return (currentFocusCycleRoot.appContext ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                    AppContext.getAppContext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                ? currentFocusCycleRoot
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * Returns the current focus cycle root, even if the calling thread is in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * different context than the current focus cycle root. If the focus owner
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * is itself a focus cycle root, then it may be ambiguous as to which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * Components represent the next and previous Components to focus during
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * normal focus traversal. In that case, the current focus cycle root is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * used to differentiate among the possibilities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * This method will throw a SecurityException if this KeyboardFocusManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * is not the current KeyboardFocusManager for the calling thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * @return the current focus cycle root, or null if the current focus cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     *         root is not a member of the calling thread's context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * @see #getCurrentFocusCycleRoot
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * @see #setGlobalCurrentFocusCycleRoot
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * @throws SecurityException if this KeyboardFocusManager is not the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     *         current KeyboardFocusManager for the calling thread's context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
    protected Container getGlobalCurrentFocusCycleRoot()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        throws SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        synchronized (KeyboardFocusManager.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
            if (this == getCurrentKeyboardFocusManager()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                return currentFocusCycleRoot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                if (focusLog.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                    focusLog.log(Level.FINER, "This manager is " + this + ", current is " + getCurrentKeyboardFocusManager());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                throw new SecurityException(notPrivileged);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * Sets the current focus cycle root. If the focus owner is itself a focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * cycle root, then it may be ambiguous as to which Components represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * the next and previous Components to focus during normal focus traversal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * In that case, the current focus cycle root is used to differentiate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * among the possibilities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * This method is intended to be used only by KeyboardFocusManagers and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     * focus implementations. It is not for general client use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * @param newFocusCycleRoot the new focus cycle root
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * @see #getCurrentFocusCycleRoot
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * @see #getGlobalCurrentFocusCycleRoot
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
    public void setGlobalCurrentFocusCycleRoot(Container newFocusCycleRoot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        Container oldFocusCycleRoot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        synchronized (KeyboardFocusManager.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
            oldFocusCycleRoot  = getCurrentFocusCycleRoot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            currentFocusCycleRoot = newFocusCycleRoot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        firePropertyChange("currentFocusCycleRoot", oldFocusCycleRoot,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
                           newFocusCycleRoot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     * Adds a PropertyChangeListener to the listener list. The listener is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     * registered for all bound properties of this class, including the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     * following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     *    <li>whether the KeyboardFocusManager is currently managing focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     *        for this application or applet's browser context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     *        ("managingFocus")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     *    <li>the focus owner ("focusOwner")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     *    <li>the permanent focus owner ("permanentFocusOwner")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     *    <li>the focused Window ("focusedWindow")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     *    <li>the active Window ("activeWindow")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     *    <li>the default focus traversal policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     *        ("defaultFocusTraversalPolicy")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     *    <li>the Set of default FORWARD_TRAVERSAL_KEYS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     *        ("forwardDefaultFocusTraversalKeys")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     *    <li>the Set of default BACKWARD_TRAVERSAL_KEYS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     *        ("backwardDefaultFocusTraversalKeys")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     *    <li>the Set of default UP_CYCLE_TRAVERSAL_KEYS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
     *        ("upCycleDefaultFocusTraversalKeys")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     *    <li>the Set of default DOWN_CYCLE_TRAVERSAL_KEYS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
     *        ("downCycleDefaultFocusTraversalKeys")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     *    <li>the current focus cycle root ("currentFocusCycleRoot")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     * If listener is null, no exception is thrown and no action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     * @param listener the PropertyChangeListener to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * @see #removePropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     * @see #getPropertyChangeListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * @see #addPropertyChangeListener(java.lang.String,java.beans.PropertyChangeListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
    public void addPropertyChangeListener(PropertyChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        if (listener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                if (changeSupport == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                    changeSupport = new PropertyChangeSupport(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
                changeSupport.addPropertyChangeListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * Removes a PropertyChangeListener from the listener list. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     * should be used to remove the PropertyChangeListeners that were
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     * registered for all bound properties of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     * If listener is null, no exception is thrown and no action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     * @param listener the PropertyChangeListener to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     * @see #addPropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     * @see #getPropertyChangeListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     * @see #removePropertyChangeListener(java.lang.String,java.beans.PropertyChangeListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
    public void removePropertyChangeListener(PropertyChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        if (listener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                if (changeSupport != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                    changeSupport.removePropertyChangeListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                }
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
     * Returns an array of all the property change listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     * registered on this keyboard focus manager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     * @return all of this keyboard focus manager's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     *         <code>PropertyChangeListener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     *         or an empty array if no property change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     *         listeners are currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     * @see #addPropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
     * @see #removePropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     * @see #getPropertyChangeListeners(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
    public synchronized PropertyChangeListener[] getPropertyChangeListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        if (changeSupport == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
            changeSupport = new PropertyChangeSupport(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        return changeSupport.getPropertyChangeListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     * Adds a PropertyChangeListener to the listener list for a specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     * property. The specified property may be user-defined, or one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     * following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     *    <li>whether the KeyboardFocusManager is currently managing focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     *        for this application or applet's browser context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     *        ("managingFocus")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     *    <li>the focus owner ("focusOwner")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     *    <li>the permanent focus owner ("permanentFocusOwner")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     *    <li>the focused Window ("focusedWindow")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     *    <li>the active Window ("activeWindow")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     *    <li>the default focus traversal policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     *        ("defaultFocusTraversalPolicy")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     *    <li>the Set of default FORWARD_TRAVERSAL_KEYS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     *        ("forwardDefaultFocusTraversalKeys")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     *    <li>the Set of default BACKWARD_TRAVERSAL_KEYS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     *        ("backwardDefaultFocusTraversalKeys")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     *    <li>the Set of default UP_CYCLE_TRAVERSAL_KEYS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     *        ("upCycleDefaultFocusTraversalKeys")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     *    <li>the Set of default DOWN_CYCLE_TRAVERSAL_KEYS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     *        ("downCycleDefaultFocusTraversalKeys")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     *    <li>the current focus cycle root ("currentFocusCycleRoot")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     * If listener is null, no exception is thrown and no action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     * @param propertyName one of the property names listed above
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     * @param listener the PropertyChangeListener to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     * @see #addPropertyChangeListener(java.beans.PropertyChangeListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     * @see #removePropertyChangeListener(java.lang.String,java.beans.PropertyChangeListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
     * @see #getPropertyChangeListeners(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
    public void addPropertyChangeListener(String propertyName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                                          PropertyChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
        if (listener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                if (changeSupport == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                    changeSupport = new PropertyChangeSupport(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                changeSupport.addPropertyChangeListener(propertyName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                                                        listener);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     * Removes a PropertyChangeListener from the listener list for a specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     * property. This method should be used to remove PropertyChangeListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     * that were registered for a specific bound property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
     * If listener is null, no exception is thrown and no action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     * @param propertyName a valid property name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     * @param listener the PropertyChangeListener to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     * @see #addPropertyChangeListener(java.lang.String,java.beans.PropertyChangeListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * @see #getPropertyChangeListeners(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     * @see #removePropertyChangeListener(java.beans.PropertyChangeListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
    public void removePropertyChangeListener(String propertyName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
                                             PropertyChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        if (listener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                if (changeSupport != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                    changeSupport.removePropertyChangeListener(propertyName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                                                               listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
     * Returns an array of all the <code>PropertyChangeListener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     * associated with the named property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     * @return all of the <code>PropertyChangeListener</code>s associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     *         the named property or an empty array if no such listeners have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     *         been added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     * @see #addPropertyChangeListener(java.lang.String,java.beans.PropertyChangeListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * @see #removePropertyChangeListener(java.lang.String,java.beans.PropertyChangeListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
    public synchronized PropertyChangeListener[] getPropertyChangeListeners(String propertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
        if (changeSupport == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
            changeSupport = new PropertyChangeSupport(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        return changeSupport.getPropertyChangeListeners(propertyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     * Fires a PropertyChangeEvent in response to a change in a bound property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     * The event will be delivered to all registered PropertyChangeListeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     * No event will be delivered if oldValue and newValue are the same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
     * @param propertyName the name of the property that has changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
     * @param oldValue the property's previous value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
     * @param newValue the property's new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
    protected void firePropertyChange(String propertyName, Object oldValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
                                      Object newValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        if (oldValue == newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        PropertyChangeSupport changeSupport = this.changeSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
        if (changeSupport != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
            changeSupport.firePropertyChange(propertyName, oldValue, newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     * Adds a VetoableChangeListener to the listener list. The listener is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     * registered for all vetoable properties of this class, including the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     * following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
     *    <li>the focus owner ("focusOwner")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
     *    <li>the permanent focus owner ("permanentFocusOwner")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
     *    <li>the focused Window ("focusedWindow")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
     *    <li>the active Window ("activeWindow")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
     * If listener is null, no exception is thrown and no action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
     * @param listener the VetoableChangeListener to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
     * @see #removeVetoableChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
     * @see #getVetoableChangeListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
     * @see #addVetoableChangeListener(java.lang.String,java.beans.VetoableChangeListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
    public void addVetoableChangeListener(VetoableChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
        if (listener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
                if (vetoableSupport == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
                    vetoableSupport =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
                        new VetoableChangeSupport(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                vetoableSupport.addVetoableChangeListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     * Removes a VetoableChangeListener from the listener list. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
     * should be used to remove the VetoableChangeListeners that were
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
     * registered for all vetoable properties of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
     * If listener is null, no exception is thrown and no action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
     * @param listener the VetoableChangeListener to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
     * @see #addVetoableChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
     * @see #getVetoableChangeListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
     * @see #removeVetoableChangeListener(java.lang.String,java.beans.VetoableChangeListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
    public void removeVetoableChangeListener(VetoableChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
        if (listener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
                if (vetoableSupport != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
                    vetoableSupport.removeVetoableChangeListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
     * Returns an array of all the vetoable change listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
     * registered on this keyboard focus manager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
     * @return all of this keyboard focus manager's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
     *         <code>VetoableChangeListener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
     *         or an empty array if no vetoable change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
     *         listeners are currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
     * @see #addVetoableChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
     * @see #removeVetoableChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
     * @see #getVetoableChangeListeners(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
    public synchronized VetoableChangeListener[] getVetoableChangeListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        if (vetoableSupport == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
            vetoableSupport = new VetoableChangeSupport(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
        return vetoableSupport.getVetoableChangeListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
     * Adds a VetoableChangeListener to the listener list for a specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
     * property. The specified property may be user-defined, or one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     * following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     *    <li>the focus owner ("focusOwner")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
     *    <li>the permanent focus owner ("permanentFocusOwner")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
     *    <li>the focused Window ("focusedWindow")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
     *    <li>the active Window ("activeWindow")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
     * If listener is null, no exception is thrown and no action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
     * @param propertyName one of the property names listed above
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
     * @param listener the VetoableChangeListener to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
     * @see #addVetoableChangeListener(java.beans.VetoableChangeListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
     * @see #removeVetoableChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
     * @see #getVetoableChangeListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
    public void addVetoableChangeListener(String propertyName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
                                          VetoableChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
        if (listener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
                if (vetoableSupport == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
                    vetoableSupport =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
                        new VetoableChangeSupport(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
                vetoableSupport.addVetoableChangeListener(propertyName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
                                                          listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
     * Removes a VetoableChangeListener from the listener list for a specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
     * property. This method should be used to remove VetoableChangeListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
     * that were registered for a specific bound property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
     * If listener is null, no exception is thrown and no action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
     * @param propertyName a valid property name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
     * @param listener the VetoableChangeListener to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
     * @see #addVetoableChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
     * @see #getVetoableChangeListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
     * @see #removeVetoableChangeListener(java.beans.VetoableChangeListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
    public void removeVetoableChangeListener(String propertyName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                                             VetoableChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
        if (listener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                if (vetoableSupport != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                    vetoableSupport.removeVetoableChangeListener(propertyName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                                                                 listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
     * Returns an array of all the <code>VetoableChangeListener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
     * associated with the named property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
     * @return all of the <code>VetoableChangeListener</code>s associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
     *         the named property or an empty array if no such listeners have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
     *         been added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
     * @see #addVetoableChangeListener(java.lang.String,java.beans.VetoableChangeListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
     * @see #removeVetoableChangeListener(java.lang.String,java.beans.VetoableChangeListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
     * @see #getVetoableChangeListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
    public synchronized VetoableChangeListener[] getVetoableChangeListeners(String propertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
        if (vetoableSupport == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
            vetoableSupport = new VetoableChangeSupport(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
        return vetoableSupport.getVetoableChangeListeners(propertyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
     * Fires a PropertyChangeEvent in response to a change in a vetoable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
     * property. The event will be delivered to all registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
     * VetoableChangeListeners. If a VetoableChangeListener throws a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     * PropertyVetoException, a new event is fired reverting all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
     * VetoableChangeListeners to the old value and the exception is then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
     * rethrown. No event will be delivered if oldValue and newValue are the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
     * same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
     * @param propertyName the name of the property that has changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
     * @param oldValue the property's previous value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
     * @param newValue the property's new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
     * @throws java.beans.PropertyVetoException if a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
     *         <code>VetoableChangeListener</code> threw
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
     *         <code>PropertyVetoException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
    protected void fireVetoableChange(String propertyName, Object oldValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
                                      Object newValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
        throws PropertyVetoException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
        if (oldValue == newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
        VetoableChangeSupport vetoableSupport =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
            this.vetoableSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
        if (vetoableSupport != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
            vetoableSupport.fireVetoableChange(propertyName, oldValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
                                               newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
     * Adds a KeyEventDispatcher to this KeyboardFocusManager's dispatcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
     * chain. This KeyboardFocusManager will request that each
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
     * KeyEventDispatcher dispatch KeyEvents generated by the user before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
     * finally dispatching the KeyEvent itself. KeyEventDispatchers will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
     * notified in the order in which they were added. Notifications will halt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
     * as soon as one KeyEventDispatcher returns <code>true</code> from its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
     * <code>dispatchKeyEvent</code> method. There is no limit to the total
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
     * number of KeyEventDispatchers which can be added, nor to the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
     * times which a particular KeyEventDispatcher instance can be added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
     * If a null dispatcher is specified, no action is taken and no exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
     * is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     * In a multithreaded application, {@link KeyEventDispatcher} behaves
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     * the same as other AWT listeners.  See
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
     * <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
     * >AWT Threading Issues</a> for more details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
     * @param dispatcher the KeyEventDispatcher to add to the dispatcher chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
     * @see #removeKeyEventDispatcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
    public void addKeyEventDispatcher(KeyEventDispatcher dispatcher) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
        if (dispatcher != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
                if (keyEventDispatchers == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
                    keyEventDispatchers = new java.util.LinkedList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
                keyEventDispatchers.add(dispatcher);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
     * Removes a KeyEventDispatcher which was previously added to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
     * KeyboardFocusManager's dispatcher chain. This KeyboardFocusManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
     * cannot itself be removed, unless it was explicitly re-registered via a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
     * call to <code>addKeyEventDispatcher</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
     * If a null dispatcher is specified, if the specified dispatcher is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
     * in the dispatcher chain, or if this KeyboardFocusManager is specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
     * without having been explicitly re-registered, no action is taken and no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
     * exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
     * In a multithreaded application, {@link KeyEventDispatcher} behaves
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
     * the same as other AWT listeners.  See
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
     * <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
     * >AWT Threading Issues</a> for more details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
     * @param dispatcher the KeyEventDispatcher to remove from the dispatcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
     *        chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
     * @see #addKeyEventDispatcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
    public void removeKeyEventDispatcher(KeyEventDispatcher dispatcher) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
        if (dispatcher != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
                if (keyEventDispatchers != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
                    keyEventDispatchers.remove(dispatcher);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
     * Returns this KeyboardFocusManager's KeyEventDispatcher chain as a List.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
     * The List will not include this KeyboardFocusManager unless it was
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
     * explicitly re-registered via a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
     * <code>addKeyEventDispatcher</code>. If no other KeyEventDispatchers are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
     * registered, implementations are free to return null or a List of length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
     * 0. Client code should not assume one behavior over another, nor should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
     * it assume that the behavior, once established, will not change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
     * @return a possibly null or empty List of KeyEventDispatchers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
     * @see #addKeyEventDispatcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
     * @see #removeKeyEventDispatcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
    protected synchronized java.util.List<KeyEventDispatcher>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
        getKeyEventDispatchers()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
        return (keyEventDispatchers != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
            ? (java.util.List)keyEventDispatchers.clone()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
            : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
     * Adds a KeyEventPostProcessor to this KeyboardFocusManager's post-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
     * processor chain. After a KeyEvent has been dispatched to and handled by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
     * its target, KeyboardFocusManager will request that each
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
     * KeyEventPostProcessor perform any necessary post-processing as part
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
     * of the KeyEvent's final resolution. KeyEventPostProcessors
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
     * will be notified in the order in which they were added; the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
     * KeyboardFocusManager will be notified last. Notifications will halt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
     * as soon as one KeyEventPostProcessor returns <code>true</code> from its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
     * <code>postProcessKeyEvent</code> method. There is no limit to the the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
     * total number of KeyEventPostProcessors that can be added, nor to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
     * number of times that a particular KeyEventPostProcessor instance can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
     * added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
     * If a null post-processor is specified, no action is taken and no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
     * exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
     * In a multithreaded application, {@link KeyEventPostProcessor} behaves
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
     * the same as other AWT listeners.  See
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
     * <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
     * >AWT Threading Issues</a> for more details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
     * @param processor the KeyEventPostProcessor to add to the post-processor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
     *        chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
     * @see #removeKeyEventPostProcessor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
    public void addKeyEventPostProcessor(KeyEventPostProcessor processor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
        if (processor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
                if (keyEventPostProcessors == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
                    keyEventPostProcessors = new java.util.LinkedList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
                keyEventPostProcessors.add(processor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
     * Removes a previously added KeyEventPostProcessor from this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
     * KeyboardFocusManager's post-processor chain. This KeyboardFocusManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
     * cannot itself be entirely removed from the chain. Only additional
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
     * references added via <code>addKeyEventPostProcessor</code> can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
     * removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
     * If a null post-processor is specified, if the specified post-processor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
     * is not in the post-processor chain, or if this KeyboardFocusManager is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
     * specified without having been explicitly added, no action is taken and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
     * no exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
     * In a multithreaded application, {@link KeyEventPostProcessor} behaves
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
     * the same as other AWT listeners.  See
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
     * <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
     * >AWT Threading Issues</a> for more details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
     * @param processor the KeyEventPostProcessor to remove from the post-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
     *        processor chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
     * @see #addKeyEventPostProcessor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
    public void removeKeyEventPostProcessor(KeyEventPostProcessor processor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
        if (processor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
                if (keyEventPostProcessors != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
                    keyEventPostProcessors.remove(processor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
     * Returns this KeyboardFocusManager's KeyEventPostProcessor chain as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
     * List. The List will not include this KeyboardFocusManager unless it was
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
     * explicitly added via a call to <code>addKeyEventPostProcessor</code>. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
     * no KeyEventPostProcessors are registered, implementations are free to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
     * return null or a List of length 0. Client code should not assume one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
     * behavior over another, nor should it assume that the behavior, once
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
     * established, will not change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
     * @return a possibly null or empty List of KeyEventPostProcessors
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
     * @see #addKeyEventPostProcessor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
     * @see #removeKeyEventPostProcessor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
    protected java.util.List<KeyEventPostProcessor>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
        getKeyEventPostProcessors()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
        return (keyEventPostProcessors != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
            ? (java.util.List)keyEventPostProcessors.clone()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
            : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
    static void setMostRecentFocusOwner(Component component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
        Component window = component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
        while (window != null && !(window instanceof Window)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
            window = window.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
        if (window != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
            setMostRecentFocusOwner((Window)window, component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
    static synchronized void setMostRecentFocusOwner(Window window,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
                                                     Component component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
        // ATTN: component has a strong reference to window via chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
        // of Component.parent fields.  Since WeakHasMap refers to its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
        // values strongly, we need to break the strong link from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
        // value (component) back to its key (window).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
        WeakReference weakValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
        if (component != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
            weakValue = new WeakReference(component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
        mostRecentFocusOwners.put(window, weakValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
    static void clearMostRecentFocusOwner(Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
        Container window;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
        if (comp == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
        synchronized (comp.getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
            window = comp.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
            while (window != null && !(window instanceof Window)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
                window = window.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
        synchronized (KeyboardFocusManager.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
            if ((window != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
                && (getMostRecentFocusOwner((Window)window) == comp))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
                setMostRecentFocusOwner((Window)window, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
            // Also clear temporary lost component stored in Window
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
            if (window != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
                Window realWindow = (Window)window;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
                if (realWindow.getTemporaryLostComponent() == comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
                    realWindow.setTemporaryLostComponent(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
     * Please be careful changing this method! It is called from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
     * javax.swing.JComponent.runInputVerifier() using reflection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
    static synchronized Component getMostRecentFocusOwner(Window window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
        WeakReference weakValue =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
            (WeakReference)mostRecentFocusOwners.get(window);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
        return weakValue == null ? null : (Component)weakValue.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
     * This method is called by the AWT event dispatcher requesting that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
     * current KeyboardFocusManager dispatch the specified event on its behalf.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
     * It is expected that all KeyboardFocusManagers will dispatch all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
     * FocusEvents, all WindowEvents related to focus, and all KeyEvents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
     * These events should be dispatched based on the KeyboardFocusManager's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
     * notion of the focus owner and the focused and active Windows, sometimes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
     * overriding the source of the specified AWTEvent. Dispatching must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
     * done using <code>redispatchEvent</code> to prevent the AWT event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
     * dispatcher from recursively requesting that the KeyboardFocusManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
     * dispatch the event again. If this method returns <code>false</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
     * then the AWT event dispatcher will attempt to dispatch the event itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
     * @param e the AWTEvent to be dispatched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
     * @return <code>true</code> if this method dispatched the event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
     *         <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
     * @see #redispatchEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
     * @see #dispatchKeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
    public abstract boolean dispatchEvent(AWTEvent e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
     * Redispatches an AWTEvent in such a way that the AWT event dispatcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
     * will not recursively request that the KeyboardFocusManager, or any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
     * installed KeyEventDispatchers, dispatch the event again. Client
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
     * implementations of <code>dispatchEvent</code> and client-defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
     * KeyEventDispatchers must call <code>redispatchEvent(target, e)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
     * instead of <code>target.dispatchEvent(e)</code> to dispatch an event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
     * This method is intended to be used only by KeyboardFocusManagers and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
     * KeyEventDispatchers. It is not for general client use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
     * @param target the Component to which the event should be dispatched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
     * @param e the event to dispatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
     * @see #dispatchEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
     * @see KeyEventDispatcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
    public final void redispatchEvent(Component target, AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
        e.focusManagerIsDispatching = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
        target.dispatchEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
        e.focusManagerIsDispatching = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
     * Typically this method will be called by <code>dispatchEvent</code> if no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
     * other KeyEventDispatcher in the dispatcher chain dispatched the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
     * KeyEvent, or if no other KeyEventDispatchers are registered. If an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
     * implementation of this method returns <code>false</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
     * <code>dispatchEvent</code> may try to dispatch the KeyEvent itself, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
     * may simply return <code>false</code>. If <code>true</code> is returned,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
     * <code>dispatchEvent</code> should return <code>true</code> as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
     * @param e the KeyEvent which the current KeyboardFocusManager has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
     *        requested that this KeyEventDispatcher dispatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
     * @return <code>true</code> if the KeyEvent was dispatched;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
     *         <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
     * @see #dispatchEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
    public abstract boolean dispatchKeyEvent(KeyEvent e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
     * This method will be called by <code>dispatchKeyEvent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
     * By default, this method will handle any unconsumed KeyEvents that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
     * map to an AWT <code>MenuShortcut</code> by consuming the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
     * and activating the shortcut.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
     * @param e the KeyEvent to post-process
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
     * @return <code>true</code> to indicate that no other
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
     *         KeyEventPostProcessor will be notified of the KeyEvent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
     * @see #dispatchKeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
     * @see MenuShortcut
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
    public abstract boolean postProcessKeyEvent(KeyEvent e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
     * This method initiates a focus traversal operation if and only if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
     * KeyEvent represents a focus traversal key for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
     * focusedComponent. It is expected that focusedComponent is the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
     * focus owner, although this need not be the case. If it is not,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
     * focus traversal will nevertheless proceed as if focusedComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
     * were the current focus owner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
     * @param focusedComponent the Component that will be the basis for a focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
     *        traversal operation if the specified event represents a focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
     *        traversal key for the Component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
     * @param e the event that may represent a focus traversal key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
    public abstract void processKeyEvent(Component focusedComponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
                                         KeyEvent e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
     * Called by the AWT to notify the KeyboardFocusManager that it should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
     * delay dispatching of KeyEvents until the specified Component becomes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
     * the focus owner. If client code requests a focus change, and the AWT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
     * determines that this request might be granted by the native windowing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
     * system, then the AWT will call this method. It is the responsibility of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
     * the KeyboardFocusManager to delay dispatching of KeyEvents with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
     * timestamps later than the specified time stamp until the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
     * Component receives a FOCUS_GAINED event, or the AWT cancels the delay
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
     * request by invoking <code>dequeueKeyEvents</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
     * <code>discardKeyEvents</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
     * @param after timestamp of current event, or the current, system time if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
     *        the current event has no timestamp, or the AWT cannot determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
     *        which event is currently being handled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
     * @param untilFocused Component which should receive a FOCUS_GAINED event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
     *        before any pending KeyEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
     * @see #dequeueKeyEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
     * @see #discardKeyEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
    protected abstract void enqueueKeyEvents(long after,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
                                             Component untilFocused);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
     * Called by the AWT to notify the KeyboardFocusManager that it should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
     * cancel delayed dispatching of KeyEvents. All KeyEvents which were
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
     * enqueued because of a call to <code>enqueueKeyEvents</code> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
     * same timestamp and Component should be released for normal dispatching
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
     * to the current focus owner. If the given timestamp is less than zero,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
     * the outstanding enqueue request for the given Component with the <b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
     * oldest</b> timestamp (if any) should be cancelled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
     * @param after the timestamp specified in the call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
     *        <code>enqueueKeyEvents</code>, or any value < 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
     * @param untilFocused the Component specified in the call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
     *        <code>enqueueKeyEvents</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
     * @see #enqueueKeyEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
     * @see #discardKeyEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
    protected abstract void dequeueKeyEvents(long after,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
                                             Component untilFocused);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
     * Called by the AWT to notify the KeyboardFocusManager that it should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
     * cancel delayed dispatching of KeyEvents. All KeyEvents which were
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
     * enqueued because of one or more calls to <code>enqueueKeyEvents</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
     * with the same Component should be discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
     * @param comp the Component specified in one or more calls to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
     *        <code>enqueueKeyEvents</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
     * @see #enqueueKeyEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
     * @see #dequeueKeyEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
    protected abstract void discardKeyEvents(Component comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
     * Focuses the Component after aComponent, typically based on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
     * FocusTraversalPolicy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
     * @param aComponent the Component that is the basis for the focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
     *        traversal operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
     * @see FocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
    public abstract void focusNextComponent(Component aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
     * Focuses the Component before aComponent, typically based on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
     * FocusTraversalPolicy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
     * @param aComponent the Component that is the basis for the focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
     *        traversal operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
     * @see FocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
    public abstract void focusPreviousComponent(Component aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
     * Moves the focus up one focus traversal cycle. Typically, the focus owner
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
     * is set to aComponent's focus cycle root, and the current focus cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
     * root is set to the new focus owner's focus cycle root. If, however,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
     * aComponent's focus cycle root is a Window, then typically the focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
     * owner is set to the Window's default Component to focus, and the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
     * focus cycle root is unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
     * @param aComponent the Component that is the basis for the focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
     *        traversal operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
    public abstract void upFocusCycle(Component aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
     * Moves the focus down one focus traversal cycle. Typically, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
     * aContainer is a focus cycle root, then the focus owner is set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
     * aContainer's default Component to focus, and the current focus cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
     * root is set to aContainer. If aContainer is not a focus cycle root, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
     * no focus traversal operation occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
     * @param aContainer the Container that is the basis for the focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
     *        traversal operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
    public abstract void downFocusCycle(Container aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
     * Focuses the Component after the current focus owner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
    public final void focusNextComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
        Component focusOwner = getFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
        if (focusOwner != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
            focusNextComponent(focusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
     * Focuses the Component before the current focus owner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
    public final void focusPreviousComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
        Component focusOwner = getFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
        if (focusOwner != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
            focusPreviousComponent(focusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
     * Moves the focus up one focus traversal cycle from the current focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
     * owner. Typically, the new focus owner is set to the current focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
     * owner's focus cycle root, and the current focus cycle root is set to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
     * new focus owner's focus cycle root. If, however, the current focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
     * owner's focus cycle root is a Window, then typically the focus owner is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
     * set to the focus cycle root's default Component to focus, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
     * current focus cycle root is unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
    public final void upFocusCycle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
        Component focusOwner = getFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
        if (focusOwner != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
            upFocusCycle(focusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
     * Moves the focus down one focus traversal cycle from the current focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
     * owner, if and only if the current focus owner is a Container that is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
     * focus cycle root. Typically, the focus owner is set to the current focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
     * owner's default Component to focus, and the current focus cycle root is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
     * set to the current focus owner. If the current focus owner is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
     * Container that is a focus cycle root, then no focus traversal operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
     * occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
    public final void downFocusCycle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
        Component focusOwner = getFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
        if (focusOwner instanceof Container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
            downFocusCycle((Container)focusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
     * Dumps the list of focus requests to stderr
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
    void dumpRequests() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
        System.err.println(">>> Requests dump, time: " + System.currentTimeMillis());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
        synchronized (heavyweightRequests) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
            for (HeavyweightFocusRequest req : heavyweightRequests) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
                System.err.println(">>> Req: " + req);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
        System.err.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
    private static final class LightweightFocusRequest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
        final Component component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
        final boolean temporary;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
        final CausedFocusEvent.Cause cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
        LightweightFocusRequest(Component component, boolean temporary, CausedFocusEvent.Cause cause) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
            this.component = component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
            this.temporary = temporary;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
            this.cause = cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
            return "LightweightFocusRequest[component=" + component +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
                ",temporary=" + temporary + ", cause=" + cause + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
    private static final class HeavyweightFocusRequest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
        final Component heavyweight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
        final LinkedList<LightweightFocusRequest> lightweightRequests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
        static final HeavyweightFocusRequest CLEAR_GLOBAL_FOCUS_OWNER =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
            new HeavyweightFocusRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
        private HeavyweightFocusRequest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
            heavyweight = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
            lightweightRequests = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
        HeavyweightFocusRequest(Component heavyweight, Component descendant,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
                                boolean temporary, CausedFocusEvent.Cause cause) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
            if (log.isLoggable(Level.FINE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
                if (heavyweight == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
                    log.log(Level.FINE, "Assertion (heavyweight != null) failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
            this.heavyweight = heavyweight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
            this.lightweightRequests = new LinkedList<LightweightFocusRequest>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
            addLightweightRequest(descendant, temporary, cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
        boolean addLightweightRequest(Component descendant,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
                                      boolean temporary, CausedFocusEvent.Cause cause) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
            if (log.isLoggable(Level.FINE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
                if (this == HeavyweightFocusRequest.CLEAR_GLOBAL_FOCUS_OWNER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
                    log.log(Level.FINE, "Assertion (this != HeavyweightFocusRequest.CLEAR_GLOBAL_FOCUS_OWNER) failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
                if (descendant == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
                    log.log(Level.FINE, "Assertion (descendant != null) failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
            Component lastDescendant = ((lightweightRequests.size() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
                ? lightweightRequests.getLast().component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
                : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
            if (descendant != lastDescendant) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
                // Not a duplicate request
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
                lightweightRequests.add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
                    (new LightweightFocusRequest(descendant, temporary, cause));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
        LightweightFocusRequest getFirstLightweightRequest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
            if (this == CLEAR_GLOBAL_FOCUS_OWNER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
            return lightweightRequests.getFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
            boolean first = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
            String str = "HeavyweightFocusRequest[heavweight=" + heavyweight +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
                ",lightweightRequests=";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
            if (lightweightRequests == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
                str += null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
                str += "[";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
                for (LightweightFocusRequest lwRequest : lightweightRequests) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
                    if (first) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
                        first = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
                        str += ",";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
                    str += lwRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
                str += "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
            str += "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
            return str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
     * heavyweightRequests is used as a monitor for synchronized changes of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
     * currentLightweightRequests, clearingCurrentLightweightRequests and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
     * newFocusOwner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
    private static LinkedList<HeavyweightFocusRequest> heavyweightRequests =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
        new LinkedList<HeavyweightFocusRequest>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
    private static LinkedList<LightweightFocusRequest> currentLightweightRequests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
    private static boolean clearingCurrentLightweightRequests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
    private static boolean allowSyncFocusRequests = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
    private static Component newFocusOwner = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
    private static volatile boolean disableRestoreFocus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
    static final int SNFH_FAILURE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
    static final int SNFH_SUCCESS_HANDLED = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
    static final int SNFH_SUCCESS_PROCEED = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
    static boolean processSynchronousLightweightTransfer(Component heavyweight, Component descendant,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
                                                  boolean temporary, boolean focusedWindowChangeAllowed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
                                                  long time)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
        Window parentWindow = Component.getContainingWindow(heavyweight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
        if (parentWindow == null || !parentWindow.syncLWRequests) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
        if (descendant == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
            // Focus transfers from a lightweight child back to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
            // heavyweight Container should be treated like lightweight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
            // focus transfers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
            descendant = heavyweight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
        KeyboardFocusManager manager = getCurrentKeyboardFocusManager(SunToolkit.targetToAppContext(descendant));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
        FocusEvent currentFocusOwnerEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
        FocusEvent newFocusOwnerEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
        Component currentFocusOwner = manager.getGlobalFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
        synchronized (heavyweightRequests) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
            HeavyweightFocusRequest hwFocusRequest = getLastHWRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
            if (hwFocusRequest == null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
                heavyweight == manager.getNativeFocusOwner() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
                allowSyncFocusRequests)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
                if (descendant == currentFocusOwner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
                    // Redundant request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
                // 'heavyweight' owns the native focus and there are no pending
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
                // requests. 'heavyweight' must be a Container and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
                // 'descendant' must not be the focus owner. Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
                // we would never have gotten this far.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
                manager.enqueueKeyEvents(time, descendant);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
                hwFocusRequest =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
                    new HeavyweightFocusRequest(heavyweight, descendant,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
                                                temporary, CausedFocusEvent.Cause.UNKNOWN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
                heavyweightRequests.add(hwFocusRequest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
                if (currentFocusOwner != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
                    currentFocusOwnerEvent =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
                        new FocusEvent(currentFocusOwner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
                                       FocusEvent.FOCUS_LOST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
                                       temporary, descendant);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
                newFocusOwnerEvent =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
                    new FocusEvent(descendant, FocusEvent.FOCUS_GAINED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
                                   temporary, currentFocusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
        boolean result = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
        final boolean clearing = clearingCurrentLightweightRequests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
        Throwable caughtEx = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
            clearingCurrentLightweightRequests = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
            synchronized(Component.LOCK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
                if (currentFocusOwnerEvent != null && currentFocusOwner != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
                    ((AWTEvent) currentFocusOwnerEvent).isPosted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
                    caughtEx = dispatchAndCatchException(caughtEx, currentFocusOwner, currentFocusOwnerEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
                    result = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
                if (newFocusOwnerEvent != null && descendant != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
                    ((AWTEvent) newFocusOwnerEvent).isPosted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
                    caughtEx = dispatchAndCatchException(caughtEx, descendant, newFocusOwnerEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
                    result = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
            clearingCurrentLightweightRequests = clearing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
        if (caughtEx instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
            throw (RuntimeException)caughtEx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
        } else if (caughtEx instanceof Error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
            throw (Error)caughtEx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
     * Indicates whether the native implementation should proceed with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
     * pending, native focus request. Before changing the focus at the native
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
     * level, the AWT implementation should always call this function for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
     * permission. This function will reject the request if a duplicate request
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
     * preceded it, or if the specified heavyweight Component already owns the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
     * focus and no native focus changes are pending. Otherwise, the request
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
     * will be approved and the focus request list will be updated so that,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
     * if necessary, the proper descendant will be focused when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
     * corresponding FOCUS_GAINED event on the heavyweight is received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
     * An implementation must ensure that calls to this method and native
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
     * focus changes are atomic. If this is not guaranteed, then the ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
     * of the focus request list may be incorrect, leading to errors in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
     * type-ahead mechanism. Typically this is accomplished by only calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
     * this function from the native event pumping thread, or by holding a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
     * global, native lock during invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
    static int shouldNativelyFocusHeavyweight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
        (Component heavyweight, Component descendant, boolean temporary,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
         boolean focusedWindowChangeAllowed, long time, CausedFocusEvent.Cause cause)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
        if (log.isLoggable(Level.FINE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
            if (heavyweight == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
                log.log(Level.FINE, "Assertion (heavyweight != null) failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
            if (time == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
                log.log(Level.FINE, "Assertion (time != 0) failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
        if (descendant == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
            // Focus transfers from a lightweight child back to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
            // heavyweight Container should be treated like lightweight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
            // focus transfers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
            descendant = heavyweight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
        KeyboardFocusManager manager =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
            getCurrentKeyboardFocusManager(SunToolkit.targetToAppContext(descendant));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
        KeyboardFocusManager thisManager = getCurrentKeyboardFocusManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
        Component currentFocusOwner = thisManager.getGlobalFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
        Component nativeFocusOwner = thisManager.getNativeFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
        Window nativeFocusedWindow = thisManager.getNativeFocusedWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
        if (focusLog.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
            focusLog.log(Level.FINER, "SNFH for {0} in {1}",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
                         new Object[] {descendant, heavyweight});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
        if (focusLog.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
            focusLog.log(Level.FINEST, "0. Current focus owner {0}",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
                         currentFocusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
            focusLog.log(Level.FINEST, "0. Native focus owner {0}",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
                         nativeFocusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
            focusLog.log(Level.FINEST, "0. Native focused window {0}",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
                         nativeFocusedWindow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
        synchronized (heavyweightRequests) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
            HeavyweightFocusRequest hwFocusRequest = getLastHWRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
            if (focusLog.isLoggable(Level.FINEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
                focusLog.log(Level.FINEST, "Request {0}", hwFocusRequest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
            if (hwFocusRequest == null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
                heavyweight == nativeFocusOwner)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
                if (descendant == currentFocusOwner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
                    // Redundant request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
                    if (focusLog.isLoggable(Level.FINEST))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
                        focusLog.log(Level.FINEST, "1. SNFH_FAILURE for {0}",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
                                     descendant);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
                    return SNFH_FAILURE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
                // 'heavyweight' owns the native focus and there are no pending
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
                // requests. 'heavyweight' must be a Container and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
                // 'descendant' must not be the focus owner. Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
                // we would never have gotten this far.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
                manager.enqueueKeyEvents(time, descendant);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
                hwFocusRequest =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
                    new HeavyweightFocusRequest(heavyweight, descendant,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
                                                temporary, cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
                heavyweightRequests.add(hwFocusRequest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
                if (currentFocusOwner != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
                    FocusEvent currentFocusOwnerEvent =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
                        new CausedFocusEvent(currentFocusOwner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
                                       FocusEvent.FOCUS_LOST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
                                       temporary, descendant, cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
                    // Fix 5028014. Rolled out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
                    // SunToolkit.postPriorityEvent(currentFocusOwnerEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
                    SunToolkit.postEvent(currentFocusOwner.appContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
                                         currentFocusOwnerEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
                FocusEvent newFocusOwnerEvent =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
                    new CausedFocusEvent(descendant, FocusEvent.FOCUS_GAINED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
                                   temporary, currentFocusOwner, cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
                // Fix 5028014. Rolled out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
                // SunToolkit.postPriorityEvent(newFocusOwnerEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
                SunToolkit.postEvent(descendant.appContext, newFocusOwnerEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
                if (focusLog.isLoggable(Level.FINEST))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
                    focusLog.log(Level.FINEST, "2. SNFH_HANDLED for {0}", descendant);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
                return SNFH_SUCCESS_HANDLED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
            } else if (hwFocusRequest != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
                       hwFocusRequest.heavyweight == heavyweight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
                // 'heavyweight' doesn't have the native focus right now, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
                // if all pending requests were completed, it would. Add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
                // descendant to the heavyweight's list of pending
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
                // lightweight focus transfers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
                if (hwFocusRequest.addLightweightRequest(descendant,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
                                                         temporary, cause)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
                    manager.enqueueKeyEvents(time, descendant);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
                if (focusLog.isLoggable(Level.FINEST))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
                    focusLog.finest("3. SNFH_HANDLED for lightweight" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
                                    descendant + " in " + heavyweight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
                return SNFH_SUCCESS_HANDLED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
                if (!focusedWindowChangeAllowed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
                    // For purposes of computing oldFocusedWindow, we should look at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
                    // the second to last HeavyweightFocusRequest on the queue iff the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
                    // last HeavyweightFocusRequest is CLEAR_GLOBAL_FOCUS_OWNER. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
                    // there is no second to last HeavyweightFocusRequest, null is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
                    // acceptable value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
                    if (hwFocusRequest ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
                        HeavyweightFocusRequest.CLEAR_GLOBAL_FOCUS_OWNER)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
                        int size = heavyweightRequests.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
                        hwFocusRequest = (HeavyweightFocusRequest)((size >= 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
                            ? heavyweightRequests.get(size - 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
                            : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
                    if (focusedWindowChanged(heavyweight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
                                             (hwFocusRequest != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
                                             ? hwFocusRequest.heavyweight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
                                             : nativeFocusedWindow)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
                        if (focusLog.isLoggable(Level.FINEST))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
                            focusLog.finest("4. SNFH_FAILURE for " + descendant);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
                        return SNFH_FAILURE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
                manager.enqueueKeyEvents(time, descendant);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
                heavyweightRequests.add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
                    (new HeavyweightFocusRequest(heavyweight, descendant,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
                                                 temporary, cause));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
                if (focusLog.isLoggable(Level.FINEST))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
                    focusLog.finest("5. SNFH_PROCEED for " + descendant);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
                return SNFH_SUCCESS_PROCEED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
    static void heavyweightButtonDown(Component heavyweight, long time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
        heavyweightButtonDown(heavyweight, time, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
    static void heavyweightButtonDown(Component heavyweight, long time, boolean acceptDuplicates) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
        if (log.isLoggable(Level.FINE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
            if (heavyweight == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
                log.log(Level.FINE, "Assertion (heavyweight != null) failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
            if (time == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
                log.log(Level.FINE, "Assertion (time != 0) failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
        KeyboardFocusManager manager = getCurrentKeyboardFocusManager(SunToolkit.targetToAppContext(heavyweight));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
        synchronized (heavyweightRequests) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
            HeavyweightFocusRequest hwFocusRequest = getLastHWRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
            Component currentNativeFocusOwner = (hwFocusRequest == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
                ? manager.getNativeFocusOwner()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
                : hwFocusRequest.heavyweight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
            // Behavior for all use cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
            // 1. Heavyweight leaf Components (e.g., Button, Checkbox, Choice,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
            //    List, TextComponent, Canvas) that respond to button down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
            //    Native platform will generate a FOCUS_GAINED if and only if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
            //    the Component is not the focus owner (or, will not be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
            //    focus owner when all outstanding focus requests are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
            //    processed).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
            // 2. Panel with no descendants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
            //    Same as (1).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
            // 3. Panel with at least one heavyweight descendant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
            //    This function should NOT be called for this case!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
            // 4. Panel with only lightweight descendants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
            //    Native platform will generate a FOCUS_GAINED if and only if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
            //    neither the Panel, nor any of its recursive, lightweight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
            //    descendants, is the focus owner. However, we want a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
            //    requestFocus() for any lightweight descendant to win out over
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
            //    the focus request for the Panel. To accomplish this, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
            //    differ from the algorithm for shouldNativelyFocusHeavyweight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
            //    as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
            //      a. If the requestFocus() for a lightweight descendant has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
            //         been fully handled by the time this function is invoked,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
            //         then 'hwFocusRequest' will be null and 'heavyweight'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
            //         will be the native focus owner. Do *not* synthesize a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
            //         focus transfer to the Panel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
            //      b. If the requestFocus() for a lightweight descendant has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
            //         been recorded, but not handled, then 'hwFocusRequest'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
            //         will be non-null and 'hwFocusRequest.heavyweight' will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
            //         equal 'heavyweight'. Do *not* append 'heavyweight' to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
            //         hwFocusRequest.lightweightRequests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
            //      c. If the requestFocus() for a lightweight descendant is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
            //         yet to be made, then post a new HeavyweightFocusRequest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
            //         If no lightweight descendant ever requests focus, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
            //         the Panel will get focus. If some descendant does, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
            //         the descendant will get focus by either a synthetic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
            //         focus transfer, or a lightweightRequests focus transfer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
            if (acceptDuplicates || heavyweight != currentNativeFocusOwner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
                getCurrentKeyboardFocusManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
                    (SunToolkit.targetToAppContext(heavyweight)).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
                    enqueueKeyEvents(time, heavyweight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
                heavyweightRequests.add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
                    (new HeavyweightFocusRequest(heavyweight, heavyweight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
                                                 false, CausedFocusEvent.Cause.MOUSE_EVENT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
     * Returns the Window which will be active after processing this request,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
     * or null if this is a duplicate request. The active Window is useful
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
     * because some native platforms do not support setting the native focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
     * owner to null. On these platforms, the obvious choice is to set the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
     * focus owner to the focus proxy of the active Window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
    static Window markClearGlobalFocusOwner() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
        // need to call this out of synchronized block to avoid possible deadlock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
        // see 6454631.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
        final Component nativeFocusedWindow =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
                getCurrentKeyboardFocusManager().getNativeFocusedWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
        synchronized (heavyweightRequests) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
            HeavyweightFocusRequest hwFocusRequest = getLastHWRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
            if (hwFocusRequest ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
                HeavyweightFocusRequest.CLEAR_GLOBAL_FOCUS_OWNER)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
                // duplicate request
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
            heavyweightRequests.add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
                (HeavyweightFocusRequest.CLEAR_GLOBAL_FOCUS_OWNER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
            Component activeWindow = ((hwFocusRequest != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
                ? Component.getContainingWindow(hwFocusRequest.heavyweight)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
                : nativeFocusedWindow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
            while (activeWindow != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
                   !((activeWindow instanceof Frame) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
                     (activeWindow instanceof Dialog)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
                activeWindow = activeWindow.getParent_NoClientCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
            return (Window) activeWindow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
    Component getCurrentWaitingRequest(Component parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
        synchronized (heavyweightRequests) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
            HeavyweightFocusRequest hwFocusRequest = getFirstHWRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
            if (hwFocusRequest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
                if (hwFocusRequest.heavyweight == parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
                    LightweightFocusRequest lwFocusRequest =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
                        hwFocusRequest.lightweightRequests.getFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
                    if (lwFocusRequest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
                        return lwFocusRequest.component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
    static boolean isAutoFocusTransferEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
        synchronized (heavyweightRequests) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
            return (heavyweightRequests.size() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
                    && !disableRestoreFocus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
                    && (null == currentLightweightRequests);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
     * Used to process exceptions in dispatching focus event (in focusLost/focusGained callbacks).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
     * @param ex previously caught exception that may be processed right here, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
     * @param comp the component to dispatch the event to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
     * @param event the event to dispatch to the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
    static private Throwable dispatchAndCatchException(Throwable ex, Component comp, FocusEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
        Throwable retEx = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
            comp.dispatchEvent(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
        } catch (RuntimeException re) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
            retEx = re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
        } catch (Error er) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
            retEx = er;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
        if (retEx != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
            if (ex != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
                handleException(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
            return retEx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
        return ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
    static private void handleException(Throwable ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
        ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
    static void processCurrentLightweightRequests() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
        KeyboardFocusManager manager = getCurrentKeyboardFocusManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
        LinkedList<LightweightFocusRequest> localLightweightRequests = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
        Component globalFocusOwner = manager.getGlobalFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
        if ((globalFocusOwner != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
            (globalFocusOwner.appContext != AppContext.getAppContext()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
            // The current app context differs from the app context of a focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
            // owner (and all pending lightweight requests), so we do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
            // now and wait for a next event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
        synchronized(heavyweightRequests) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
            if (currentLightweightRequests != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
                clearingCurrentLightweightRequests = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
                disableRestoreFocus = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
                localLightweightRequests = currentLightweightRequests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
                allowSyncFocusRequests = (localLightweightRequests.size() < 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
                currentLightweightRequests = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
                // do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
        Throwable caughtEx = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
            if (localLightweightRequests != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
                Component lastFocusOwner = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
                Component currentFocusOwner = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
                for (Iterator iter = localLightweightRequests.iterator(); iter.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
                    currentFocusOwner = manager.getGlobalFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
                    LightweightFocusRequest lwFocusRequest =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
                        (LightweightFocusRequest)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
                     * WARNING: This is based on DKFM's logic solely!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
                     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
                     * We allow to trigger restoreFocus() in the dispatching process
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
                     * only if we have the last request to dispatch. If the last request
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
                     * fails, focus will be restored to either the component of the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
                     * previously succedded request, or to to the focus owner that was
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
                     * before this clearing proccess.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
                    if (!iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
                        disableRestoreFocus = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
                    FocusEvent currentFocusOwnerEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
                     * We're not dispatching FOCUS_LOST while the current focus owner is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
                     * But regardless of whether it's null or not, we're clearing ALL the local
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
                     * lw requests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
                    if (currentFocusOwner != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
                        currentFocusOwnerEvent = new CausedFocusEvent(currentFocusOwner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
                                       FocusEvent.FOCUS_LOST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
                                       lwFocusRequest.temporary,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
                                       lwFocusRequest.component, lwFocusRequest.cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
                    FocusEvent newFocusOwnerEvent =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
                        new CausedFocusEvent(lwFocusRequest.component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
                                       FocusEvent.FOCUS_GAINED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
                                       lwFocusRequest.temporary,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
                                       currentFocusOwner == null ? lastFocusOwner : currentFocusOwner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
                                       lwFocusRequest.cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
                    if (currentFocusOwner != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
                        ((AWTEvent) currentFocusOwnerEvent).isPosted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
                        caughtEx = dispatchAndCatchException(caughtEx, currentFocusOwner, currentFocusOwnerEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
                    ((AWTEvent) newFocusOwnerEvent).isPosted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
                    caughtEx = dispatchAndCatchException(caughtEx, lwFocusRequest.component, newFocusOwnerEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
                    if (manager.getGlobalFocusOwner() == lwFocusRequest.component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
                        lastFocusOwner = lwFocusRequest.component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
            clearingCurrentLightweightRequests = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
            disableRestoreFocus = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
            localLightweightRequests = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
            allowSyncFocusRequests = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
        if (caughtEx instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
            throw (RuntimeException)caughtEx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
        } else if (caughtEx instanceof Error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
            throw (Error)caughtEx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
    static FocusEvent retargetUnexpectedFocusEvent(FocusEvent fe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
        synchronized (heavyweightRequests) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
            // Any other case represents a failure condition which we did
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
            // not expect. We need to clearFocusRequestList() and patch up
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
            // the event as best as possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
            if (removeFirstRequest()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
                return (FocusEvent)retargetFocusEvent(fe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
            Component source = fe.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
            Component opposite = fe.getOppositeComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
            boolean temporary = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
            if (fe.getID() == FocusEvent.FOCUS_LOST &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
                (opposite == null || isTemporary(opposite, source)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
                temporary = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
            return new CausedFocusEvent(source, fe.getID(), temporary, opposite,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
                                        CausedFocusEvent.Cause.NATIVE_SYSTEM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
    static FocusEvent retargetFocusGained(FocusEvent fe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
        assert (fe.getID() == FocusEvent.FOCUS_GAINED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
        Component currentFocusOwner = getCurrentKeyboardFocusManager().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
            getGlobalFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
        Component source = fe.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
        Component opposite = fe.getOppositeComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
        Component nativeSource = getHeavyweight(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
        synchronized (heavyweightRequests) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
            HeavyweightFocusRequest hwFocusRequest = getFirstHWRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
            if (hwFocusRequest == HeavyweightFocusRequest.CLEAR_GLOBAL_FOCUS_OWNER)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
                return retargetUnexpectedFocusEvent(fe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
            if (source != null && nativeSource == null && hwFocusRequest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
                // if source w/o peer and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
                // if source is equal to first lightweight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
                // then we should correct source and nativeSource
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
                if (source == hwFocusRequest.getFirstLightweightRequest().component)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
                    source = hwFocusRequest.heavyweight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
                    nativeSource = source; // source is heavuweight itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
            if (hwFocusRequest != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
                nativeSource == hwFocusRequest.heavyweight)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
                // Focus change as a result of a known call to requestFocus(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
                // or known click on a peer focusable heavyweight Component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
                heavyweightRequests.removeFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
                LightweightFocusRequest lwFocusRequest =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
                    hwFocusRequest.lightweightRequests.removeFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
                Component newSource = lwFocusRequest.component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
                if (currentFocusOwner != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
                     * Since we receive FOCUS_GAINED when current focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
                     * owner is not null, correcponding FOCUS_LOST is supposed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
                     * to be lost.  And so,  we keep new focus owner
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
                     * to determine synthetic FOCUS_LOST event which will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
                     * generated by KeyboardFocusManager for this FOCUS_GAINED.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
                     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
                     * This code based on knowledge of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
                     * DefaultKeyboardFocusManager's implementation and might
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
                     * be not applicable for another KeyboardFocusManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
                    newFocusOwner = newSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
                boolean temporary = (opposite == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
                                     isTemporary(newSource, opposite))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
                        ? false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
                        : lwFocusRequest.temporary;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
                if (hwFocusRequest.lightweightRequests.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
                    currentLightweightRequests =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
                        hwFocusRequest.lightweightRequests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
                    EventQueue.invokeLater(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
                            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
                                processCurrentLightweightRequests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
                        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
                // 'opposite' will be fixed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
                // DefaultKeyboardFocusManager.realOppositeComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
                return new CausedFocusEvent(newSource,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
                                      FocusEvent.FOCUS_GAINED, temporary,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
                                      opposite, lwFocusRequest.cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
            if (currentFocusOwner != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
                && currentFocusOwner.getContainingWindow() == source
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
                && (hwFocusRequest == null || source != hwFocusRequest.heavyweight))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
                // Special case for FOCUS_GAINED in top-levels
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
                // If it arrives as the result of activation we should skip it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
                // This event will not have appropriate request record and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
                // on arrival there will be already some focus owner set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
                return new CausedFocusEvent(currentFocusOwner, FocusEvent.FOCUS_GAINED, false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
                                            null, CausedFocusEvent.Cause.ACTIVATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
            return retargetUnexpectedFocusEvent(fe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
        } // end synchronized(heavyweightRequests)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
    static FocusEvent retargetFocusLost(FocusEvent fe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
        assert (fe.getID() == FocusEvent.FOCUS_LOST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
        Component currentFocusOwner = getCurrentKeyboardFocusManager().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
            getGlobalFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
        Component opposite = fe.getOppositeComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
        Component nativeOpposite = getHeavyweight(opposite);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
        synchronized (heavyweightRequests) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
            HeavyweightFocusRequest hwFocusRequest = getFirstHWRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
            if (hwFocusRequest == HeavyweightFocusRequest.CLEAR_GLOBAL_FOCUS_OWNER)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
                if (currentFocusOwner != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
                    // Call to KeyboardFocusManager.clearGlobalFocusOwner()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
                    heavyweightRequests.removeFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
                    return new CausedFocusEvent(currentFocusOwner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
                                                FocusEvent.FOCUS_LOST, false, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
                                                CausedFocusEvent.Cause.CLEAR_GLOBAL_FOCUS_OWNER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
                // Otherwise, fall through to failure case below
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
            } else if (opposite == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
                // Focus leaving application
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
                if (currentFocusOwner != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
                    return new CausedFocusEvent(currentFocusOwner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
                                                FocusEvent.FOCUS_LOST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
                                                true, null, CausedFocusEvent.Cause.ACTIVATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
                    return fe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
            } else if (hwFocusRequest != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
                       (nativeOpposite == hwFocusRequest.heavyweight ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
                        nativeOpposite == null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
                        opposite == hwFocusRequest.getFirstLightweightRequest().component))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
                if (currentFocusOwner == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
                    return fe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
                // Focus change as a result of a known call to requestFocus(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
                // or click on a peer focusable heavyweight Component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
                // If a focus transfer is made across top-levels, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
                // FOCUS_LOST event is always temporary, and the FOCUS_GAINED
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
                // event is always permanent. Otherwise, the stored temporary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
                // value is honored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
                LightweightFocusRequest lwFocusRequest =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
                    hwFocusRequest.lightweightRequests.getFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
                boolean temporary = isTemporary(opposite, currentFocusOwner)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
                    ? true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
                    : lwFocusRequest.temporary;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
                return new CausedFocusEvent(currentFocusOwner, FocusEvent.FOCUS_LOST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
                                            temporary, lwFocusRequest.component, lwFocusRequest.cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
            } else if (focusedWindowChanged(opposite, currentFocusOwner)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
                // If top-level changed there might be no focus request in a list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
                // But we know the opposite, we now it is temporary - dispatch the event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
                if (!fe.isTemporary() && currentFocusOwner != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
                    // Create copy of the event with only difference in temporary parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
                    fe = new CausedFocusEvent(currentFocusOwner, FocusEvent.FOCUS_LOST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
                                              true, opposite, CausedFocusEvent.Cause.ACTIVATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
                return fe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
            return retargetUnexpectedFocusEvent(fe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
        }  // end synchronized(heavyweightRequests)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
    static AWTEvent retargetFocusEvent(AWTEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
        if (clearingCurrentLightweightRequests) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
            return event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
        KeyboardFocusManager manager = getCurrentKeyboardFocusManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
        if (focusLog.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
            if (event instanceof FocusEvent || event instanceof WindowEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
                focusLog.log(Level.FINER, ">>> {0}", new Object[] {event});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
            if (focusLog.isLoggable(Level.FINER) && event instanceof KeyEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
                focusLog.log(Level.FINER, "    focus owner is {0}", new Object[] {manager.getGlobalFocusOwner()});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
                focusLog.log(Level.FINER, ">>> {0}", new Object[] {event});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
        synchronized(heavyweightRequests) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
             * This code handles FOCUS_LOST event which is generated by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
             * DefaultKeyboardFocusManager for FOCUS_GAINED.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
             * This code based on knowledge of DefaultKeyboardFocusManager's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
             * implementation and might be not applicable for another
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
             * KeyboardFocusManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
             * Fix for 4472032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
            if (newFocusOwner != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
                event.getID() == FocusEvent.FOCUS_LOST)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
                FocusEvent fe = (FocusEvent)event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
                if (manager.getGlobalFocusOwner() == fe.getComponent() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
                    fe.getOppositeComponent() == newFocusOwner)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
                    newFocusOwner = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
                    return event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
        processCurrentLightweightRequests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
        switch (event.getID()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
            case FocusEvent.FOCUS_GAINED: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
                event = retargetFocusGained((FocusEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
            case FocusEvent.FOCUS_LOST: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
                event = retargetFocusLost((FocusEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
                /* do nothing */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
        return event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
     * Clears markers queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
     * This method is not intended to be overridden by KFM's.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
     * Only DefaultKeyboardFocusManager can implement it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
    void clearMarkers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
    static boolean removeFirstRequest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
        KeyboardFocusManager manager =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
            KeyboardFocusManager.getCurrentKeyboardFocusManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
        synchronized(heavyweightRequests) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
            HeavyweightFocusRequest hwFocusRequest = getFirstHWRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
            if (hwFocusRequest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
                heavyweightRequests.removeFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
                if (hwFocusRequest.lightweightRequests != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
                    for (Iterator lwIter = hwFocusRequest.lightweightRequests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
                             iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
                         lwIter.hasNext(); )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
                        manager.dequeueKeyEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
                            (-1, ((LightweightFocusRequest)lwIter.next()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
                             component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
            // Fix for 4799136 - clear type-ahead markers if requests queue is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
            // We do it here because this method is called only when problems happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
            if (heavyweightRequests.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
                manager.clearMarkers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
            return (heavyweightRequests.size() > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
    static void removeLastFocusRequest(Component heavyweight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
        if (log.isLoggable(Level.FINE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
            if (heavyweight == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
                log.log(Level.FINE, "Assertion (heavyweight != null) failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
        KeyboardFocusManager manager =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
            KeyboardFocusManager.getCurrentKeyboardFocusManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
        synchronized(heavyweightRequests) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
            HeavyweightFocusRequest hwFocusRequest = getLastHWRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
            if (hwFocusRequest != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
                hwFocusRequest.heavyweight == heavyweight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
                heavyweightRequests.removeLast();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
            // Fix for 4799136 - clear type-ahead markers if requests queue is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
            // We do it here because this method is called only when problems happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
            if (heavyweightRequests.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
                manager.clearMarkers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
    private static boolean focusedWindowChanged(Component to, Component from) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
        Window wto = Component.getContainingWindow(to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
        Window wfrom = Component.getContainingWindow(from);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
        if (wto == null && wfrom == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
        if (wto == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
        if (wfrom == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
        return (wto != wfrom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
    private static boolean isTemporary(Component to, Component from) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
        Window wto = Component.getContainingWindow(to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
        Window wfrom = Component.getContainingWindow(from);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
        if (wto == null && wfrom == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
        if (wto == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
        if (wfrom == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
        return (wto != wfrom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
    static Component getHeavyweight(Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3042
        if (comp == null || comp.getPeer() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
        } else if (comp.getPeer() instanceof LightweightPeer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
            return comp.getNativeContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
            return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3049
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
    static Field proxyActive;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
    // Accessor to private field isProxyActive of KeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
    private static boolean isProxyActiveImpl(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
        if (proxyActive == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
            proxyActive = (Field) AccessController.doPrivileged(new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3056
                    public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
                        Field field = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
                            field = KeyEvent.class.getDeclaredField("isProxyActive");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
                            if (field != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
                                field.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
                        } catch (NoSuchFieldException nsf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
                            assert(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
                        return field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3067
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3072
            return proxyActive.getBoolean(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
        } catch (IllegalAccessException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3074
            assert(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
    // Returns the value of this KeyEvent's field isProxyActive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
    static boolean isProxyActive(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
        if (!GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
            return isProxyActiveImpl(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3084
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3086
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3088
    private static HeavyweightFocusRequest getLastHWRequest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
        synchronized(heavyweightRequests) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
            return (heavyweightRequests.size() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
                ? heavyweightRequests.getLast()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
                : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
    private static HeavyweightFocusRequest getFirstHWRequest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
        synchronized(heavyweightRequests) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
            return (heavyweightRequests.size() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
                ? heavyweightRequests.getFirst()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
                : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
}