1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
/* * @(#)DefaultFocusManager.java 1.30 05/11/17 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package javax.swing; import java.awt.Component; import java.awt.Container; import java.awt.FocusTraversalPolicy; import java.util.Comparator; /** * This class has been obsoleted by the 1.4 focus APIs. While client code may * still use this class, developers are strongly encouraged to use * <code>java.awt.KeyboardFocusManager</code> and * <code>java.awt.DefaultKeyboardFocusManager</code> instead. * <p> * Please see * <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html"> * How to Use the Focus Subsystem</a>, * a section in <em>The Java Tutorial</em>, and the * <a href="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a> * for more information. * * @version 1.30, 11/17/05 * @author Arnaud Weber * @author David Mendenhall */ public class DefaultFocusManager extends FocusManager { final FocusTraversalPolicy gluePolicy = new LegacyGlueFocusTraversalPolicy(this); private final FocusTraversalPolicy layoutPolicy = new LegacyLayoutFocusTraversalPolicy(this); private final LayoutComparator comparator = new LayoutComparator(); public DefaultFocusManager() { setDefaultFocusTraversalPolicy(gluePolicy); } public Component getComponentAfter(Container aContainer, Component aComponent) { Container root = (aContainer.isFocusCycleRoot()) ? aContainer : aContainer.getFocusCycleRootAncestor(); // Support for mixed 1.4/pre-1.4 focus APIs. If a particular root's // traversal policy is non-legacy, then honor it. if (root != null) { FocusTraversalPolicy policy = root.getFocusTraversalPolicy(); if (policy != gluePolicy) { return policy.getComponentAfter(root, aComponent); } comparator.setComponentOrientation(root.getComponentOrientation()); return layoutPolicy.getComponentAfter(root, aComponent); } return null; } public Component getComponentBefore(Container aContainer, Component aComponent) { Container root = (aContainer.isFocusCycleRoot()) ? aContainer : aContainer.getFocusCycleRootAncestor(); // Support for mixed 1.4/pre-1.4 focus APIs. If a particular root's // traversal policy is non-legacy, then honor it. if (root != null) { FocusTraversalPolicy policy = root.getFocusTraversalPolicy(); if (policy != gluePolicy) { return policy.getComponentBefore(root, aComponent); } comparator.setComponentOrientation(root.getComponentOrientation()); return layoutPolicy.getComponentBefore(root, aComponent); } return null; } public Component getFirstComponent(Container aContainer) { Container root = (aContainer.isFocusCycleRoot()) ? aContainer : aContainer.getFocusCycleRootAncestor(); // Support for mixed 1.4/pre-1.4 focus APIs. If a particular root's // traversal policy is non-legacy, then honor it. if (root != null) { FocusTraversalPolicy policy = root.getFocusTraversalPolicy(); if (policy != gluePolicy) { return policy.getFirstComponent(root); } comparator.setComponentOrientation(root.getComponentOrientation()); return layoutPolicy.getFirstComponent(root); } return null; } public Component getLastComponent(Container aContainer) { Container root = (aContainer.isFocusCycleRoot()) ? aContainer : aContainer.getFocusCycleRootAncestor(); // Support for mixed 1.4/pre-1.4 focus APIs. If a particular root's // traversal policy is non-legacy, then honor it. if (root != null) { FocusTraversalPolicy policy = root.getFocusTraversalPolicy(); if (policy != gluePolicy) { return policy.getLastComponent(root); } comparator.setComponentOrientation(root.getComponentOrientation()); return layoutPolicy.getLastComponent(root); } return null; } public boolean compareTabOrder(Component a, Component b) { return (comparator.compare(a, b) < 0); } } final class LegacyLayoutFocusTraversalPolicy extends LayoutFocusTraversalPolicy { LegacyLayoutFocusTraversalPolicy(DefaultFocusManager defaultFocusManager) { super(new CompareTabOrderComparator(defaultFocusManager)); } } final class CompareTabOrderComparator implements Comparator { private final DefaultFocusManager defaultFocusManager; CompareTabOrderComparator(DefaultFocusManager defaultFocusManager) { this.defaultFocusManager = defaultFocusManager; } public int compare(Object o1, Object o2) { if (o1 == o2) { return 0; } return (defaultFocusManager.compareTabOrder((Component)o1, (Component)o2)) ? -1 : 1; } }