Path: blob/master/src/java.desktop/share/classes/javax/swing/DefaultFocusManager.java
41153 views
/*1* Copyright (c) 1997, 2015, 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* @since 1.248*/49@SuppressWarnings("serial") // Obsolete class50public class DefaultFocusManager extends FocusManager {5152final FocusTraversalPolicy gluePolicy =53new LegacyGlueFocusTraversalPolicy(this);54private final FocusTraversalPolicy layoutPolicy =55new LegacyLayoutFocusTraversalPolicy(this);56private final LayoutComparator comparator =57new LayoutComparator();5859/**60* Constructs a {@code DefaultFocusManager}.61*/62public DefaultFocusManager() {63setDefaultFocusTraversalPolicy(gluePolicy);64}6566/**67* Returns the component after.68* @return the component after69* @param aContainer a container70* @param aComponent a component71*/72public Component getComponentAfter(Container aContainer,73Component aComponent)74{75Container root = (aContainer.isFocusCycleRoot())76? aContainer77: aContainer.getFocusCycleRootAncestor();7879// Support for mixed 1.4/pre-1.4 focus APIs. If a particular root's80// traversal policy is non-legacy, then honor it.81if (root != null) {82FocusTraversalPolicy policy = root.getFocusTraversalPolicy();83if (policy != gluePolicy) {84return policy.getComponentAfter(root, aComponent);85}8687comparator.setComponentOrientation(root.getComponentOrientation());88return layoutPolicy.getComponentAfter(root, aComponent);89}9091return null;92}9394/**95* Returns the component before.96* @return the component before97* @param aContainer a container98* @param aComponent a component99*/100public Component getComponentBefore(Container aContainer,101Component aComponent)102{103Container root = (aContainer.isFocusCycleRoot())104? aContainer105: aContainer.getFocusCycleRootAncestor();106107// Support for mixed 1.4/pre-1.4 focus APIs. If a particular root's108// traversal policy is non-legacy, then honor it.109if (root != null) {110FocusTraversalPolicy policy = root.getFocusTraversalPolicy();111if (policy != gluePolicy) {112return policy.getComponentBefore(root, aComponent);113}114115comparator.setComponentOrientation(root.getComponentOrientation());116return layoutPolicy.getComponentBefore(root, aComponent);117}118119return null;120}121122/**123* Returns the first component.124* @return the first component125* @param aContainer a container126*/127public Component getFirstComponent(Container aContainer) {128Container root = (aContainer.isFocusCycleRoot())129? aContainer130: aContainer.getFocusCycleRootAncestor();131132// Support for mixed 1.4/pre-1.4 focus APIs. If a particular root's133// traversal policy is non-legacy, then honor it.134if (root != null) {135FocusTraversalPolicy policy = root.getFocusTraversalPolicy();136if (policy != gluePolicy) {137return policy.getFirstComponent(root);138}139140comparator.setComponentOrientation(root.getComponentOrientation());141return layoutPolicy.getFirstComponent(root);142}143144return null;145}146147/**148* Returns the last component.149* @return the last component150* @param aContainer a container151*/152public Component getLastComponent(Container aContainer) {153Container root = (aContainer.isFocusCycleRoot())154? aContainer155: aContainer.getFocusCycleRootAncestor();156157// Support for mixed 1.4/pre-1.4 focus APIs. If a particular root's158// traversal policy is non-legacy, then honor it.159if (root != null) {160FocusTraversalPolicy policy = root.getFocusTraversalPolicy();161if (policy != gluePolicy) {162return policy.getLastComponent(root);163}164165comparator.setComponentOrientation(root.getComponentOrientation());166return layoutPolicy.getLastComponent(root);167}168169return null;170}171172/**173* Compares the components by their focus traversal cycle order.174* @param a the first component175* @param b the second component176* @return a comparison of the components by their focus traversal cycle order177*/178public boolean compareTabOrder(Component a, Component b) {179return (comparator.compare(a, b) < 0);180}181}182183@SuppressWarnings("serial") // JDK-implementation class184final class LegacyLayoutFocusTraversalPolicy185extends LayoutFocusTraversalPolicy186{187LegacyLayoutFocusTraversalPolicy(DefaultFocusManager defaultFocusManager) {188super(new CompareTabOrderComparator(defaultFocusManager));189}190}191192final class CompareTabOrderComparator implements Comparator<Component> {193private final DefaultFocusManager defaultFocusManager;194195CompareTabOrderComparator(DefaultFocusManager defaultFocusManager) {196this.defaultFocusManager = defaultFocusManager;197}198199public int compare(Component o1, Component o2) {200if (o1 == o2) {201return 0;202}203return (defaultFocusManager.compareTabOrder(o1, o2)) ? -1 : 1;204}205}206207208