Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/swing/DefaultFocusManager.java
38829 views
/*1* Copyright (c) 1997, 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 javax.swing;2526import java.awt.Component;27import java.awt.Container;28import java.awt.FocusTraversalPolicy;29import java.util.Comparator;303132/**33* This class has been obsoleted by the 1.4 focus APIs. While client code may34* still use this class, developers are strongly encouraged to use35* <code>java.awt.KeyboardFocusManager</code> and36* <code>java.awt.DefaultKeyboardFocusManager</code> instead.37* <p>38* Please see39* <a href="https://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html">40* How to Use the Focus Subsystem</a>,41* a section in <em>The Java Tutorial</em>, and the42* <a href="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>43* for more information.44*45* @author Arnaud Weber46* @author David Mendenhall47*/48public class DefaultFocusManager extends FocusManager {4950final FocusTraversalPolicy gluePolicy =51new LegacyGlueFocusTraversalPolicy(this);52private final FocusTraversalPolicy layoutPolicy =53new LegacyLayoutFocusTraversalPolicy(this);54private final LayoutComparator comparator =55new LayoutComparator();5657public DefaultFocusManager() {58setDefaultFocusTraversalPolicy(gluePolicy);59}6061public Component getComponentAfter(Container aContainer,62Component aComponent)63{64Container root = (aContainer.isFocusCycleRoot())65? aContainer66: aContainer.getFocusCycleRootAncestor();6768// Support for mixed 1.4/pre-1.4 focus APIs. If a particular root's69// traversal policy is non-legacy, then honor it.70if (root != null) {71FocusTraversalPolicy policy = root.getFocusTraversalPolicy();72if (policy != gluePolicy) {73return policy.getComponentAfter(root, aComponent);74}7576comparator.setComponentOrientation(root.getComponentOrientation());77return layoutPolicy.getComponentAfter(root, aComponent);78}7980return null;81}8283public Component getComponentBefore(Container aContainer,84Component aComponent)85{86Container root = (aContainer.isFocusCycleRoot())87? aContainer88: aContainer.getFocusCycleRootAncestor();8990// Support for mixed 1.4/pre-1.4 focus APIs. If a particular root's91// traversal policy is non-legacy, then honor it.92if (root != null) {93FocusTraversalPolicy policy = root.getFocusTraversalPolicy();94if (policy != gluePolicy) {95return policy.getComponentBefore(root, aComponent);96}9798comparator.setComponentOrientation(root.getComponentOrientation());99return layoutPolicy.getComponentBefore(root, aComponent);100}101102return null;103}104105public Component getFirstComponent(Container aContainer) {106Container root = (aContainer.isFocusCycleRoot())107? aContainer108: aContainer.getFocusCycleRootAncestor();109110// Support for mixed 1.4/pre-1.4 focus APIs. If a particular root's111// traversal policy is non-legacy, then honor it.112if (root != null) {113FocusTraversalPolicy policy = root.getFocusTraversalPolicy();114if (policy != gluePolicy) {115return policy.getFirstComponent(root);116}117118comparator.setComponentOrientation(root.getComponentOrientation());119return layoutPolicy.getFirstComponent(root);120}121122return null;123}124125public Component getLastComponent(Container aContainer) {126Container root = (aContainer.isFocusCycleRoot())127? aContainer128: aContainer.getFocusCycleRootAncestor();129130// Support for mixed 1.4/pre-1.4 focus APIs. If a particular root's131// traversal policy is non-legacy, then honor it.132if (root != null) {133FocusTraversalPolicy policy = root.getFocusTraversalPolicy();134if (policy != gluePolicy) {135return policy.getLastComponent(root);136}137138comparator.setComponentOrientation(root.getComponentOrientation());139return layoutPolicy.getLastComponent(root);140}141142return null;143}144145public boolean compareTabOrder(Component a, Component b) {146return (comparator.compare(a, b) < 0);147}148}149150final class LegacyLayoutFocusTraversalPolicy151extends LayoutFocusTraversalPolicy152{153LegacyLayoutFocusTraversalPolicy(DefaultFocusManager defaultFocusManager) {154super(new CompareTabOrderComparator(defaultFocusManager));155}156}157158final class CompareTabOrderComparator implements Comparator<Component> {159private final DefaultFocusManager defaultFocusManager;160161CompareTabOrderComparator(DefaultFocusManager defaultFocusManager) {162this.defaultFocusManager = defaultFocusManager;163}164165public int compare(Component o1, Component o2) {166if (o1 == o2) {167return 0;168}169return (defaultFocusManager.compareTabOrder(o1, o2)) ? -1 : 1;170}171}172173174