1 7 package javax.swing; 8 9 import java.awt.Component ; 10 import java.awt.Container ; 11 import java.awt.FocusTraversalPolicy ; 12 import java.util.Comparator ; 13 14 15 32 public class DefaultFocusManager extends FocusManager { 33 34 final FocusTraversalPolicy gluePolicy = 35 new LegacyGlueFocusTraversalPolicy (this); 36 private final FocusTraversalPolicy layoutPolicy = 37 new LegacyLayoutFocusTraversalPolicy(this); 38 private final LayoutComparator comparator = 39 new LayoutComparator (); 40 41 public DefaultFocusManager() { 42 setDefaultFocusTraversalPolicy(gluePolicy); 43 } 44 45 public Component getComponentAfter(Container aContainer, 46 Component aComponent) 47 { 48 Container root = (aContainer.isFocusCycleRoot()) 49 ? aContainer 50 : aContainer.getFocusCycleRootAncestor(); 51 52 if (root != null) { 55 FocusTraversalPolicy policy = root.getFocusTraversalPolicy(); 56 if (policy != gluePolicy) { 57 return policy.getComponentAfter(root, aComponent); 58 } 59 60 comparator.setComponentOrientation(root.getComponentOrientation()); 61 return layoutPolicy.getComponentAfter(root, aComponent); 62 } 63 64 return null; 65 } 66 67 public Component getComponentBefore(Container aContainer, 68 Component aComponent) 69 { 70 Container root = (aContainer.isFocusCycleRoot()) 71 ? aContainer 72 : aContainer.getFocusCycleRootAncestor(); 73 74 if (root != null) { 77 FocusTraversalPolicy policy = root.getFocusTraversalPolicy(); 78 if (policy != gluePolicy) { 79 return policy.getComponentBefore(root, aComponent); 80 } 81 82 comparator.setComponentOrientation(root.getComponentOrientation()); 83 return layoutPolicy.getComponentBefore(root, aComponent); 84 } 85 86 return null; 87 } 88 89 public Component getFirstComponent(Container aContainer) { 90 Container root = (aContainer.isFocusCycleRoot()) 91 ? aContainer 92 : aContainer.getFocusCycleRootAncestor(); 93 94 if (root != null) { 97 FocusTraversalPolicy policy = root.getFocusTraversalPolicy(); 98 if (policy != gluePolicy) { 99 return policy.getFirstComponent(root); 100 } 101 102 comparator.setComponentOrientation(root.getComponentOrientation()); 103 return layoutPolicy.getFirstComponent(root); 104 } 105 106 return null; 107 } 108 109 public Component getLastComponent(Container aContainer) { 110 Container root = (aContainer.isFocusCycleRoot()) 111 ? aContainer 112 : aContainer.getFocusCycleRootAncestor(); 113 114 if (root != null) { 117 FocusTraversalPolicy policy = root.getFocusTraversalPolicy(); 118 if (policy != gluePolicy) { 119 return policy.getLastComponent(root); 120 } 121 122 comparator.setComponentOrientation(root.getComponentOrientation()); 123 return layoutPolicy.getLastComponent(root); 124 } 125 126 return null; 127 } 128 129 public boolean compareTabOrder(Component a, Component b) { 130 return (comparator.compare(a, b) < 0); 131 } 132 } 133 134 final class LegacyLayoutFocusTraversalPolicy 135 extends LayoutFocusTraversalPolicy 136 { 137 LegacyLayoutFocusTraversalPolicy(DefaultFocusManager defaultFocusManager) { 138 super(new CompareTabOrderComparator(defaultFocusManager)); 139 } 140 } 141 142 final class CompareTabOrderComparator implements Comparator { 143 private final DefaultFocusManager defaultFocusManager; 144 145 CompareTabOrderComparator(DefaultFocusManager defaultFocusManager) { 146 this.defaultFocusManager = defaultFocusManager; 147 } 148 149 public int compare(Object o1, Object o2) { 150 if (o1 == o2) { 151 return 0; 152 } 153 return (defaultFocusManager.compareTabOrder((Component )o1, 154 (Component )o2)) ? -1 : 1; 155 } 156 } 157 | Popular Tags |