Path: blob/master/src/java.desktop/share/classes/java/awt/DefaultFocusTraversalPolicy.java
41152 views
/*1* Copyright (c) 2000, 2021, 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*/2425package java.awt;2627import java.awt.peer.ComponentPeer;28import java.io.Serial;2930/**31* A FocusTraversalPolicy that determines traversal order based on the order32* of child Components in a Container. From a particular focus cycle root, the33* policy makes a pre-order traversal of the Component hierarchy, and traverses34* a Container's children according to the ordering of the array returned by35* {@code Container.getComponents()}. Portions of the hierarchy that are36* not visible and displayable will not be searched.37* <p>38* If client code has explicitly set the focusability of a Component by either39* overriding {@code Component.isFocusTraversable()} or40* {@code Component.isFocusable()}, or by calling41* {@code Component.setFocusable()}, then a DefaultFocusTraversalPolicy42* behaves exactly like a ContainerOrderFocusTraversalPolicy. If, however, the43* Component is relying on default focusability, then a44* DefaultFocusTraversalPolicy will reject all Components with non-focusable45* peers. This is the default FocusTraversalPolicy for all AWT Containers.46* <p>47* The focusability of a peer is implementation-dependent. Sun recommends that48* all implementations for a particular native platform construct peers with49* the same focusability. The recommendations for Windows and Unix are that50* Canvases, Labels, Panels, Scrollbars, ScrollPanes, Windows, and lightweight51* Components have non-focusable peers, and all other Components have focusable52* peers. These recommendations are used in the Sun AWT implementations. Note53* that the focusability of a Component's peer is different from, and does not54* impact, the focusability of the Component itself.55* <p>56* Please see57* <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 the60* <a href="doc-files/FocusSpec.html">Focus Specification</a>61* for more information.62*63* @author David Mendenhall64*65* @see Container#getComponents66* @see Component#isFocusable67* @see Component#setFocusable68* @since 1.469*/70public class DefaultFocusTraversalPolicy71extends ContainerOrderFocusTraversalPolicy72{73/**74* Use serialVersionUID from JDK 1.6 for interoperability.75*/76@Serial77private static final long serialVersionUID = 8876966522510157497L;7879/**80* Constructs a {@code DefaultFocusTraversalPolicy}.81*/82public DefaultFocusTraversalPolicy() {}8384/**85* Determines whether a Component is an acceptable choice as the new86* focus owner. The Component must be visible, displayable, and enabled87* to be accepted. If client code has explicitly set the focusability88* of the Component by either overriding89* {@code Component.isFocusTraversable()} or90* {@code Component.isFocusable()}, or by calling91* {@code Component.setFocusable()}, then the Component will be92* accepted if and only if it is focusable. If, however, the Component is93* relying on default focusability, then all Canvases, Labels, Panels,94* Scrollbars, ScrollPanes, Windows, and lightweight Components will be95* rejected.96*97* @param aComponent the Component whose fitness as a focus owner is to98* be tested99* @return {@code true} if aComponent meets the above requirements;100* {@code false} otherwise101*/102protected boolean accept(Component aComponent) {103if (!(aComponent.isVisible() && aComponent.isDisplayable() &&104aComponent.isEnabled()))105{106return false;107}108109// Verify that the Component is recursively enabled. Disabling a110// heavyweight Container disables its children, whereas disabling111// a lightweight Container does not.112if (!(aComponent instanceof Window)) {113for (Container enableTest = aComponent.getParent();114enableTest != null;115enableTest = enableTest.getParent())116{117if (!(enableTest.isEnabled() || enableTest.isLightweight())) {118return false;119}120if (enableTest instanceof Window) {121break;122}123}124}125126boolean focusable = aComponent.isFocusable();127if (aComponent.isFocusTraversableOverridden()) {128return focusable;129}130131ComponentPeer peer = aComponent.peer;132return (peer != null && peer.isFocusable());133}134}135136137