jdk/src/java.desktop/share/classes/java/awt/FocusTraversalPolicy.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 23010 jdk/src/share/classes/java/awt/FocusTraversalPolicy.java@6dadb192ad81
child 35667 ed476aba94de
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 20455
diff changeset
     2
 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * A FocusTraversalPolicy defines the order in which Components with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * particular focus cycle root are traversed. Instances can apply the policy to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * arbitrary focus cycle roots, allowing themselves to be shared across
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Containers. They do not need to be reinitialized when the focus cycle roots
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * of a Component hierarchy change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * The core responsibility of a FocusTraversalPolicy is to provide algorithms
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * determining the next and previous Components to focus when traversing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * forward or backward in a UI. Each FocusTraversalPolicy must also provide
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * algorithms for determining the first, last, and default Components in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * traversal cycle. First and last Components are used when normal forward and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * backward traversal, respectively, wraps. The default Component is the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * to receive focus when traversing down into a new focus traversal cycle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * A FocusTraversalPolicy can optionally provide an algorithm for determining
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * a Window's initial Component. The initial Component is the first to receive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * focus when a Window is first made visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * FocusTraversalPolicy takes into account <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * href="doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus traversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * policy providers</a>.  When searching for first/last/next/previous Component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * if a focus traversal policy provider is encountered, its focus traversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * policy is used to perform the search operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Please see
20455
f6f9a0c2796b 8020688: Broken links in documentation at http://docs.oracle.com/javase/6/docs/api/index.
mcherkas
parents: 5506
diff changeset
    52
 * <a href="http://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * How to Use the Focus Subsystem</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * a section in <em>The Java Tutorial</em>, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <a href="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @author David Mendenhall
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @see Container#setFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @see Container#getFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @see Container#setFocusCycleRoot
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @see Container#isFocusCycleRoot
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @see Container#setFocusTraversalPolicyProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * @see Container#isFocusTraversalPolicyProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * @see KeyboardFocusManager#setDefaultFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * @see KeyboardFocusManager#getDefaultFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
public abstract class FocusTraversalPolicy {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Returns the Component that should receive the focus after aComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * aContainer must be a focus cycle root of aComponent or a focus traversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * policy provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param aContainer a focus cycle root of aComponent or focus traversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *        policy provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param aComponent a (possibly indirect) child of aContainer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *        aContainer itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @return the Component that should receive the focus after aComponent, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *         null if no suitable Component can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @throws IllegalArgumentException if aContainer is not a focus cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *         root of aComponent or a focus traversal policy provider, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *         either aContainer or aComponent is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public abstract Component getComponentAfter(Container aContainer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                                                Component aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Returns the Component that should receive the focus before aComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * aContainer must be a focus cycle root of aComponent or a focus traversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * policy provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param aContainer a focus cycle root of aComponent or focus traversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *        policy provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @param aComponent a (possibly indirect) child of aContainer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *        aContainer itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @return the Component that should receive the focus before aComponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *         or null if no suitable Component can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @throws IllegalArgumentException if aContainer is not a focus cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *         root of aComponent or a focus traversal policy provider, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *         either aContainer or aComponent is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public abstract Component getComponentBefore(Container aContainer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                                                 Component aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Returns the first Component in the traversal cycle. This method is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * to determine the next Component to focus when traversal wraps in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * forward direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param aContainer the focus cycle root or focus traversal policy provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *        whose first Component is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @return the first Component in the traversal cycle of aContainer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *         or null if no suitable Component can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @throws IllegalArgumentException if aContainer is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public abstract Component getFirstComponent(Container aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Returns the last Component in the traversal cycle. This method is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * to determine the next Component to focus when traversal wraps in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * reverse direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @param aContainer the focus cycle root or focus traversal policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *        provider whose last Component is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @return the last Component in the traversal cycle of aContainer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *         or null if no suitable Component can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @throws IllegalArgumentException if aContainer is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public abstract Component getLastComponent(Container aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Returns the default Component to focus. This Component will be the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * to receive focus when traversing down into a new focus traversal cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * rooted at aContainer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param aContainer the focus cycle root or focus traversal policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *        provider whose default Component is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @return the default Component in the traversal cycle of aContainer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *         or null if no suitable Component can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @throws IllegalArgumentException if aContainer is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public abstract Component getDefaultComponent(Container aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Returns the Component that should receive the focus when a Window is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * made visible for the first time. Once the Window has been made visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * by a call to <code>show()</code> or <code>setVisible(true)</code>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * initial Component will not be used again. Instead, if the Window loses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * and subsequently regains focus, or is made invisible or undisplayable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * and subsequently made visible and displayable, the Window's most
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * recently focused Component will become the focus owner. The default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * implementation of this method returns the default Component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param window the Window whose initial Component is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @return the Component that should receive the focus when window is made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *         visible for the first time, or null if no suitable Component can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *         be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @see #getDefaultComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @see Window#getMostRecentFocusOwner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @throws IllegalArgumentException if window is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public Component getInitialComponent(Window window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if ( window == null ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            throw new IllegalArgumentException("window cannot be equal to null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        Component def = getDefaultComponent(window);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (def == null && window.isFocusableWindow()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            def = window;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        return def;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
}