jdk/src/java.desktop/share/classes/javax/swing/SortingFocusTraversalPolicy.java
author aghaisas
Mon, 10 Jul 2017 14:55:29 +0530
changeset 47151 362dcbee0613
parent 40995 c096d4be5b5e
permissions -rw-r--r--
6919529: NPE from MultiUIDefaults.getUIError Reviewed-by: aghaisas, psadhukhan, serb Contributed-by: shashidhara.veerabhadraiah@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
40995
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
     2
 * Copyright (c) 2000, 2016, 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 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.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.FocusTraversalPolicy;
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3082
diff changeset
    31
import sun.util.logging.PlatformLogger;
25570
a72022403893 8048887: SortingFocusTraversalPolicy throws IllegalArgumentException from the sort method
ant
parents: 23697
diff changeset
    32
import sun.security.action.GetPropertyAction;
a72022403893 8048887: SortingFocusTraversalPolicy throws IllegalArgumentException from the sort method
ant
parents: 23697
diff changeset
    33
import java.security.AccessController;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * A FocusTraversalPolicy that determines traversal order by sorting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Components of a focus traversal cycle based on a given Comparator. Portions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * of the Component hierarchy that are not visible and displayable will not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * included.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * By default, SortingFocusTraversalPolicy implicitly transfers focus down-
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * cycle. That is, during normal focus traversal, the Component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * traversed after a focus cycle root will be the focus-cycle-root's default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Component to focus. This behavior can be disabled using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <code>setImplicitDownCycleTraversal</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * By default, methods of this class with return a Component only if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * visible, displayable, enabled, and focusable. Subclasses can modify this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * behavior by overriding the <code>accept</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * This policy takes into account <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * href="../../java/awt/doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus traversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * policy providers</a>.  When searching for first/last/next/previous Component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * if a focus traversal policy provider is encountered, its focus traversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * policy is used to perform the search operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @author David Mendenhall
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @see java.util.Comparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
public class SortingFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    extends InternalFrameFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private Comparator<? super Component> comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private boolean implicitDownCycleTraversal = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3082
diff changeset
    68
    private PlatformLogger log = PlatformLogger.getLogger("javax.swing.SortingFocusTraversalPolicy");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Used by getComponentAfter and getComponentBefore for efficiency. In
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * order to maintain compliance with the specification of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * FocusTraversalPolicy, if traversal wraps, we should invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * getFirstComponent or getLastComponent. These methods may be overriden in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * subclasses to behave in a non-generic way. However, in the generic case,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * these methods will simply return the first or last Components of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * sorted list, respectively. Since getComponentAfter and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * getComponentBefore have already built the sorted list before determining
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * that they need to invoke getFirstComponent or getLastComponent, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * sorted list should be reused if possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 30458
diff changeset
    82
    private transient Container cachedRoot;
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 30458
diff changeset
    83
    private transient List<Component> cachedCycle;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // Delegate our fitness test to ContainerOrder so that we only have to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    // code the algorithm once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private static final SwingContainerOrderFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        fitnessTestPolicy = new SwingContainerOrderFocusTraversalPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 30458
diff changeset
    90
    private final int FORWARD_TRAVERSAL = 0;
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 30458
diff changeset
    91
    private final int BACKWARD_TRAVERSAL = 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
25570
a72022403893 8048887: SortingFocusTraversalPolicy throws IllegalArgumentException from the sort method
ant
parents: 23697
diff changeset
    93
    /*
a72022403893 8048887: SortingFocusTraversalPolicy throws IllegalArgumentException from the sort method
ant
parents: 23697
diff changeset
    94
     * When true (by default), the legacy merge-sort algo is used to sort an FTP cycle.
a72022403893 8048887: SortingFocusTraversalPolicy throws IllegalArgumentException from the sort method
ant
parents: 23697
diff changeset
    95
     * When false, the default (tim-sort) algo is used, which may lead to an exception.
a72022403893 8048887: SortingFocusTraversalPolicy throws IllegalArgumentException from the sort method
ant
parents: 23697
diff changeset
    96
     * See: JDK-8048887
a72022403893 8048887: SortingFocusTraversalPolicy throws IllegalArgumentException from the sort method
ant
parents: 23697
diff changeset
    97
     */
a72022403893 8048887: SortingFocusTraversalPolicy throws IllegalArgumentException from the sort method
ant
parents: 23697
diff changeset
    98
    private static final boolean legacySortingFTPEnabled;
a72022403893 8048887: SortingFocusTraversalPolicy throws IllegalArgumentException from the sort method
ant
parents: 23697
diff changeset
    99
a72022403893 8048887: SortingFocusTraversalPolicy throws IllegalArgumentException from the sort method
ant
parents: 23697
diff changeset
   100
    static {
a72022403893 8048887: SortingFocusTraversalPolicy throws IllegalArgumentException from the sort method
ant
parents: 23697
diff changeset
   101
        legacySortingFTPEnabled = "true".equals(AccessController.doPrivileged(
a72022403893 8048887: SortingFocusTraversalPolicy throws IllegalArgumentException from the sort method
ant
parents: 23697
diff changeset
   102
            new GetPropertyAction("swing.legacySortingFTPEnabled", "true")));
a72022403893 8048887: SortingFocusTraversalPolicy throws IllegalArgumentException from the sort method
ant
parents: 23697
diff changeset
   103
    }
a72022403893 8048887: SortingFocusTraversalPolicy throws IllegalArgumentException from the sort method
ant
parents: 23697
diff changeset
   104
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Constructs a SortingFocusTraversalPolicy without a Comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Subclasses must set the Comparator using <code>setComparator</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * before installing this FocusTraversalPolicy on a focus cycle root or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * KeyboardFocusManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    protected SortingFocusTraversalPolicy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Constructs a SortingFocusTraversalPolicy with the specified Comparator.
25386
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 23697
diff changeset
   116
     *
9ef80c24fd74 8046590: fix doclint issues in swing classes, part 1 of 4
yan
parents: 23697
diff changeset
   117
     * @param comparator the {@code Comparator} to sort by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public SortingFocusTraversalPolicy(Comparator<? super Component> comparator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        this.comparator = comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private List<Component> getFocusTraversalCycle(Container aContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        List<Component> cycle = new ArrayList<Component>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        enumerateAndSortCycle(aContainer, cycle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return cycle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private int getComponentIndex(List<Component> cycle, Component aComponent) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   129
        int index;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            index = Collections.binarySearch(cycle, aComponent, comparator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        } catch (ClassCastException e) {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   133
            if (log.isLoggable(PlatformLogger.Level.FINE)) {
21591
35320b590d9b 8026491: Typos in string literals
malenkov
parents: 18178
diff changeset
   134
                log.fine("### During the binary search for " + aComponent + " the exception occurred: ", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (index < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            // Fix for 5070991.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            // A workaround for a transitivity problem caused by ROW_TOLERANCE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            // because of that the component may be missed in the binary search.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            // Try to search it again directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            index = cycle.indexOf(aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   148
    private void enumerateAndSortCycle(Container focusCycleRoot, List<Component> cycle) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (focusCycleRoot.isShowing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            enumerateCycle(focusCycleRoot, cycle);
40995
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   151
            if (legacySortingFTPEnabled) {
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   152
                legacySort(cycle, comparator);
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   153
            } else {
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   154
                cycle.sort(comparator);
25570
a72022403893 8048887: SortingFocusTraversalPolicy throws IllegalArgumentException from the sort method
ant
parents: 23697
diff changeset
   155
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
40995
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   159
    private void legacySort(List<Component> l,
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   160
                                              Comparator<? super Component> c) {
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   161
        if (c != null && l.size() > 1) {
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   162
            Component[] a = l.toArray(new Component[l.size()]);
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   163
            mergeSort(a.clone(), a, 0, a.length, 0, c);
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   164
            ListIterator<Component> i = l.listIterator();
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   165
            for (Component e : a) {
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   166
                i.next();
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   167
                i.set(e);
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   168
            }
25570
a72022403893 8048887: SortingFocusTraversalPolicy throws IllegalArgumentException from the sort method
ant
parents: 23697
diff changeset
   169
        }
a72022403893 8048887: SortingFocusTraversalPolicy throws IllegalArgumentException from the sort method
ant
parents: 23697
diff changeset
   170
    }
a72022403893 8048887: SortingFocusTraversalPolicy throws IllegalArgumentException from the sort method
ant
parents: 23697
diff changeset
   171
28231
b608ffcaed74 8066621: Suppress deprecation warnings in java.desktop module
darcy
parents: 25859
diff changeset
   172
    @SuppressWarnings("deprecation")
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   173
    private void enumerateCycle(Container container, List<Component> cycle) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (!(container.isVisible() && container.isDisplayable())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        cycle.add(container);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        Component[] components = container.getComponents();
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   181
        for (Component comp : components) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            if (comp instanceof Container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                Container cont = (Container)comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                if (!cont.isFocusCycleRoot() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    !cont.isFocusTraversalPolicyProvider() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    !((cont instanceof JComponent) && ((JComponent)cont).isManagingFocus()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    enumerateCycle(cont, cycle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            cycle.add(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    Container getTopmostProvider(Container focusCycleRoot, Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        Container aCont = aComponent.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        Container ftp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        while (aCont  != focusCycleRoot && aCont != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            if (aCont.isFocusTraversalPolicyProvider()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                ftp = aCont;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            aCont = aCont.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (aCont == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return ftp;
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
     * Checks if a new focus cycle takes place and returns a Component to traverse focus to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @param comp a possible focus cycle root or policy provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @param traversalDirection the direction of the traversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @return a Component to traverse focus to if {@code comp} is a root or provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *         and implicit down-cycle is set, otherwise {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    private Component getComponentDownCycle(Component comp, int traversalDirection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        Component retComp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if (comp instanceof Container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            Container cont = (Container)comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            if (cont.isFocusCycleRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                if (getImplicitDownCycleTraversal()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    retComp = cont.getFocusTraversalPolicy().getDefaultComponent(cont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   229
                    if (retComp != null && log.isLoggable(PlatformLogger.Level.FINE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                        log.fine("### Transfered focus down-cycle to " + retComp +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                                 " in the focus cycle root " + cont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            } else if (cont.isFocusTraversalPolicyProvider()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                retComp = (traversalDirection == FORWARD_TRAVERSAL ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                           cont.getFocusTraversalPolicy().getDefaultComponent(cont) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                           cont.getFocusTraversalPolicy().getLastComponent(cont));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   241
                if (retComp != null && log.isLoggable(PlatformLogger.Level.FINE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    log.fine("### Transfered focus to " + retComp + " in the FTP provider " + cont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        return retComp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * Returns the Component that should receive the focus after aComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * aContainer must be a focus cycle root of aComponent or a focus traversal policy provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * By default, SortingFocusTraversalPolicy implicitly transfers focus down-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * cycle. That is, during normal focus traversal, the Component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * traversed after a focus cycle root will be the focus-cycle-root's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * default Component to focus. This behavior can be disabled using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * <code>setImplicitDownCycleTraversal</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * If aContainer is <a href="../../java/awt/doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * traversal policy provider</a>, the focus is always transferred down-cycle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @param aComponent a (possibly indirect) child of aContainer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *        aContainer itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @return the Component that should receive the focus after aComponent, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *         null if no suitable Component can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @throws IllegalArgumentException if aContainer is not a focus cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *         root of aComponent or a focus traversal policy provider, or if either aContainer or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *         aComponent is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    public Component getComponentAfter(Container aContainer, Component aComponent) {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   272
        if (log.isLoggable(PlatformLogger.Level.FINE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            log.fine("### Searching in " + aContainer + " for component after " + aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (aContainer == null || aComponent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            throw new IllegalArgumentException("aContainer and aComponent cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (!aContainer.isFocusTraversalPolicyProvider() && !aContainer.isFocusCycleRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            throw new IllegalArgumentException("aContainer should be focus cycle root or focus traversal policy provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        } else if (aContainer.isFocusCycleRoot() && !aComponent.isFocusCycleRoot(aContainer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            throw new IllegalArgumentException("aContainer is not a focus cycle root of aComponent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        // 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
   287
        // If it's the case just go down cycle (if it's set to "implicit").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        Component comp = getComponentDownCycle(aComponent, FORWARD_TRAVERSAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (comp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        // See if the component is inside of policy provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        Container provider = getTopmostProvider(aContainer, aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (provider != null) {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   296
            if (log.isLoggable(PlatformLogger.Level.FINE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                log.fine("### Asking FTP " + provider + " for component after " + aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            // FTP knows how to find component after the given. We don't.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            FocusTraversalPolicy policy = provider.getFocusTraversalPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            Component afterComp = policy.getComponentAfter(provider, aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            // Null result means that we overstepped the limit of the FTP's cycle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            // In that case we must quit the cycle, otherwise return the component found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            if (afterComp != null) {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   307
                if (log.isLoggable(PlatformLogger.Level.FINE)) {
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   308
                    log.fine("### FTP returned " + afterComp);
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   309
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                return afterComp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            aComponent = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        List<Component> cycle = getFocusTraversalCycle(aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   317
        if (log.isLoggable(PlatformLogger.Level.FINE)) {
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   318
            log.fine("### Cycle is " + cycle + ", component is " + aComponent);
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   319
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        int index = getComponentIndex(cycle, aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (index < 0) {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   324
            if (log.isLoggable(PlatformLogger.Level.FINE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                log.fine("### Didn't find component " + aComponent + " in a cycle " + aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            return getFirstComponent(aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        for (index++; index < cycle.size(); index++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            comp = cycle.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            if (accept(comp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            } else if ((comp = getComponentDownCycle(comp, FORWARD_TRAVERSAL)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        if (aContainer.isFocusCycleRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            this.cachedRoot = aContainer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            this.cachedCycle = cycle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            comp = getFirstComponent(aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            this.cachedRoot = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            this.cachedCycle = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * Returns the Component that should receive the focus before aComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * aContainer must be a focus cycle root of aComponent or a focus traversal policy provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * By default, SortingFocusTraversalPolicy implicitly transfers focus down-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * cycle. That is, during normal focus traversal, the Component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * traversed after a focus cycle root will be the focus-cycle-root's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * default Component to focus. This behavior can be disabled using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * <code>setImplicitDownCycleTraversal</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * If aContainer is <a href="../../java/awt/doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * traversal policy provider</a>, the focus is always transferred down-cycle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @param aComponent a (possibly indirect) child of aContainer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *        aContainer itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @return the Component that should receive the focus before aComponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *         or null if no suitable Component can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @throws IllegalArgumentException if aContainer is not a focus cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *         root of aComponent or a focus traversal policy provider, or if either aContainer or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *         aComponent is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    public Component getComponentBefore(Container aContainer, Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        if (aContainer == null || aComponent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            throw new IllegalArgumentException("aContainer and aComponent cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        if (!aContainer.isFocusTraversalPolicyProvider() && !aContainer.isFocusCycleRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            throw new IllegalArgumentException("aContainer should be focus cycle root or focus traversal policy provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        } else if (aContainer.isFocusCycleRoot() && !aComponent.isFocusCycleRoot(aContainer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            throw new IllegalArgumentException("aContainer is not a focus cycle root of aComponent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        // See if the component is inside of policy provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        Container provider = getTopmostProvider(aContainer, aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        if (provider != null) {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   389
            if (log.isLoggable(PlatformLogger.Level.FINE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                log.fine("### Asking FTP " + provider + " for component after " + aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            // FTP knows how to find component after the given. We don't.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            FocusTraversalPolicy policy = provider.getFocusTraversalPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            Component beforeComp = policy.getComponentBefore(provider, aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            // Null result means that we overstepped the limit of the FTP's cycle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            // In that case we must quit the cycle, otherwise return the component found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            if (beforeComp != null) {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   400
                if (log.isLoggable(PlatformLogger.Level.FINE)) {
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   401
                    log.fine("### FTP returned " + beforeComp);
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   402
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                return beforeComp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            aComponent = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            // If the provider is traversable it's returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            if (accept(aComponent)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                return aComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        List<Component> cycle = getFocusTraversalCycle(aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   415
        if (log.isLoggable(PlatformLogger.Level.FINE)) {
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   416
            log.fine("### Cycle is " + cycle + ", component is " + aComponent);
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   417
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        int index = getComponentIndex(cycle, aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        if (index < 0) {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   422
            if (log.isLoggable(PlatformLogger.Level.FINE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                log.fine("### Didn't find component " + aComponent + " in a cycle " + aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            return getLastComponent(aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   428
        Component comp;
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   429
        Component tryComp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        for (index--; index>=0; index--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            comp = cycle.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            if (comp != aContainer && (tryComp = getComponentDownCycle(comp, BACKWARD_TRAVERSAL)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                return tryComp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            } else if (accept(comp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        if (aContainer.isFocusCycleRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            this.cachedRoot = aContainer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            this.cachedCycle = cycle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            comp = getLastComponent(aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            this.cachedRoot = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            this.cachedCycle = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * Returns the first Component in the traversal cycle. This method is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * to determine the next Component to focus when traversal wraps in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * forward direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *        first Component is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * @return the first Component in the traversal cycle of aContainer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     *         or null if no suitable Component can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * @throws IllegalArgumentException if aContainer is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    public Component getFirstComponent(Container aContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        List<Component> cycle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   468
        if (log.isLoggable(PlatformLogger.Level.FINE)) {
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   469
            log.fine("### Getting first component in " + aContainer);
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   470
        }
2
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) {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   482
            if (log.isLoggable(PlatformLogger.Level.FINE)) {
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   483
                log.fine("### Cycle is empty");
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   484
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        }
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   487
        if (log.isLoggable(PlatformLogger.Level.FINE)) {
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   488
            log.fine("### Cycle is " + cycle);
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   489
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   491
        for (Component comp : cycle) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            if (accept(comp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                return comp;
3082
24c8d93ac1e1 4788402: SortingFocusTraversalPolicy: prob with non-focusable focus Cycle Root as first
ant
parents: 1639
diff changeset
   494
            } else if (comp != aContainer &&
24c8d93ac1e1 4788402: SortingFocusTraversalPolicy: prob with non-focusable focus Cycle Root as first
ant
parents: 1639
diff changeset
   495
                       (comp = getComponentDownCycle(comp, FORWARD_TRAVERSAL)) != null)
24c8d93ac1e1 4788402: SortingFocusTraversalPolicy: prob with non-focusable focus Cycle Root as first
ant
parents: 1639
diff changeset
   496
            {
24c8d93ac1e1 4788402: SortingFocusTraversalPolicy: prob with non-focusable focus Cycle Root as first
ant
parents: 1639
diff changeset
   497
                return comp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * Returns the last Component in the traversal cycle. This method is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * to determine the next Component to focus when traversal wraps in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * reverse direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *        last Component is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * @return the last Component in the traversal cycle of aContainer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *         or null if no suitable Component can be found
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 getLastComponent(Container aContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        List<Component> cycle;
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   516
        if (log.isLoggable(PlatformLogger.Level.FINE)) {
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   517
            log.fine("### Getting last component in " + aContainer);
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   518
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        if (aContainer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            throw new IllegalArgumentException("aContainer cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        if (this.cachedRoot == aContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            cycle = this.cachedCycle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            cycle = getFocusTraversalCycle(aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        if (cycle.size() == 0) {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   531
            if (log.isLoggable(PlatformLogger.Level.FINE)) {
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   532
                log.fine("### Cycle is empty");
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   533
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        }
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   536
        if (log.isLoggable(PlatformLogger.Level.FINE)) {
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   537
            log.fine("### Cycle is " + cycle);
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 5506
diff changeset
   538
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        for (int i= cycle.size() - 1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            Component comp = cycle.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            if (accept(comp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            } else if (comp instanceof Container && comp != aContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                Container cont = (Container)comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                if (cont.isFocusTraversalPolicyProvider()) {
30458
e8bb8c0f25ba 8073453: Focus doesn't move when pressing Shift + Tab keys
dmarkov
parents: 28231
diff changeset
   547
                    Component retComp = cont.getFocusTraversalPolicy().getLastComponent(cont);
e8bb8c0f25ba 8073453: Focus doesn't move when pressing Shift + Tab keys
dmarkov
parents: 28231
diff changeset
   548
                    if (retComp != null) {
e8bb8c0f25ba 8073453: Focus doesn't move when pressing Shift + Tab keys
dmarkov
parents: 28231
diff changeset
   549
                        return retComp;
e8bb8c0f25ba 8073453: Focus doesn't move when pressing Shift + Tab keys
dmarkov
parents: 28231
diff changeset
   550
                    }
2
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
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * Returns the default Component to focus. This Component will be the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * to receive focus when traversing down into a new focus traversal cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * rooted at aContainer. The default implementation of this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * returns the same Component as <code>getFirstComponent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     *        default Component is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * @return the default Component in the traversal cycle of aContainer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *         or null if no suitable Component can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * @see #getFirstComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * @throws IllegalArgumentException if aContainer is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    public Component getDefaultComponent(Container aContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        return getFirstComponent(aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * Sets whether this SortingFocusTraversalPolicy transfers focus down-cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * implicitly. If <code>true</code>, during normal focus traversal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * the Component traversed after a focus cycle root will be the focus-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * cycle-root's default Component to focus. If <code>false</code>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * next Component in the focus traversal cycle rooted at the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * focus cycle root will be traversed instead. The default value for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * property is <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @param implicitDownCycleTraversal whether this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     *        SortingFocusTraversalPolicy transfers focus down-cycle implicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * @see #getImplicitDownCycleTraversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * @see #getFirstComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    public void setImplicitDownCycleTraversal(boolean implicitDownCycleTraversal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        this.implicitDownCycleTraversal = implicitDownCycleTraversal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * Returns whether this SortingFocusTraversalPolicy transfers focus down-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * cycle implicitly. If <code>true</code>, during normal focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * traversal, the Component traversed after a focus cycle root will be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * focus-cycle-root's default Component to focus. If <code>false</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * the next Component in the focus traversal cycle rooted at the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * focus cycle root will be traversed instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * @return whether this SortingFocusTraversalPolicy transfers focus down-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     *         cycle implicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * @see #setImplicitDownCycleTraversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * @see #getFirstComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    public boolean getImplicitDownCycleTraversal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        return implicitDownCycleTraversal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * Sets the Comparator which will be used to sort the Components in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * focus traversal cycle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * @param comparator the Comparator which will be used for sorting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    protected void setComparator(Comparator<? super Component> comparator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        this.comparator = comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * Returns the Comparator which will be used to sort the Components in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * focus traversal cycle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * @return the Comparator which will be used for sorting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    protected Comparator<? super Component> getComparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        return comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * Determines whether a Component is an acceptable choice as the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * focus owner. By default, this method will accept a Component if and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * only if it is visible, displayable, enabled, and focusable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * @param aComponent the Component whose fitness as a focus owner is to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     *        be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * @return <code>true</code> if aComponent is visible, displayable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     *         enabled, and focusable; <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    protected boolean accept(Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        return fitnessTestPolicy.accept(aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    }
40995
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   642
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   643
    // merge sort implementation copied from java.utils.Arrays
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   644
    private <T> void mergeSort(T[] src, T[] dest,
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   645
                               int low, int high, int off,
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   646
                               Comparator<? super T> c) {
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   647
        int length = high - low;
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   648
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   649
        // Insertion sort on smallest arrays
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   650
        if (length < 7) {
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   651
            for (int i=low; i<high; i++)
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   652
                for (int j=i; j>low && c.compare(dest[j-1], dest[j])>0; j--) {
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   653
                    T t = dest[j];
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   654
                    dest[j] = dest[j-1];
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   655
                    dest[j-1] = t;
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   656
                }
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   657
            return;
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   658
        }
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   659
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   660
        // Recursively sort halves of dest into src
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   661
        int destLow  = low;
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   662
        int destHigh = high;
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   663
        low  += off;
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   664
        high += off;
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   665
        int mid = (low + high) >>> 1;
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   666
        mergeSort(dest, src, low, mid, -off, c);
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   667
        mergeSort(dest, src, mid, high, -off, c);
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   668
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   669
        // If list is already sorted, just copy from src to dest.  This is an
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   670
        // optimization that results in faster sorts for nearly ordered lists.
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   671
        if (c.compare(src[mid-1], src[mid]) <= 0) {
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   672
            System.arraycopy(src, low, dest, destLow, length);
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   673
            return;
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   674
        }
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   675
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   676
        // Merge sorted halves (now in src) into dest
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   677
        for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   678
            if (q >= high || p < mid && c.compare(src[p], src[q]) <= 0)
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   679
                dest[i] = src[p++];
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   680
            else
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   681
                dest[i] = src[q++];
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   682
        }
c096d4be5b5e 8164937: Remove code from SortingFocusTraversalPolicy that hacks into non-public Arrays.legacyMergeSort
ssadetsky
parents: 32865
diff changeset
   683
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
// Create our own subclass and change accept to public so that we can call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
// accept.
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 23010
diff changeset
   688
@SuppressWarnings("serial") // JDK-implementation class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
class SwingContainerOrderFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    extends java.awt.ContainerOrderFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    public boolean accept(Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        return super.accept(aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
}