1 /*
   2  * Copyright (c) 2000, 2020, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * 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()}. 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()} or
  40  * {@code Component.isFocusable()}, or by calling
  41  * {@code Component.setFocusable()}, 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="https://docs.oracle.com/javase/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      * Constructs a {@code DefaultFocusTraversalPolicy}.
  80      */
  81     public DefaultFocusTraversalPolicy() {}
  82 
  83     /**
  84      * Determines whether a Component is an acceptable choice as the new
  85      * focus owner. The Component must be visible, displayable, and enabled
  86      * to be accepted. If client code has explicitly set the focusability
  87      * of the Component by either overriding
  88      * {@code Component.isFocusTraversable()} or
  89      * {@code Component.isFocusable()}, or by calling
  90      * {@code Component.setFocusable()}, then the Component will be
  91      * accepted if and only if it is focusable. If, however, the Component is
  92      * relying on default focusability, then all Canvases, Labels, Panels,
  93      * Scrollbars, ScrollPanes, Windows, and lightweight Components will be
  94      * rejected.
  95      *
  96      * @param aComponent the Component whose fitness as a focus owner is to
  97      *        be tested
  98      * @return {@code true} if aComponent meets the above requirements;
  99      *         {@code false} otherwise
 100      */
 101     protected boolean accept(Component aComponent) {
 102         if (!(aComponent.isVisible() && aComponent.isDisplayable() &&
 103               aComponent.isEnabled()))
 104         {
 105             return false;
 106         }
 107 
 108         // Verify that the Component is recursively enabled. Disabling a
 109         // heavyweight Container disables its children, whereas disabling
 110         // a lightweight Container does not.
 111         if (!(aComponent instanceof Window)) {
 112             for (Container enableTest = aComponent.getParent();
 113                  enableTest != null;
 114                  enableTest = enableTest.getParent())
 115             {
 116                 if (!(enableTest.isEnabled() || enableTest.isLightweight())) {
 117                     return false;
 118                 }
 119                 if (enableTest instanceof Window) {
 120                     break;
 121                 }
 122             }
 123         }
 124 
 125         boolean focusable = aComponent.isFocusable();
 126         if (aComponent.isFocusTraversableOverridden()) {
 127             return focusable;
 128         }
 129 
 130         ComponentPeer peer = aComponent.peer;
 131         return (peer != null && peer.isFocusable());
 132     }
 133 }
--- EOF ---