Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/awt/DefaultFocusTraversalPolicy.java
38829 views
/*1* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package java.awt;2526import java.awt.peer.ComponentPeer;272829/**30* A FocusTraversalPolicy that determines traversal order based on the order31* of child Components in a Container. From a particular focus cycle root, the32* policy makes a pre-order traversal of the Component hierarchy, and traverses33* a Container's children according to the ordering of the array returned by34* <code>Container.getComponents()</code>. Portions of the hierarchy that are35* not visible and displayable will not be searched.36* <p>37* If client code has explicitly set the focusability of a Component by either38* overriding <code>Component.isFocusTraversable()</code> or39* <code>Component.isFocusable()</code>, or by calling40* <code>Component.setFocusable()</code>, then a DefaultFocusTraversalPolicy41* behaves exactly like a ContainerOrderFocusTraversalPolicy. If, however, the42* Component is relying on default focusability, then a43* DefaultFocusTraversalPolicy will reject all Components with non-focusable44* peers. This is the default FocusTraversalPolicy for all AWT Containers.45* <p>46* The focusability of a peer is implementation-dependent. Sun recommends that47* all implementations for a particular native platform construct peers with48* the same focusability. The recommendations for Windows and Unix are that49* Canvases, Labels, Panels, Scrollbars, ScrollPanes, Windows, and lightweight50* Components have non-focusable peers, and all other Components have focusable51* peers. These recommendations are used in the Sun AWT implementations. Note52* that the focusability of a Component's peer is different from, and does not53* impact, the focusability of the Component itself.54* <p>55* Please see56* <a href="https://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html">57* How to Use the Focus Subsystem</a>,58* a section in <em>The Java Tutorial</em>, and the59* <a href="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>60* for more information.61*62* @author David Mendenhall63*64* @see Container#getComponents65* @see Component#isFocusable66* @see Component#setFocusable67* @since 1.468*/69public class DefaultFocusTraversalPolicy70extends ContainerOrderFocusTraversalPolicy71{72/*73* serialVersionUID74*/75private static final long serialVersionUID = 8876966522510157497L;7677/**78* Determines whether a Component is an acceptable choice as the new79* focus owner. The Component must be visible, displayable, and enabled80* to be accepted. If client code has explicitly set the focusability81* of the Component by either overriding82* <code>Component.isFocusTraversable()</code> or83* <code>Component.isFocusable()</code>, or by calling84* <code>Component.setFocusable()</code>, then the Component will be85* accepted if and only if it is focusable. If, however, the Component is86* relying on default focusability, then all Canvases, Labels, Panels,87* Scrollbars, ScrollPanes, Windows, and lightweight Components will be88* rejected.89*90* @param aComponent the Component whose fitness as a focus owner is to91* be tested92* @return <code>true</code> if aComponent meets the above requirements;93* <code>false</code> otherwise94*/95protected boolean accept(Component aComponent) {96if (!(aComponent.isVisible() && aComponent.isDisplayable() &&97aComponent.isEnabled()))98{99return false;100}101102// Verify that the Component is recursively enabled. Disabling a103// heavyweight Container disables its children, whereas disabling104// a lightweight Container does not.105if (!(aComponent instanceof Window)) {106for (Container enableTest = aComponent.getParent();107enableTest != null;108enableTest = enableTest.getParent())109{110if (!(enableTest.isEnabled() || enableTest.isLightweight())) {111return false;112}113if (enableTest instanceof Window) {114break;115}116}117}118119boolean focusable = aComponent.isFocusable();120if (aComponent.isFocusTraversableOverridden()) {121return focusable;122}123124ComponentPeer peer = aComponent.getPeer();125return (peer != null && peer.isFocusable());126}127}128129130