1 7 package javax.swing; 8 9 import java.awt.Component ; 10 import java.awt.Container ; 11 import java.awt.ComponentOrientation ; 12 import java.util.Comparator ; 13 import java.io.*; 14 15 16 32 public class LayoutFocusTraversalPolicy extends SortingFocusTraversalPolicy 33 implements Serializable 34 { 35 private static final SwingDefaultFocusTraversalPolicy fitnessTestPolicy = 38 new SwingDefaultFocusTraversalPolicy(); 39 40 43 public LayoutFocusTraversalPolicy() { 44 super(new LayoutComparator ()); 45 } 46 47 51 LayoutFocusTraversalPolicy(Comparator c) { 52 super(c); 53 } 54 55 77 public Component getComponentAfter(Container aContainer, 78 Component aComponent) { 79 if (aContainer == null || aComponent == null) { 80 throw new IllegalArgumentException ("aContainer and aComponent cannot be null"); 81 } 82 Comparator comparator = getComparator(); 83 if (comparator instanceof LayoutComparator ) { 84 ((LayoutComparator )comparator). 85 setComponentOrientation(aContainer. 86 getComponentOrientation()); 87 } 88 return super.getComponentAfter(aContainer, aComponent); 89 } 90 91 113 public Component getComponentBefore(Container aContainer, 114 Component aComponent) { 115 if (aContainer == null || aComponent == null) { 116 throw new IllegalArgumentException ("aContainer and aComponent cannot be null"); 117 } 118 Comparator comparator = getComparator(); 119 if (comparator instanceof LayoutComparator ) { 120 ((LayoutComparator )comparator). 121 setComponentOrientation(aContainer. 122 getComponentOrientation()); 123 } 124 return super.getComponentBefore(aContainer, aComponent); 125 } 126 127 138 public Component getFirstComponent(Container aContainer) { 139 if (aContainer == null) { 140 throw new IllegalArgumentException ("aContainer cannot be null"); 141 } 142 Comparator comparator = getComparator(); 143 if (comparator instanceof LayoutComparator ) { 144 ((LayoutComparator )comparator). 145 setComponentOrientation(aContainer. 146 getComponentOrientation()); 147 } 148 return super.getFirstComponent(aContainer); 149 } 150 151 162 public Component getLastComponent(Container aContainer) { 163 if (aContainer == null) { 164 throw new IllegalArgumentException ("aContainer cannot be null"); 165 } 166 Comparator comparator = getComparator(); 167 if (comparator instanceof LayoutComparator ) { 168 ((LayoutComparator )comparator). 169 setComponentOrientation(aContainer. 170 getComponentOrientation()); 171 } 172 return super.getLastComponent(aContainer); 173 } 174 175 209 protected boolean accept(Component aComponent) { 210 if (!super.accept(aComponent)) { 211 return false; 212 } else if (aComponent instanceof JTable ) { 213 return true; 216 } else if (aComponent instanceof JComboBox ) { 217 JComboBox box = (JComboBox )aComponent; 218 return box.getUI().isFocusTraversable(box); 219 } else if (aComponent instanceof JComponent ) { 220 JComponent jComponent = (JComponent )aComponent; 221 InputMap inputMap = jComponent.getInputMap(JComponent.WHEN_FOCUSED, 222 false); 223 while (inputMap != null && inputMap.size() == 0) { 224 inputMap = inputMap.getParent(); 225 } 226 if (inputMap != null) { 227 return true; 228 } 229 } 233 return fitnessTestPolicy.accept(aComponent); 234 } 235 236 private void writeObject(ObjectOutputStream out) throws IOException { 237 out.writeObject(getComparator()); 238 out.writeBoolean(getImplicitDownCycleTraversal()); 239 } 240 private void readObject(ObjectInputStream in) 241 throws IOException, ClassNotFoundException 242 { 243 setComparator((Comparator )in.readObject()); 244 setImplicitDownCycleTraversal(in.readBoolean()); 245 } 246 } 247 248 class SwingDefaultFocusTraversalPolicy 251 extends java.awt.DefaultFocusTraversalPolicy 252 { 253 public boolean accept(Component aComponent) { 254 return super.accept(aComponent); 255 } 256 } 257 | Popular Tags |