jdk/src/share/classes/java/awt/DefaultFocusTraversalPolicy.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2000-2004 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 package java.awt;
       
    26 
       
    27 import java.awt.peer.ComponentPeer;
       
    28 
       
    29 
       
    30 /**
       
    31  * A FocusTraversalPolicy that determines traversal order based on the order
       
    32  * of child Components in a Container. From a particular focus cycle root, the
       
    33  * policy makes a pre-order traversal of the Component hierarchy, and traverses
       
    34  * a Container's children according to the ordering of the array returned by
       
    35  * <code>Container.getComponents()</code>. Portions of the hierarchy that are
       
    36  * not visible and displayable will not be searched.
       
    37  * <p>
       
    38  * If client code has explicitly set the focusability of a Component by either
       
    39  * overriding <code>Component.isFocusTraversable()</code> or
       
    40  * <code>Component.isFocusable()</code>, or by calling
       
    41  * <code>Component.setFocusable()</code>, then a DefaultFocusTraversalPolicy
       
    42  * behaves exactly like a ContainerOrderFocusTraversalPolicy. If, however, the
       
    43  * Component is relying on default focusability, then a
       
    44  * DefaultFocusTraversalPolicy will reject all Components with non-focusable
       
    45  * peers. This is the default FocusTraversalPolicy for all AWT Containers.
       
    46  * <p>
       
    47  * The focusability of a peer is implementation-dependent. Sun recommends that
       
    48  * all implementations for a particular native platform construct peers with
       
    49  * the same focusability. The recommendations for Windows and Unix are that
       
    50  * Canvases, Labels, Panels, Scrollbars, ScrollPanes, Windows, and lightweight
       
    51  * Components have non-focusable peers, and all other Components have focusable
       
    52  * peers. These recommendations are used in the Sun AWT implementations. Note
       
    53  * that the focusability of a Component's peer is different from, and does not
       
    54  * impact, the focusability of the Component itself.
       
    55  * <p>
       
    56  * Please see
       
    57  * <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html">
       
    58  * How to Use the Focus Subsystem</a>,
       
    59  * a section in <em>The Java Tutorial</em>, and the
       
    60  * <a href="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>
       
    61  * for more information.
       
    62  *
       
    63  * @author David Mendenhall
       
    64  *
       
    65  * @see Container#getComponents
       
    66  * @see Component#isFocusable
       
    67  * @see Component#setFocusable
       
    68  * @since 1.4
       
    69  */
       
    70 public class DefaultFocusTraversalPolicy
       
    71     extends ContainerOrderFocusTraversalPolicy
       
    72 {
       
    73     /*
       
    74      * serialVersionUID
       
    75      */
       
    76     private static final long serialVersionUID = 8876966522510157497L;
       
    77 
       
    78     /**
       
    79      * Determines whether a Component is an acceptable choice as the new
       
    80      * focus owner. The Component must be visible, displayable, and enabled
       
    81      * to be accepted. If client code has explicitly set the focusability
       
    82      * of the Component by either overriding
       
    83      * <code>Component.isFocusTraversable()</code> or
       
    84      * <code>Component.isFocusable()</code>, or by calling
       
    85      * <code>Component.setFocusable()</code>, then the Component will be
       
    86      * accepted if and only if it is focusable. If, however, the Component is
       
    87      * relying on default focusability, then all Canvases, Labels, Panels,
       
    88      * Scrollbars, ScrollPanes, Windows, and lightweight Components will be
       
    89      * rejected.
       
    90      *
       
    91      * @param aComponent the Component whose fitness as a focus owner is to
       
    92      *        be tested
       
    93      * @return <code>true</code> if aComponent meets the above requirements;
       
    94      *         <code>false</code> otherwise
       
    95      */
       
    96     protected boolean accept(Component aComponent) {
       
    97         if (!(aComponent.isVisible() && aComponent.isDisplayable() &&
       
    98               aComponent.isEnabled()))
       
    99         {
       
   100             return false;
       
   101         }
       
   102 
       
   103         // Verify that the Component is recursively enabled. Disabling a
       
   104         // heavyweight Container disables its children, whereas disabling
       
   105         // a lightweight Container does not.
       
   106         if (!(aComponent instanceof Window)) {
       
   107             for (Container enableTest = aComponent.getParent();
       
   108                  enableTest != null;
       
   109                  enableTest = enableTest.getParent())
       
   110             {
       
   111                 if (!(enableTest.isEnabled() || enableTest.isLightweight())) {
       
   112                     return false;
       
   113                 }
       
   114                 if (enableTest instanceof Window) {
       
   115                     break;
       
   116                 }
       
   117             }
       
   118         }
       
   119 
       
   120         boolean focusable = aComponent.isFocusable();
       
   121         if (aComponent.isFocusTraversableOverridden()) {
       
   122             return focusable;
       
   123         }
       
   124 
       
   125         ComponentPeer peer = aComponent.getPeer();
       
   126         return (peer != null && peer.isFocusable());
       
   127     }
       
   128 }