jdk/src/share/classes/javax/swing/SortingFocusTraversalPolicy.java
author mchung
Tue, 29 Sep 2009 16:03:03 -0700
changeset 3938 ef327bd847c0
parent 3082 24c8d93ac1e1
child 5506 202f599c92aa
permissions -rw-r--r--
6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes Summary: Replace calls to Logger with sun.util.logging.PlatformLogger Reviewed-by: prr, art, alexp, dcherepanov, igor, dav, anthony
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
1639
a97859015238 6785258: Update copyright year
xdono
parents: 1301
diff changeset
     2
 * Copyright 2000-2008 Sun Microsystems, Inc.  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
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 javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Container;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Window;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.FocusTraversalPolicy;
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3082
diff changeset
    32
import sun.util.logging.PlatformLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * A FocusTraversalPolicy that determines traversal order by sorting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Components of a focus traversal cycle based on a given Comparator. Portions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * of the Component hierarchy that are not visible and displayable will not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * included.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * By default, SortingFocusTraversalPolicy implicitly transfers focus down-
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * cycle. That is, during normal focus traversal, the Component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * traversed after a focus cycle root will be the focus-cycle-root's default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Component to focus. This behavior can be disabled using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <code>setImplicitDownCycleTraversal</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * By default, methods of this class with return a Component only if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * visible, displayable, enabled, and focusable. Subclasses can modify this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * behavior by overriding the <code>accept</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * This policy takes into account <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * href="../../java/awt/doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus traversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * policy providers</a>.  When searching for first/last/next/previous Component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * if a focus traversal policy provider is encountered, its focus traversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * policy is used to perform the search operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @author David Mendenhall
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @see java.util.Comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
public class SortingFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    extends InternalFrameFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private Comparator<? super Component> comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private boolean implicitDownCycleTraversal = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3082
diff changeset
    67
    private PlatformLogger log = PlatformLogger.getLogger("javax.swing.SortingFocusTraversalPolicy");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Used by getComponentAfter and getComponentBefore for efficiency. In
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * order to maintain compliance with the specification of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * FocusTraversalPolicy, if traversal wraps, we should invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * getFirstComponent or getLastComponent. These methods may be overriden in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * subclasses to behave in a non-generic way. However, in the generic case,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * these methods will simply return the first or last Components of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * sorted list, respectively. Since getComponentAfter and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * getComponentBefore have already built the sorted list before determining
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * that they need to invoke getFirstComponent or getLastComponent, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * sorted list should be reused if possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    transient private Container cachedRoot;
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    82
    transient private List<Component> cachedCycle;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // Delegate our fitness test to ContainerOrder so that we only have to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // code the algorithm once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private static final SwingContainerOrderFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        fitnessTestPolicy = new SwingContainerOrderFocusTraversalPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    final private int FORWARD_TRAVERSAL = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    final private int BACKWARD_TRAVERSAL = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Constructs a SortingFocusTraversalPolicy without a Comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Subclasses must set the Comparator using <code>setComparator</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * before installing this FocusTraversalPolicy on a focus cycle root or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * KeyboardFocusManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    protected SortingFocusTraversalPolicy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Constructs a SortingFocusTraversalPolicy with the specified Comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public SortingFocusTraversalPolicy(Comparator<? super Component> comparator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        this.comparator = comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private List<Component> getFocusTraversalCycle(Container aContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        List<Component> cycle = new ArrayList<Component>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        enumerateAndSortCycle(aContainer, cycle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return cycle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private int getComponentIndex(List<Component> cycle, Component aComponent) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   114
        int index;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            index = Collections.binarySearch(cycle, aComponent, comparator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        } catch (ClassCastException e) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3082
diff changeset
   118
            if (log.isLoggable(PlatformLogger.FINE)) {
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3082
diff changeset
   119
                log.fine("### During the binary search for " + aComponent + " the exception occured: ", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (index < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            // Fix for 5070991.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            // A workaround for a transitivity problem caused by ROW_TOLERANCE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            // because of that the component may be missed in the binary search.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            // Try to search it again directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            index = cycle.indexOf(aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   133
    private void enumerateAndSortCycle(Container focusCycleRoot, List<Component> cycle) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (focusCycleRoot.isShowing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            enumerateCycle(focusCycleRoot, cycle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            Collections.sort(cycle, comparator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   140
    private void enumerateCycle(Container container, List<Component> cycle) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (!(container.isVisible() && container.isDisplayable())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        cycle.add(container);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        Component[] components = container.getComponents();
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   148
        for (Component comp : components) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (comp instanceof Container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                Container cont = (Container)comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                if (!cont.isFocusCycleRoot() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    !cont.isFocusTraversalPolicyProvider() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    !((cont instanceof JComponent) && ((JComponent)cont).isManagingFocus()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    enumerateCycle(cont, cycle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            cycle.add(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    Container getTopmostProvider(Container focusCycleRoot, Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        Container aCont = aComponent.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        Container ftp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        while (aCont  != focusCycleRoot && aCont != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            if (aCont.isFocusTraversalPolicyProvider()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                ftp = aCont;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            aCont = aCont.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (aCont == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return ftp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Checks if a new focus cycle takes place and returns a Component to traverse focus to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @param comp a possible focus cycle root or policy provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @param traversalDirection the direction of the traversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @return a Component to traverse focus to if {@code comp} is a root or provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *         and implicit down-cycle is set, otherwise {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    private Component getComponentDownCycle(Component comp, int traversalDirection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        Component retComp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (comp instanceof Container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            Container cont = (Container)comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            if (cont.isFocusCycleRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                if (getImplicitDownCycleTraversal()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    retComp = cont.getFocusTraversalPolicy().getDefaultComponent(cont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3082
diff changeset
   196
                    if (retComp != null && log.isLoggable(PlatformLogger.FINE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                        log.fine("### Transfered focus down-cycle to " + retComp +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                                 " in the focus cycle root " + cont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            } else if (cont.isFocusTraversalPolicyProvider()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                retComp = (traversalDirection == FORWARD_TRAVERSAL ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                           cont.getFocusTraversalPolicy().getDefaultComponent(cont) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                           cont.getFocusTraversalPolicy().getLastComponent(cont));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3082
diff changeset
   208
                if (retComp != null && log.isLoggable(PlatformLogger.FINE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    log.fine("### Transfered focus to " + retComp + " in the FTP provider " + cont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        return retComp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Returns the Component that should receive the focus after aComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * aContainer must be a focus cycle root of aComponent or a focus traversal policy provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * By default, SortingFocusTraversalPolicy implicitly transfers focus down-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * cycle. That is, during normal focus traversal, the Component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * traversed after a focus cycle root will be the focus-cycle-root's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * default Component to focus. This behavior can be disabled using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * <code>setImplicitDownCycleTraversal</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * If aContainer is <a href="../../java/awt/doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * traversal policy provider</a>, the focus is always transferred down-cycle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @param aComponent a (possibly indirect) child of aContainer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *        aContainer itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @return the Component that should receive the focus after aComponent, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *         null if no suitable Component can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @throws IllegalArgumentException if aContainer is not a focus cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *         root of aComponent or a focus traversal policy provider, or if either aContainer or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *         aComponent is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public Component getComponentAfter(Container aContainer, Component aComponent) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3082
diff changeset
   239
        if (log.isLoggable(PlatformLogger.FINE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            log.fine("### Searching in " + aContainer + " for component after " + aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (aContainer == null || aComponent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            throw new IllegalArgumentException("aContainer and aComponent cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (!aContainer.isFocusTraversalPolicyProvider() && !aContainer.isFocusCycleRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            throw new IllegalArgumentException("aContainer should be focus cycle root or focus traversal policy provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        } else if (aContainer.isFocusCycleRoot() && !aComponent.isFocusCycleRoot(aContainer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            throw new IllegalArgumentException("aContainer is not a focus cycle root of aComponent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        // 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
   254
        // If it's the case just go down cycle (if it's set to "implicit").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        Component comp = getComponentDownCycle(aComponent, FORWARD_TRAVERSAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (comp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        // See if the component is inside of policy provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        Container provider = getTopmostProvider(aContainer, aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if (provider != null) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3082
diff changeset
   263
            if (log.isLoggable(PlatformLogger.FINE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                log.fine("### Asking FTP " + provider + " for component after " + aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            // FTP knows how to find component after the given. We don't.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            FocusTraversalPolicy policy = provider.getFocusTraversalPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            Component afterComp = policy.getComponentAfter(provider, aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            // Null result means that we overstepped the limit of the FTP's cycle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            // In that case we must quit the cycle, otherwise return the component found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            if (afterComp != null) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3082
diff changeset
   274
                if (log.isLoggable(PlatformLogger.FINE)) log.fine("### FTP returned " + afterComp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                return afterComp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            aComponent = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        List<Component> cycle = getFocusTraversalCycle(aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3082
diff changeset
   282
        if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Cycle is " + cycle + ", component is " + aComponent);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        int index = getComponentIndex(cycle, aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        if (index < 0) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3082
diff changeset
   287
            if (log.isLoggable(PlatformLogger.FINE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                log.fine("### Didn't find component " + aComponent + " in a cycle " + aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            return getFirstComponent(aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        for (index++; index < cycle.size(); index++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            comp = cycle.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            if (accept(comp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            } else if ((comp = getComponentDownCycle(comp, FORWARD_TRAVERSAL)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                return comp;
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
        if (aContainer.isFocusCycleRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            this.cachedRoot = aContainer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            this.cachedCycle = cycle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            comp = getFirstComponent(aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            this.cachedRoot = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            this.cachedCycle = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        return null;
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
     * Returns the Component that should receive the focus before aComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * aContainer must be a focus cycle root of aComponent or a focus traversal policy provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * By default, SortingFocusTraversalPolicy implicitly transfers focus down-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * cycle. That is, during normal focus traversal, the Component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * traversed after a focus cycle root will be the focus-cycle-root's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * default Component to focus. This behavior can be disabled using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * <code>setImplicitDownCycleTraversal</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * If aContainer is <a href="../../java/awt/doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * traversal policy provider</a>, the focus is always transferred down-cycle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @param aComponent a (possibly indirect) child of aContainer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *        aContainer itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @return the Component that should receive the focus before aComponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *         or null if no suitable Component can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @throws IllegalArgumentException if aContainer is not a focus cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *         root of aComponent or a focus traversal policy provider, or if either aContainer or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *         aComponent is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    public Component getComponentBefore(Container aContainer, Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        if (aContainer == null || aComponent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            throw new IllegalArgumentException("aContainer and aComponent cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        if (!aContainer.isFocusTraversalPolicyProvider() && !aContainer.isFocusCycleRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            throw new IllegalArgumentException("aContainer should be focus cycle root or focus traversal policy provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        } else if (aContainer.isFocusCycleRoot() && !aComponent.isFocusCycleRoot(aContainer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            throw new IllegalArgumentException("aContainer is not a focus cycle root of aComponent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        // See if the component is inside of policy provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        Container provider = getTopmostProvider(aContainer, aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        if (provider != null) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3082
diff changeset
   352
            if (log.isLoggable(PlatformLogger.FINE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                log.fine("### Asking FTP " + provider + " for component after " + aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            // FTP knows how to find component after the given. We don't.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            FocusTraversalPolicy policy = provider.getFocusTraversalPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            Component beforeComp = policy.getComponentBefore(provider, aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            // Null result means that we overstepped the limit of the FTP's cycle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            // In that case we must quit the cycle, otherwise return the component found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            if (beforeComp != null) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3082
diff changeset
   363
                if (log.isLoggable(PlatformLogger.FINE)) log.fine("### FTP returned " + beforeComp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                return beforeComp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            aComponent = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            // If the provider is traversable it's returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            if (accept(aComponent)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                return aComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        List<Component> cycle = getFocusTraversalCycle(aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3082
diff changeset
   376
        if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Cycle is " + cycle + ", component is " + aComponent);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        int index = getComponentIndex(cycle, aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        if (index < 0) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3082
diff changeset
   381
            if (log.isLoggable(PlatformLogger.FINE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                log.fine("### Didn't find component " + aComponent + " in a cycle " + aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            return getLastComponent(aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   387
        Component comp;
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   388
        Component tryComp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        for (index--; index>=0; index--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            comp = cycle.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            if (comp != aContainer && (tryComp = getComponentDownCycle(comp, BACKWARD_TRAVERSAL)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                return tryComp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            } else if (accept(comp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        if (aContainer.isFocusCycleRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            this.cachedRoot = aContainer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            this.cachedCycle = cycle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            comp = getLastComponent(aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            this.cachedRoot = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            this.cachedCycle = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * Returns the first Component in the traversal cycle. This method is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * to determine the next Component to focus when traversal wraps in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * forward direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *        first Component is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @return the first Component in the traversal cycle of aContainer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *         or null if no suitable Component can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @throws IllegalArgumentException if aContainer is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    public Component getFirstComponent(Container aContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        List<Component> cycle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3082
diff changeset
   427
        if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Getting first component in " + aContainer);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        if (aContainer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            throw new IllegalArgumentException("aContainer cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        if (this.cachedRoot == aContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            cycle = this.cachedCycle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            cycle = getFocusTraversalCycle(aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        if (cycle.size() == 0) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3082
diff changeset
   439
            if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Cycle is empty");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        }
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3082
diff changeset
   442
        if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Cycle is " + cycle);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
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: 1639
diff changeset
   447
            } else if (comp != aContainer &&
24c8d93ac1e1 4788402: SortingFocusTraversalPolicy: prob with non-focusable focus Cycle Root as first
ant
parents: 1639
diff changeset
   448
                       (comp = getComponentDownCycle(comp, FORWARD_TRAVERSAL)) != null)
24c8d93ac1e1 4788402: SortingFocusTraversalPolicy: prob with non-focusable focus Cycle Root as first
ant
parents: 1639
diff changeset
   449
            {
24c8d93ac1e1 4788402: SortingFocusTraversalPolicy: prob with non-focusable focus Cycle Root as first
ant
parents: 1639
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
        return null;
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 last Component in the traversal cycle. This method is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * to determine the next Component to focus when traversal wraps in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * reverse direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     *        last Component is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * @return the last Component in the traversal cycle of aContainer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *         or null if no suitable Component can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * @throws IllegalArgumentException if aContainer is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    public Component getLastComponent(Container aContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        List<Component> cycle;
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3082
diff changeset
   469
        if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Getting last component in " + aContainer);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        if (aContainer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            throw new IllegalArgumentException("aContainer cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        if (this.cachedRoot == aContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            cycle = this.cachedCycle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            cycle = getFocusTraversalCycle(aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        if (cycle.size() == 0) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3082
diff changeset
   482
            if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Cycle is empty");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3082
diff changeset
   485
        if (log.isLoggable(PlatformLogger.FINE)) log.fine("### Cycle is " + cycle);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        for (int i= cycle.size() - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            Component comp = cycle.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            if (accept(comp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            } else if (comp instanceof Container && comp != aContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                Container cont = (Container)comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                if (cont.isFocusTraversalPolicyProvider()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                    return cont.getFocusTraversalPolicy().getLastComponent(cont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * Returns the default Component to focus. This Component will be the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * to receive focus when traversing down into a new focus traversal cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * rooted at aContainer. The default implementation of this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * returns the same Component as <code>getFirstComponent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *        default Component is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @return the default Component in the traversal cycle of aContainer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     *         or null if no suitable Component can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @see #getFirstComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @throws IllegalArgumentException if aContainer is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    public Component getDefaultComponent(Container aContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        return getFirstComponent(aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * Sets whether this SortingFocusTraversalPolicy transfers focus down-cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * implicitly. If <code>true</code>, during normal focus traversal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * the Component traversed after a focus cycle root will be the focus-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * cycle-root's default Component to focus. If <code>false</code>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * next Component in the focus traversal cycle rooted at the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * focus cycle root will be traversed instead. The default value for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * property is <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * @param implicitDownCycleTraversal whether this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     *        SortingFocusTraversalPolicy transfers focus down-cycle implicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * @see #getImplicitDownCycleTraversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @see #getFirstComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    public void setImplicitDownCycleTraversal(boolean implicitDownCycleTraversal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        this.implicitDownCycleTraversal = implicitDownCycleTraversal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * Returns whether this SortingFocusTraversalPolicy transfers focus down-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * cycle implicitly. If <code>true</code>, during normal focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * traversal, the Component traversed after a focus cycle root will be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * focus-cycle-root's default Component to focus. If <code>false</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * the next Component in the focus traversal cycle rooted at the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * focus cycle root will be traversed instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * @return whether this SortingFocusTraversalPolicy transfers focus down-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *         cycle implicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * @see #setImplicitDownCycleTraversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * @see #getFirstComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    public boolean getImplicitDownCycleTraversal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        return implicitDownCycleTraversal;
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
     * Sets the Comparator which will be used to sort the Components in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * focus traversal cycle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * @param comparator the Comparator which will be used for sorting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    protected void setComparator(Comparator<? super Component> comparator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        this.comparator = comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * Returns the Comparator which will be used to sort the Components in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * focus traversal cycle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * @return the Comparator which will be used for sorting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    protected Comparator<? super Component> getComparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        return comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * Determines whether a Component is an acceptable choice as the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * focus owner. By default, this method will accept a Component if and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * only if it is visible, displayable, enabled, and focusable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * @param aComponent the Component whose fitness as a focus owner is to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     *        be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * @return <code>true</code> if aComponent is visible, displayable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *         enabled, and focusable; <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    protected boolean accept(Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        return fitnessTestPolicy.accept(aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
// Create our own subclass and change accept to public so that we can call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
// accept.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
class SwingContainerOrderFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    extends java.awt.ContainerOrderFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    public boolean accept(Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        return super.accept(aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
}