jdk/src/share/classes/java/awt/DefaultFocusTraversalPolicy.java
author chegar
Mon, 17 Jun 2013 14:09:25 +0100
changeset 18178 ee71c923891d
parent 5506 202f599c92aa
child 20455 f6f9a0c2796b
permissions -rw-r--r--
8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level) Reviewed-by: darcy
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: 2
diff changeset
     2
 * Copyright (c) 2000, 2004, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.peer.ComponentPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * A FocusTraversalPolicy that determines traversal order based on the order
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * of child Components in a Container. From a particular focus cycle root, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * policy makes a pre-order traversal of the Component hierarchy, and traverses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * a Container's children according to the ordering of the array returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <code>Container.getComponents()</code>. Portions of the hierarchy that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * not visible and displayable will not be searched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * If client code has explicitly set the focusability of a Component by either
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * overriding <code>Component.isFocusTraversable()</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <code>Component.isFocusable()</code>, or by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <code>Component.setFocusable()</code>, then a DefaultFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * behaves exactly like a ContainerOrderFocusTraversalPolicy. If, however, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Component is relying on default focusability, then a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * DefaultFocusTraversalPolicy will reject all Components with non-focusable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * peers. This is the default FocusTraversalPolicy for all AWT Containers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * The focusability of a peer is implementation-dependent. Sun recommends that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * all implementations for a particular native platform construct peers with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * the same focusability. The recommendations for Windows and Unix are that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Canvases, Labels, Panels, Scrollbars, ScrollPanes, Windows, and lightweight
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Components have non-focusable peers, and all other Components have focusable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * peers. These recommendations are used in the Sun AWT implementations. Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * that the focusability of a Component's peer is different from, and does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * impact, the focusability of the Component itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Please see
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * How to Use the Focus Subsystem</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * a section in <em>The Java Tutorial</em>, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <a href="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @author David Mendenhall
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * @see Container#getComponents
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * @see Component#isFocusable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * @see Component#setFocusable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
public class DefaultFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    extends ContainerOrderFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private static final long serialVersionUID = 8876966522510157497L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Determines whether a Component is an acceptable choice as the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * focus owner. The Component must be visible, displayable, and enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * to be accepted. If client code has explicitly set the focusability
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * of the Component by either overriding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * <code>Component.isFocusTraversable()</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * <code>Component.isFocusable()</code>, or by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * <code>Component.setFocusable()</code>, then the Component will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * accepted if and only if it is focusable. If, however, the Component is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * relying on default focusability, then all Canvases, Labels, Panels,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Scrollbars, ScrollPanes, Windows, and lightweight Components will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * rejected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @param aComponent the Component whose fitness as a focus owner is to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *        be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @return <code>true</code> if aComponent meets the above requirements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *         <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    protected boolean accept(Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (!(aComponent.isVisible() && aComponent.isDisplayable() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
              aComponent.isEnabled()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        // Verify that the Component is recursively enabled. Disabling a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        // heavyweight Container disables its children, whereas disabling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        // a lightweight Container does not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (!(aComponent instanceof Window)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            for (Container enableTest = aComponent.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                 enableTest != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                 enableTest = enableTest.getParent())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                if (!(enableTest.isEnabled() || enableTest.isLightweight())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                if (enableTest instanceof Window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        boolean focusable = aComponent.isFocusable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (aComponent.isFocusTraversableOverridden()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            return focusable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        ComponentPeer peer = aComponent.getPeer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return (peer != null && peer.isFocusable());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
}