jdk/src/share/classes/javax/swing/LayoutFocusTraversalPolicy.java
author malenkov
Wed, 07 May 2008 23:20:32 +0400
changeset 466 6acd5ec503a8
parent 2 90ce3da70b43
child 1301 15e81207e1f2
permissions -rw-r--r--
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE Summary: Add the Transient annotation and support it (JSR-273) Reviewed-by: peterz, loneid
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Container;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.ComponentOrientation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * A SortingFocusTraversalPolicy which sorts Components based on their size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * position, and orientation. Based on their size and position, Components are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * roughly categorized into rows and columns. For a Container with horizontal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * orientation, columns run left-to-right or right-to-left, and rows run top-
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * to-bottom. For a Container with vertical orientation, columns run top-to-
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * bottom and rows run left-to-right or right-to-left. See
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <code>ComponentOrientation</code> for more information. All columns in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * row are fully traversed before proceeding to the next row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author David Mendenhall
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @see java.awt.ComponentOrientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class LayoutFocusTraversalPolicy extends SortingFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    implements Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // Delegate most of our fitness test to Default so that we only have to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // code the algorithm once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static final SwingDefaultFocusTraversalPolicy fitnessTestPolicy =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        new SwingDefaultFocusTraversalPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Constructs a LayoutFocusTraversalPolicy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public LayoutFocusTraversalPolicy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        super(new LayoutComparator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Constructs a LayoutFocusTraversalPolicy with the passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * <code>Comparator</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    LayoutFocusTraversalPolicy(Comparator c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        super(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Returns the Component that should receive the focus after aComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * aContainer must be a focus cycle root of aComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * By default, LayoutFocusTraversalPolicy implicitly transfers focus down-
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * cycle. That is, during normal focus traversal, the Component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * traversed after a focus cycle root will be the focus-cycle-root's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * default Component to focus. This behavior can be disabled using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * <code>setImplicitDownCycleTraversal</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * If aContainer is <a href="../../java/awt/doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * traversal policy provider</a>, the focus is always transferred down-cycle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param aComponent a (possibly indirect) child of aContainer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *        aContainer itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @return the Component that should receive the focus after aComponent, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *         null if no suitable Component can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @throws IllegalArgumentException if aContainer is not a focus cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *         root of aComponent or a focus traversal policy provider, or if either aContainer or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *         aComponent is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public Component getComponentAfter(Container aContainer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                                       Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (aContainer == null || aComponent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throw new IllegalArgumentException("aContainer and aComponent cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        Comparator comparator = getComparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (comparator instanceof LayoutComparator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            ((LayoutComparator)comparator).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                setComponentOrientation(aContainer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                        getComponentOrientation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return super.getComponentAfter(aContainer, aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Returns the Component that should receive the focus before aComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * aContainer must be a focus cycle root of aComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * By default, LayoutFocusTraversalPolicy implicitly transfers focus down-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * cycle. That is, during normal focus traversal, the Component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * traversed after a focus cycle root will be the focus-cycle-root's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * default Component to focus. This behavior can be disabled using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * <code>setImplicitDownCycleTraversal</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * If aContainer is <a href="../../java/awt/doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * traversal policy provider</a>, the focus is always transferred down-cycle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param aComponent a (possibly indirect) child of aContainer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *        aContainer itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @return the Component that should receive the focus before aComponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *         or null if no suitable Component can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @throws IllegalArgumentException if aContainer is not a focus cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *         root of aComponent or a focus traversal policy provider, or if either aContainer or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *         aComponent is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public Component getComponentBefore(Container aContainer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                                        Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (aContainer == null || aComponent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            throw new IllegalArgumentException("aContainer and aComponent cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        Comparator comparator = getComparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (comparator instanceof LayoutComparator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            ((LayoutComparator)comparator).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                setComponentOrientation(aContainer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                        getComponentOrientation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return super.getComponentBefore(aContainer, aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Returns the first Component in the traversal cycle. This method is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * to determine the next Component to focus when traversal wraps in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * forward direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *        first Component is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @return the first Component in the traversal cycle of aContainer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *         or null if no suitable Component can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @throws IllegalArgumentException if aContainer is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public Component getFirstComponent(Container aContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (aContainer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            throw new IllegalArgumentException("aContainer cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        Comparator comparator = getComparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (comparator instanceof LayoutComparator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            ((LayoutComparator)comparator).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                setComponentOrientation(aContainer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                                        getComponentOrientation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return super.getFirstComponent(aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Returns the last Component in the traversal cycle. This method is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * to determine the next Component to focus when traversal wraps in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * reverse direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @param aContainer a focus cycle root of aComponent or a focus traversal policy provider whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *        last Component is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @return the last Component in the traversal cycle of aContainer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *         or null if no suitable Component can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @throws IllegalArgumentException if aContainer is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public Component getLastComponent(Container aContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (aContainer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            throw new IllegalArgumentException("aContainer cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        Comparator comparator = getComparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (comparator instanceof LayoutComparator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            ((LayoutComparator)comparator).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                setComponentOrientation(aContainer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                                        getComponentOrientation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return super.getLastComponent(aContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Determines whether the specified <code>Component</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * is an acceptable choice as the new focus owner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * This method performs the following sequence of operations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * <li>Checks whether <code>aComponent</code> is visible, displayable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *     enabled, and focusable.  If any of these properties is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *     <code>false</code>, this method returns <code>false</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * <li>If <code>aComponent</code> is an instance of <code>JTable</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *     returns <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * <li>If <code>aComponent</code> is an instance of <code>JComboBox</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *     then returns the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *     <code>aComponent.getUI().isFocusTraversable(aComponent)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * <li>If <code>aComponent</code> is a <code>JComponent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *     with a <code>JComponent.WHEN_FOCUSED</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *     <code>InputMap</code> that is neither <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *     nor empty, returns <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * <li>Returns the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *     <code>DefaultFocusTraversalPolicy.accept(aComponent)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @param aComponent the <code>Component</code> whose fitness
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *                   as a focus owner is to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @see java.awt.Component#isVisible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @see java.awt.Component#isDisplayable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @see java.awt.Component#isEnabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @see java.awt.Component#isFocusable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @see javax.swing.plaf.ComboBoxUI#isFocusTraversable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @see javax.swing.JComponent#getInputMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @see java.awt.DefaultFocusTraversalPolicy#accept
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @return <code>true</code> if <code>aComponent</code> is a valid choice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *         for a focus owner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *         otherwise <code>false</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     protected boolean accept(Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (!super.accept(aComponent)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } else if (aComponent instanceof JTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            // JTable only has ancestor focus bindings, we thus force it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            // to be focusable by returning true here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        } else if (aComponent instanceof JComboBox) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            JComboBox box = (JComboBox)aComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            return box.getUI().isFocusTraversable(box);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        } else if (aComponent instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            JComponent jComponent = (JComponent)aComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            InputMap inputMap = jComponent.getInputMap(JComponent.WHEN_FOCUSED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                                                       false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            while (inputMap != null && inputMap.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                inputMap = inputMap.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            if (inputMap != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            // Delegate to the fitnessTestPolicy, this will test for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            // case where the developer has overriden isFocusTraversable to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            // return true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        return fitnessTestPolicy.accept(aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    private void writeObject(ObjectOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        out.writeObject(getComparator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        out.writeBoolean(getImplicitDownCycleTraversal());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    private void readObject(ObjectInputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        setComparator((Comparator)in.readObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        setImplicitDownCycleTraversal(in.readBoolean());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
// Create our own subclass and change accept to public so that we can call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
// accept.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
class SwingDefaultFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    extends java.awt.DefaultFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public boolean accept(Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        return super.accept(aComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
}