Path: blob/master/src/java.desktop/share/classes/java/awt/FocusTraversalPolicy.java
41152 views
/*1* Copyright (c) 2000, 2020, 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;2526/**27* A FocusTraversalPolicy defines the order in which Components with a28* particular focus cycle root are traversed. Instances can apply the policy to29* arbitrary focus cycle roots, allowing themselves to be shared across30* Containers. They do not need to be reinitialized when the focus cycle roots31* of a Component hierarchy change.32* <p>33* The core responsibility of a FocusTraversalPolicy is to provide algorithms34* determining the next and previous Components to focus when traversing35* forward or backward in a UI. Each FocusTraversalPolicy must also provide36* algorithms for determining the first, last, and default Components in a37* traversal cycle. First and last Components are used when normal forward and38* backward traversal, respectively, wraps. The default Component is the first39* to receive focus when traversing down into a new focus traversal cycle.40* A FocusTraversalPolicy can optionally provide an algorithm for determining41* a Window's initial Component. The initial Component is the first to receive42* focus when a Window is first made visible.43* <p>44* FocusTraversalPolicy takes into account <a45* href="doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus traversal46* policy providers</a>. When searching for first/last/next/previous Component,47* if a focus traversal policy provider is encountered, its focus traversal48* policy is used to perform the search operation.49* <p>50* Please see51* <a href="https://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html">52* How to Use the Focus Subsystem</a>,53* a section in <em>The Java Tutorial</em>, and the54* <a href="doc-files/FocusSpec.html">Focus Specification</a>55* for more information.56*57* @author David Mendenhall58*59* @see Container#setFocusTraversalPolicy60* @see Container#getFocusTraversalPolicy61* @see Container#setFocusCycleRoot62* @see Container#isFocusCycleRoot63* @see Container#setFocusTraversalPolicyProvider64* @see Container#isFocusTraversalPolicyProvider65* @see KeyboardFocusManager#setDefaultFocusTraversalPolicy66* @see KeyboardFocusManager#getDefaultFocusTraversalPolicy67* @since 1.468*/69public abstract class FocusTraversalPolicy {7071/**72* Constructs a {@code FocusTraversalPolicy}.73*/74protected FocusTraversalPolicy() {}7576/**77* Returns the Component that should receive the focus after aComponent.78* aContainer must be a focus cycle root of aComponent or a focus traversal79* policy provider.80*81* @param aContainer a focus cycle root of aComponent or focus traversal82* policy provider83* @param aComponent a (possibly indirect) child of aContainer, or84* aContainer itself85* @return the Component that should receive the focus after aComponent, or86* null if no suitable Component can be found87* @throws IllegalArgumentException if aContainer is not a focus cycle88* root of aComponent or a focus traversal policy provider, or if89* either aContainer or aComponent is null90*/91public abstract Component getComponentAfter(Container aContainer,92Component aComponent);9394/**95* Returns the Component that should receive the focus before aComponent.96* aContainer must be a focus cycle root of aComponent or a focus traversal97* policy provider.98*99* @param aContainer a focus cycle root of aComponent or focus traversal100* policy provider101* @param aComponent a (possibly indirect) child of aContainer, or102* aContainer itself103* @return the Component that should receive the focus before aComponent,104* or null if no suitable Component can be found105* @throws IllegalArgumentException if aContainer is not a focus cycle106* root of aComponent or a focus traversal policy provider, or if107* either aContainer or aComponent is null108*/109public abstract Component getComponentBefore(Container aContainer,110Component aComponent);111112/**113* Returns the first Component in the traversal cycle. This method is used114* to determine the next Component to focus when traversal wraps in the115* forward direction.116*117* @param aContainer the focus cycle root or focus traversal policy provider118* whose first Component is to be returned119* @return the first Component in the traversal cycle of aContainer,120* or null if no suitable Component can be found121* @throws IllegalArgumentException if aContainer is null122*/123public abstract Component getFirstComponent(Container aContainer);124125/**126* Returns the last Component in the traversal cycle. This method is used127* to determine the next Component to focus when traversal wraps in the128* reverse direction.129*130* @param aContainer the focus cycle root or focus traversal policy131* provider whose last Component is to be returned132* @return the last Component in the traversal cycle of aContainer,133* or null if no suitable Component can be found134* @throws IllegalArgumentException if aContainer is null135*/136public abstract Component getLastComponent(Container aContainer);137138/**139* Returns the default Component to focus. This Component will be the first140* to receive focus when traversing down into a new focus traversal cycle141* rooted at aContainer.142*143* @param aContainer the focus cycle root or focus traversal policy144* provider whose default Component is to be returned145* @return the default Component in the traversal cycle of aContainer,146* or null if no suitable Component can be found147* @throws IllegalArgumentException if aContainer is null148*/149public abstract Component getDefaultComponent(Container aContainer);150151/**152* Returns the Component that should receive the focus when a Window is153* made visible for the first time. Once the Window has been made visible154* by a call to {@code show()} or {@code setVisible(true)}, the155* initial Component will not be used again. Instead, if the Window loses156* and subsequently regains focus, or is made invisible or undisplayable157* and subsequently made visible and displayable, the Window's most158* recently focused Component will become the focus owner. The default159* implementation of this method returns the default Component.160*161* @param window the Window whose initial Component is to be returned162* @return the Component that should receive the focus when window is made163* visible for the first time, or null if no suitable Component can164* be found165* @see #getDefaultComponent166* @see Window#getMostRecentFocusOwner167* @throws IllegalArgumentException if window is null168*/169public Component getInitialComponent(Window window) {170if ( window == null ){171throw new IllegalArgumentException("window cannot be equal to null.");172}173Component def = getDefaultComponent(window);174if (def == null && window.isFocusableWindow()) {175def = window;176}177return def;178}179}180181182