Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/awt/FocusTraversalPolicy.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;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="../../java/awt/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* Returns the Component that should receive the focus after aComponent.73* aContainer must be a focus cycle root of aComponent or a focus traversal74* policy provider.75*76* @param aContainer a focus cycle root of aComponent or focus traversal77* policy provider78* @param aComponent a (possibly indirect) child of aContainer, or79* aContainer itself80* @return the Component that should receive the focus after aComponent, or81* null if no suitable Component can be found82* @throws IllegalArgumentException if aContainer is not a focus cycle83* root of aComponent or a focus traversal policy provider, or if84* either aContainer or aComponent is null85*/86public abstract Component getComponentAfter(Container aContainer,87Component aComponent);8889/**90* Returns the Component that should receive the focus before aComponent.91* aContainer must be a focus cycle root of aComponent or a focus traversal92* policy provider.93*94* @param aContainer a focus cycle root of aComponent or focus traversal95* policy provider96* @param aComponent a (possibly indirect) child of aContainer, or97* aContainer itself98* @return the Component that should receive the focus before aComponent,99* or null if no suitable Component can be found100* @throws IllegalArgumentException if aContainer is not a focus cycle101* root of aComponent or a focus traversal policy provider, or if102* either aContainer or aComponent is null103*/104public abstract Component getComponentBefore(Container aContainer,105Component aComponent);106107/**108* Returns the first Component in the traversal cycle. This method is used109* to determine the next Component to focus when traversal wraps in the110* forward direction.111*112* @param aContainer the focus cycle root or focus traversal policy provider113* whose first Component is to be returned114* @return the first Component in the traversal cycle of aContainer,115* or null if no suitable Component can be found116* @throws IllegalArgumentException if aContainer is null117*/118public abstract Component getFirstComponent(Container aContainer);119120/**121* Returns the last Component in the traversal cycle. This method is used122* to determine the next Component to focus when traversal wraps in the123* reverse direction.124*125* @param aContainer the focus cycle root or focus traversal policy126* provider whose last Component is to be returned127* @return the last Component in the traversal cycle of aContainer,128* or null if no suitable Component can be found129* @throws IllegalArgumentException if aContainer is null130*/131public abstract Component getLastComponent(Container aContainer);132133/**134* Returns the default Component to focus. This Component will be the first135* to receive focus when traversing down into a new focus traversal cycle136* rooted at aContainer.137*138* @param aContainer the focus cycle root or focus traversal policy139* provider whose default Component is to be returned140* @return the default Component in the traversal cycle of aContainer,141* or null if no suitable Component can be found142* @throws IllegalArgumentException if aContainer is null143*/144public abstract Component getDefaultComponent(Container aContainer);145146/**147* Returns the Component that should receive the focus when a Window is148* made visible for the first time. Once the Window has been made visible149* by a call to <code>show()</code> or <code>setVisible(true)</code>, the150* initial Component will not be used again. Instead, if the Window loses151* and subsequently regains focus, or is made invisible or undisplayable152* and subsequently made visible and displayable, the Window's most153* recently focused Component will become the focus owner. The default154* implementation of this method returns the default Component.155*156* @param window the Window whose initial Component is to be returned157* @return the Component that should receive the focus when window is made158* visible for the first time, or null if no suitable Component can159* be found160* @see #getDefaultComponent161* @see Window#getMostRecentFocusOwner162* @throws IllegalArgumentException if window is null163*/164public Component getInitialComponent(Window window) {165if ( window == null ){166throw new IllegalArgumentException("window cannot be equal to null.");167}168Component def = getDefaultComponent(window);169if (def == null && window.isFocusableWindow()) {170def = window;171}172return def;173}174}175176177