jdk/src/share/classes/javax/swing/LayoutFocusTraversalPolicy.java
author ant
Thu, 17 May 2012 21:48:57 +0400
changeset 12661 6cf8b7116579
parent 5506 202f599c92aa
child 23697 e556a715949f
permissions -rw-r--r--
7125044: [macosx] Test failure because Component.transferFocus() works differently in applet and application. Summary: forward port from 7u4 Reviewed-by: art
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3084
diff changeset
     2
 * Copyright (c) 2000, 2008, 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: 3084
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: 3084
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: 3084
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3084
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3084
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.awt.ComponentOrientation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
3084
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1639
diff changeset
    32
import sun.awt.SunToolkit;
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
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * A SortingFocusTraversalPolicy which sorts Components based on their size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * position, and orientation. Based on their size and position, Components are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * roughly categorized into rows and columns. For a Container with horizontal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * orientation, columns run left-to-right or right-to-left, and rows run top-
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * to-bottom. For a Container with vertical orientation, columns run top-to-
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * bottom and rows run left-to-right or right-to-left. See
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <code>ComponentOrientation</code> for more information. All columns in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * row are fully traversed before proceeding to the next row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author David Mendenhall
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @see java.awt.ComponentOrientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public class LayoutFocusTraversalPolicy extends SortingFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    implements Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // Delegate most of our fitness test to Default so that we only have to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // code the algorithm once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static final SwingDefaultFocusTraversalPolicy fitnessTestPolicy =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        new SwingDefaultFocusTraversalPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * Constructs a LayoutFocusTraversalPolicy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public LayoutFocusTraversalPolicy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        super(new LayoutComparator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Constructs a LayoutFocusTraversalPolicy with the passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * <code>Comparator</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
    69
    LayoutFocusTraversalPolicy(Comparator<? super Component> c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        super(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Returns the Component that should receive the focus after aComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * aContainer must be a focus cycle root of aComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * By default, LayoutFocusTraversalPolicy implicitly transfers focus down-
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * cycle. That is, during normal focus traversal, the Component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * traversed after a focus cycle root will be the focus-cycle-root's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * default Component to focus. This behavior can be disabled using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * <code>setImplicitDownCycleTraversal</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * If aContainer is <a href="../../java/awt/doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * traversal policy provider</a>, the focus is always transferred down-cycle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @param aComponent a (possibly indirect) child of aContainer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *        aContainer itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @return the Component that should receive the focus after aComponent, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *         null if no suitable Component can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @throws IllegalArgumentException if aContainer is not a focus cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *         root of aComponent or a focus traversal policy provider, or if either aContainer or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *         aComponent is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public Component getComponentAfter(Container aContainer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                                       Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (aContainer == null || aComponent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            throw new IllegalArgumentException("aContainer and aComponent cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        Comparator comparator = getComparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (comparator instanceof LayoutComparator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            ((LayoutComparator)comparator).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                setComponentOrientation(aContainer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                                        getComponentOrientation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return super.getComponentAfter(aContainer, aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Returns the Component that should receive the focus before aComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * aContainer must be a focus cycle root of aComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * By default, LayoutFocusTraversalPolicy implicitly transfers focus down-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * cycle. That is, during normal focus traversal, the Component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * traversed after a focus cycle root will be the focus-cycle-root's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * default Component to focus. This behavior can be disabled using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * <code>setImplicitDownCycleTraversal</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * If aContainer is <a href="../../java/awt/doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * traversal policy provider</a>, the focus is always transferred down-cycle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @param aComponent a (possibly indirect) child of aContainer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *        aContainer itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @return the Component that should receive the focus before aComponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *         or null if no suitable Component can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @throws IllegalArgumentException if aContainer is not a focus cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *         root of aComponent or a focus traversal policy provider, or if either aContainer or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *         aComponent is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public Component getComponentBefore(Container aContainer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                                        Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (aContainer == null || aComponent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            throw new IllegalArgumentException("aContainer and aComponent cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        Comparator comparator = getComparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (comparator instanceof LayoutComparator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            ((LayoutComparator)comparator).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                setComponentOrientation(aContainer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                                        getComponentOrientation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        return super.getComponentBefore(aContainer, aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Returns the first Component in the traversal cycle. This method is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * to determine the next Component to focus when traversal wraps in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * forward direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *        first Component is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @return the first Component in the traversal cycle of aContainer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *         or null if no suitable Component can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @throws IllegalArgumentException if aContainer is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public Component getFirstComponent(Container aContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (aContainer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            throw new IllegalArgumentException("aContainer cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        Comparator comparator = getComparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (comparator instanceof LayoutComparator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            ((LayoutComparator)comparator).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                setComponentOrientation(aContainer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                        getComponentOrientation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return super.getFirstComponent(aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Returns the last Component in the traversal cycle. This method is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * to determine the next Component to focus when traversal wraps in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * reverse direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *        last Component is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @return the last Component in the traversal cycle of aContainer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *         or null if no suitable Component can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @throws IllegalArgumentException if aContainer is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public Component getLastComponent(Container aContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (aContainer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            throw new IllegalArgumentException("aContainer cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        Comparator comparator = getComparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (comparator instanceof LayoutComparator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            ((LayoutComparator)comparator).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                setComponentOrientation(aContainer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                        getComponentOrientation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return super.getLastComponent(aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Determines whether the specified <code>Component</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * is an acceptable choice as the new focus owner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * This method performs the following sequence of operations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * <li>Checks whether <code>aComponent</code> is visible, displayable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *     enabled, and focusable.  If any of these properties is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *     <code>false</code>, this method returns <code>false</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * <li>If <code>aComponent</code> is an instance of <code>JTable</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *     returns <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * <li>If <code>aComponent</code> is an instance of <code>JComboBox</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *     then returns the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *     <code>aComponent.getUI().isFocusTraversable(aComponent)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * <li>If <code>aComponent</code> is a <code>JComponent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *     with a <code>JComponent.WHEN_FOCUSED</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *     <code>InputMap</code> that is neither <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *     nor empty, returns <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * <li>Returns the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *     <code>DefaultFocusTraversalPolicy.accept(aComponent)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @param aComponent the <code>Component</code> whose fitness
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *                   as a focus owner is to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @see java.awt.Component#isVisible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @see java.awt.Component#isDisplayable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @see java.awt.Component#isEnabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @see java.awt.Component#isFocusable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @see javax.swing.plaf.ComboBoxUI#isFocusTraversable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @see javax.swing.JComponent#getInputMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @see java.awt.DefaultFocusTraversalPolicy#accept
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @return <code>true</code> if <code>aComponent</code> is a valid choice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *         for a focus owner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *         otherwise <code>false</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     protected boolean accept(Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (!super.accept(aComponent)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            return false;
3084
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1639
diff changeset
   230
        } else if (SunToolkit.isInstanceOf(aComponent, "javax.swing.JTable")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            // JTable only has ancestor focus bindings, we thus force it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            // to be focusable by returning true here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            return true;
3084
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 1639
diff changeset
   234
        } else if (SunToolkit.isInstanceOf(aComponent, "javax.swing.JComboBox")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            JComboBox box = (JComboBox)aComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            return box.getUI().isFocusTraversable(box);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        } else if (aComponent instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            JComponent jComponent = (JComponent)aComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            InputMap inputMap = jComponent.getInputMap(JComponent.WHEN_FOCUSED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                                                       false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            while (inputMap != null && inputMap.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                inputMap = inputMap.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            if (inputMap != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            // Delegate to the fitnessTestPolicy, this will test for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            // case where the developer has overriden isFocusTraversable to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            // return true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return fitnessTestPolicy.accept(aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    private void writeObject(ObjectOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        out.writeObject(getComparator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        out.writeBoolean(getImplicitDownCycleTraversal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    private void readObject(ObjectInputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        setComparator((Comparator)in.readObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        setImplicitDownCycleTraversal(in.readBoolean());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
// Create our own subclass and change accept to public so that we can call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
// accept.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
class SwingDefaultFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    extends java.awt.DefaultFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    public boolean accept(Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        return super.accept(aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
}