jdk/src/share/classes/java/awt/ContainerOrderFocusTraversalPolicy.java
author ant
Wed, 05 Jun 2013 17:44:50 +0400
changeset 17905 650343913d86
parent 16839 d0f2e97b7359
child 18178 ee71c923891d
permissions -rw-r--r--
8015339: Correct a wording in javadoc of java.awt.ContainerOrderFocusTraversalPolicy Reviewed-by: art, anthony
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
diff changeset
     2
 * Copyright (c) 2000, 2003, 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: 3938
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: 3938
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: 3938
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.ArrayList;
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3082
diff changeset
    29
import sun.util.logging.PlatformLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * A FocusTraversalPolicy that determines traversal order based on the order
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * of child Components in a Container. From a particular focus cycle root, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * policy makes a pre-order traversal of the Component hierarchy, and traverses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * a Container's children according to the ordering of the array returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <code>Container.getComponents()</code>. Portions of the hierarchy that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * not visible and displayable will not be searched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * By default, ContainerOrderFocusTraversalPolicy implicitly transfers focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * down-cycle. That is, during normal forward focus traversal, the Component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * traversed after a focus cycle root will be the focus-cycle-root's default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Component to focus. This behavior can be disabled using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <code>setImplicitDownCycleTraversal</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p>
17905
650343913d86 8015339: Correct a wording in javadoc of java.awt.ContainerOrderFocusTraversalPolicy
ant
parents: 16839
diff changeset
    45
 * By default, methods of this class will return a Component only if it is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * visible, displayable, enabled, and focusable. Subclasses can modify this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * behavior by overriding the <code>accept</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * This policy takes into account <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * href="doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus traversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * policy providers</a>.  When searching for first/last/next/previous Component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * if a focus traversal policy provider is encountered, its focus traversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * policy is used to perform the search operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @author David Mendenhall
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @see Container#getComponents
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
public class ContainerOrderFocusTraversalPolicy extends FocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    implements java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
{
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3082
diff changeset
    63
    private static final PlatformLogger log = PlatformLogger.getLogger("java.awt.ContainerOrderFocusTraversalPolicy");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    final private int FORWARD_TRAVERSAL = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    final private int BACKWARD_TRAVERSAL = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * JDK 1.4 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static final long serialVersionUID = 486933713763926351L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private boolean implicitDownCycleTraversal = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Used by getComponentAfter and getComponentBefore for efficiency. In
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * order to maintain compliance with the specification of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * FocusTraversalPolicy, if traversal wraps, we should invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * getFirstComponent or getLastComponent. These methods may be overriden in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * subclasses to behave in a non-generic way. However, in the generic case,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * these methods will simply return the first or last Components of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * sorted list, respectively. Since getComponentAfter and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * getComponentBefore have already built the list before determining
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * that they need to invoke getFirstComponent or getLastComponent, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * list should be reused if possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    transient private Container cachedRoot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    transient private List cachedCycle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * We suppose to use getFocusTraversalCycle & getComponentIndex methods in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * to divide the policy into two parts:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * 1) Making the focus traversal cycle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * 2) Traversing the cycle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * The 1st point assumes producing a list of components representing the focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * traversal cycle. The two methods mentioned above should implement this logic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * The 2nd point assumes implementing the common concepts of operating on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * cycle: traversing back and forth, retrieving the initial/default/first/last
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * component. These concepts are described in the AWT Focus Spec and they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * applied to the FocusTraversalPolicy in general.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Thus, a descendant of this policy may wish to not reimplement the logic of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * the 2nd point but just override the implementation of the 1st one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * A striking example of such a descendant is the javax.swing.SortingFocusTraversalPolicy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /*protected*/ private List<Component> getFocusTraversalCycle(Container aContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        List<Component> cycle = new ArrayList<Component>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        enumerateCycle(aContainer, cycle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return cycle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /*protected*/ private int getComponentIndex(List<Component> cycle, Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return cycle.indexOf(aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private void enumerateCycle(Container container, List cycle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (!(container.isVisible() && container.isDisplayable())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        cycle.add(container);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        Component[] components = container.getComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        for (int i = 0; i < components.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            Component comp = components[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (comp instanceof Container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                Container cont = (Container)comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                if (!cont.isFocusCycleRoot() && !cont.isFocusTraversalPolicyProvider()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    enumerateCycle(cont, cycle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            cycle.add(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private Container getTopmostProvider(Container focusCycleRoot, Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        Container aCont = aComponent.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        Container ftp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        while (aCont  != focusCycleRoot && aCont != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            if (aCont.isFocusTraversalPolicyProvider()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                ftp = aCont;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            aCont = aCont.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (aCont == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return ftp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Checks if a new focus cycle takes place and returns a Component to traverse focus to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param comp a possible focus cycle root or policy provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param traversalDirection the direction of the traversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @return a Component to traverse focus to if {@code comp} is a root or provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *         and implicit down-cycle is set, otherwise {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    private Component getComponentDownCycle(Component comp, int traversalDirection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        Component retComp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (comp instanceof Container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            Container cont = (Container)comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            if (cont.isFocusCycleRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                if (getImplicitDownCycleTraversal()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    retComp = cont.getFocusTraversalPolicy().getDefaultComponent(cont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3082
diff changeset
   168
                    if (retComp != null && log.isLoggable(PlatformLogger.FINE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                        log.fine("### Transfered focus down-cycle to " + retComp +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                                 " in the focus cycle root " + cont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            } else if (cont.isFocusTraversalPolicyProvider()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                retComp = (traversalDirection == FORWARD_TRAVERSAL ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                           cont.getFocusTraversalPolicy().getDefaultComponent(cont) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                           cont.getFocusTraversalPolicy().getLastComponent(cont));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3082
diff changeset
   180
                if (retComp != null && log.isLoggable(PlatformLogger.FINE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    log.fine("### Transfered focus to " + retComp + " in the FTP provider " + cont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        return retComp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Returns the Component that should receive the focus after aComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * aContainer must be a focus cycle root of aComponent or a focus traversal policy provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * By default, ContainerOrderFocusTraversalPolicy implicitly transfers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * focus down-cycle. That is, during normal forward focus traversal, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Component traversed after a focus cycle root will be the focus-cycle-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * root's default Component to focus. This behavior can be disabled using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * the <code>setImplicitDownCycleTraversal</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * If aContainer is <a href="doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * traversal policy provider</a>, the focus is always transferred down-cycle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @param aComponent a (possibly indirect) child of aContainer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *        aContainer itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @return the Component that should receive the focus after aComponent, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *         null if no suitable Component can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @throws IllegalArgumentException if aContainer is not a focus cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *         root of aComponent or focus traversal policy provider, or if either aContainer or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *         aComponent is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public Component getComponentAfter(Container aContainer, Component aComponent) {
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   211
        if (log.isLoggable(PlatformLogger.FINE)) {
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   212
            log.fine("### Searching in " + aContainer + " for component after " + aComponent);
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   213
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (aContainer == null || aComponent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            throw new IllegalArgumentException("aContainer and aComponent cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (!aContainer.isFocusTraversalPolicyProvider() && !aContainer.isFocusCycleRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            throw new IllegalArgumentException("aContainer should be focus cycle root or focus traversal policy provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        } else if (aContainer.isFocusCycleRoot() && !aComponent.isFocusCycleRoot(aContainer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            throw new IllegalArgumentException("aContainer is not a focus cycle root of aComponent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        synchronized(aContainer.getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            if (!(aContainer.isVisible() && aContainer.isDisplayable())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            // Before all the ckecks below we first see if it's an FTP provider or a focus cycle root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            // If it's the case just go down cycle (if it's set to "implicit").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            Component comp = getComponentDownCycle(aComponent, FORWARD_TRAVERSAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            if (comp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            // See if the component is inside of policy provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            Container provider = getTopmostProvider(aContainer, aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            if (provider != null) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3082
diff changeset
   241
                if (log.isLoggable(PlatformLogger.FINE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    log.fine("### Asking FTP " + provider + " for component after " + aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                // FTP knows how to find component after the given. We don't.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                FocusTraversalPolicy policy = provider.getFocusTraversalPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                Component afterComp = policy.getComponentAfter(provider, aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                // Null result means that we overstepped the limit of the FTP's cycle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                // In that case we must quit the cycle, otherwise return the component found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                if (afterComp != null) {
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   252
                    if (log.isLoggable(PlatformLogger.FINE)) {
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   253
                        log.fine("### FTP returned " + afterComp);
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   254
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    return afterComp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                aComponent = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            List<Component> cycle = getFocusTraversalCycle(aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   262
            if (log.isLoggable(PlatformLogger.FINE)) {
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   263
                log.fine("### Cycle is " + cycle + ", component is " + aComponent);
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   264
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            int index = getComponentIndex(cycle, aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            if (index < 0) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3082
diff changeset
   269
                if (log.isLoggable(PlatformLogger.FINE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    log.fine("### Didn't find component " + aComponent + " in a cycle " + aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                return getFirstComponent(aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            for (index++; index < cycle.size(); index++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                comp = cycle.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                if (accept(comp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                } else if ((comp = getComponentDownCycle(comp, FORWARD_TRAVERSAL)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            if (aContainer.isFocusCycleRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                this.cachedRoot = aContainer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                this.cachedCycle = cycle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                comp = getFirstComponent(aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                this.cachedRoot = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                this.cachedCycle = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * Returns the Component that should receive the focus before aComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * aContainer must be a focus cycle root of aComponent or a <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * href="doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus traversal policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * provider</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @param aContainer a focus cycle root of aComponent or focus traversal policy provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @param aComponent a (possibly indirect) child of aContainer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *        aContainer itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @return the Component that should receive the focus before aComponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *         or null if no suitable Component can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @throws IllegalArgumentException if aContainer is not a focus cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *         root of aComponent or focus traversal policy provider, or if either aContainer or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *         aComponent is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    public Component getComponentBefore(Container aContainer, Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        if (aContainer == null || aComponent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            throw new IllegalArgumentException("aContainer and aComponent cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (!aContainer.isFocusTraversalPolicyProvider() && !aContainer.isFocusCycleRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            throw new IllegalArgumentException("aContainer should be focus cycle root or focus traversal policy provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        } else if (aContainer.isFocusCycleRoot() && !aComponent.isFocusCycleRoot(aContainer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            throw new IllegalArgumentException("aContainer is not a focus cycle root of aComponent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        synchronized(aContainer.getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            if (!(aContainer.isVisible() && aContainer.isDisplayable())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            // See if the component is inside of policy provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            Container provider = getTopmostProvider(aContainer, aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            if (provider != null) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3082
diff changeset
   334
                if (log.isLoggable(PlatformLogger.FINE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    log.fine("### Asking FTP " + provider + " for component after " + aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                // FTP knows how to find component after the given. We don't.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                FocusTraversalPolicy policy = provider.getFocusTraversalPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                Component beforeComp = policy.getComponentBefore(provider, aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                // Null result means that we overstepped the limit of the FTP's cycle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                // In that case we must quit the cycle, otherwise return the component found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                if (beforeComp != null) {
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   345
                    if (log.isLoggable(PlatformLogger.FINE)) {
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   346
                        log.fine("### FTP returned " + beforeComp);
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   347
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    return beforeComp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                aComponent = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                // If the provider is traversable it's returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                if (accept(aComponent)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    return aComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            List<Component> cycle = getFocusTraversalCycle(aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   360
            if (log.isLoggable(PlatformLogger.FINE)) {
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   361
                log.fine("### Cycle is " + cycle + ", component is " + aComponent);
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   362
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            int index = getComponentIndex(cycle, aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            if (index < 0) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3082
diff changeset
   367
                if (log.isLoggable(PlatformLogger.FINE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    log.fine("### Didn't find component " + aComponent + " in a cycle " + aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                return getLastComponent(aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            Component comp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            Component tryComp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            for (index--; index>=0; index--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                comp = cycle.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                if (comp != aContainer && (tryComp = getComponentDownCycle(comp, BACKWARD_TRAVERSAL)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    return tryComp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                } else if (accept(comp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            if (aContainer.isFocusCycleRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                this.cachedRoot = aContainer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                this.cachedCycle = cycle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                comp = getLastComponent(aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                this.cachedRoot = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                this.cachedCycle = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * Returns the first Component in the traversal cycle. This method is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * to determine the next Component to focus when traversal wraps in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * forward direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @param aContainer the focus cycle root or focus traversal policy provider whose first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *        Component is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @return the first Component in the traversal cycle of aContainer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *         or null if no suitable Component can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @throws IllegalArgumentException if aContainer is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    public Component getFirstComponent(Container aContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        List<Component> cycle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   414
        if (log.isLoggable(PlatformLogger.FINE)) {
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   415
            log.fine("### Getting first component in " + aContainer);
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   416
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        if (aContainer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            throw new IllegalArgumentException("aContainer cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        synchronized(aContainer.getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            if (!(aContainer.isVisible() && aContainer.isDisplayable())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            if (this.cachedRoot == aContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                cycle = this.cachedCycle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                cycle = getFocusTraversalCycle(aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            if (cycle.size() == 0) {
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   435
                if (log.isLoggable(PlatformLogger.FINE)) {
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   436
                    log.fine("### Cycle is empty");
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   437
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            }
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   440
            if (log.isLoggable(PlatformLogger.FINE)) {
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   441
                log.fine("### Cycle is " + cycle);
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   442
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
3082
24c8d93ac1e1 4788402: SortingFocusTraversalPolicy: prob with non-focusable focus Cycle Root as first
ant
parents: 1171
diff changeset
   444
            for (Component comp : cycle) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                if (accept(comp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    return comp;
3082
24c8d93ac1e1 4788402: SortingFocusTraversalPolicy: prob with non-focusable focus Cycle Root as first
ant
parents: 1171
diff changeset
   447
                } else if (comp != aContainer &&
24c8d93ac1e1 4788402: SortingFocusTraversalPolicy: prob with non-focusable focus Cycle Root as first
ant
parents: 1171
diff changeset
   448
                           (comp = getComponentDownCycle(comp, FORWARD_TRAVERSAL)) != null)
24c8d93ac1e1 4788402: SortingFocusTraversalPolicy: prob with non-focusable focus Cycle Root as first
ant
parents: 1171
diff changeset
   449
                {
24c8d93ac1e1 4788402: SortingFocusTraversalPolicy: prob with non-focusable focus Cycle Root as first
ant
parents: 1171
diff changeset
   450
                    return comp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * Returns the last Component in the traversal cycle. This method is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * to determine the next Component to focus when traversal wraps in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * reverse direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * @param aContainer the focus cycle root or focus traversal policy provider whose last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *        Component is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * @return the last Component in the traversal cycle of aContainer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *         or null if no suitable Component can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @throws IllegalArgumentException if aContainer is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    public Component getLastComponent(Container aContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        List<Component> cycle;
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   470
        if (log.isLoggable(PlatformLogger.FINE)) {
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   471
            log.fine("### Getting last component in " + aContainer);
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   472
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        if (aContainer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            throw new IllegalArgumentException("aContainer cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        synchronized(aContainer.getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            if (!(aContainer.isVisible() && aContainer.isDisplayable())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            if (this.cachedRoot == aContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                cycle = this.cachedCycle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                cycle = getFocusTraversalCycle(aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            if (cycle.size() == 0) {
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   491
                if (log.isLoggable(PlatformLogger.FINE)) {
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   492
                    log.fine("### Cycle is empty");
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   493
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            }
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   496
            if (log.isLoggable(PlatformLogger.FINE)) {
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   497
                log.fine("### Cycle is " + cycle);
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   498
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            for (int i= cycle.size() - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                Component comp = cycle.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                if (accept(comp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                } else if (comp instanceof Container && comp != aContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                    Container cont = (Container)comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                    if (cont.isFocusTraversalPolicyProvider()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                        return cont.getFocusTraversalPolicy().getLastComponent(cont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * Returns the default Component to focus. This Component will be the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * to receive focus when traversing down into a new focus traversal cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * rooted at aContainer. The default implementation of this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * returns the same Component as <code>getFirstComponent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * @param aContainer the focus cycle root or focus traversal policy provider whose default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     *        Component is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * @return the default Component in the traversal cycle of aContainer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *         or null if no suitable Component can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * @see #getFirstComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @throws IllegalArgumentException if aContainer is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    public Component getDefaultComponent(Container aContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        return getFirstComponent(aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * Sets whether this ContainerOrderFocusTraversalPolicy transfers focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * down-cycle implicitly. If <code>true</code>, during normal forward focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * traversal, the Component traversed after a focus cycle root will be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * focus-cycle-root's default Component to focus. If <code>false</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * the next Component in the focus traversal cycle rooted at the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * focus cycle root will be traversed instead. The default value for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * property is <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * @param implicitDownCycleTraversal whether this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     *        ContainerOrderFocusTraversalPolicy transfers focus down-cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     *        implicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * @see #getImplicitDownCycleTraversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * @see #getFirstComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    public void setImplicitDownCycleTraversal(boolean implicitDownCycleTraversal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        this.implicitDownCycleTraversal = implicitDownCycleTraversal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * Returns whether this ContainerOrderFocusTraversalPolicy transfers focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * down-cycle implicitly. If <code>true</code>, during normal forward focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * traversal, the Component traversed after a focus cycle root will be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * focus-cycle-root's default Component to focus. If <code>false</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * the next Component in the focus traversal cycle rooted at the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * focus cycle root will be traversed instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * @return whether this ContainerOrderFocusTraversalPolicy transfers focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     *         down-cycle implicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * @see #setImplicitDownCycleTraversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * @see #getFirstComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    public boolean getImplicitDownCycleTraversal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        return implicitDownCycleTraversal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * Determines whether a Component is an acceptable choice as the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * focus owner. By default, this method will accept a Component if and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * only if it is visible, displayable, enabled, and focusable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * @param aComponent the Component whose fitness as a focus owner is to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     *        be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * @return <code>true</code> if aComponent is visible, displayable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     *         enabled, and focusable; <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    protected boolean accept(Component aComponent) {
1171
a2782dd9f312 4685768: A11y issue - Focus set to disabled component, can't Tab/Shift-Tab
ant
parents: 2
diff changeset
   579
        if (!aComponent.canBeFocusOwner()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        // Verify that the Component is recursively enabled. Disabling a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        // heavyweight Container disables its children, whereas disabling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        // a lightweight Container does not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        if (!(aComponent instanceof Window)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            for (Container enableTest = aComponent.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                 enableTest != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                 enableTest = enableTest.getParent())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                if (!(enableTest.isEnabled() || enableTest.isLightweight())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                if (enableTest instanceof Window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
}