1 7 package javax.swing; 8 9 import java.awt.FocusTraversalPolicy ; 10 import java.awt.Component ; 11 import java.awt.Container ; 12 import java.awt.Window ; 13 import java.util.HashMap ; 14 import java.util.HashSet ; 15 import java.io.*; 16 17 18 28 final class LegacyGlueFocusTraversalPolicy extends FocusTraversalPolicy 29 implements Serializable 30 { 31 private transient FocusTraversalPolicy delegatePolicy; 32 private transient DefaultFocusManager delegateManager; 33 34 private HashMap forwardMap = new HashMap (), 35 backwardMap = new HashMap (); 36 37 LegacyGlueFocusTraversalPolicy(FocusTraversalPolicy delegatePolicy) { 38 this.delegatePolicy = delegatePolicy; 39 } 40 LegacyGlueFocusTraversalPolicy(DefaultFocusManager delegateManager) { 41 this.delegateManager = delegateManager; 42 } 43 44 void setNextFocusableComponent(Component left, Component right) { 45 forwardMap.put(left, right); 46 backwardMap.put(right, left); 47 } 48 void unsetNextFocusableComponent(Component left, Component right) { 49 forwardMap.remove(left); 50 backwardMap.remove(right); 51 } 52 53 public Component getComponentAfter(Container focusCycleRoot, 54 Component aComponent) { 55 Component hardCoded = aComponent, prevHardCoded; 56 HashSet sanity = new HashSet (); 57 58 do { 59 prevHardCoded = hardCoded; 60 hardCoded = (Component )forwardMap.get(hardCoded); 61 if (hardCoded == null) { 62 if (delegatePolicy != null && 63 prevHardCoded.isFocusCycleRoot(focusCycleRoot)) { 64 return delegatePolicy.getComponentAfter(focusCycleRoot, 65 prevHardCoded); 66 } else if (delegateManager != null) { 67 return delegateManager. 68 getComponentAfter(focusCycleRoot, aComponent); 69 } else { 70 return null; 71 } 72 } 73 if (sanity.contains(hardCoded)) { 74 return null; 76 } 77 sanity.add(hardCoded); 78 } while (!accept(hardCoded)); 79 80 return hardCoded; 81 } 82 public Component getComponentBefore(Container focusCycleRoot, 83 Component aComponent) { 84 Component hardCoded = aComponent, prevHardCoded; 85 HashSet sanity = new HashSet (); 86 87 do { 88 prevHardCoded = hardCoded; 89 hardCoded = (Component )backwardMap.get(hardCoded); 90 if (hardCoded == null) { 91 if (delegatePolicy != null && 92 prevHardCoded.isFocusCycleRoot(focusCycleRoot)) { 93 return delegatePolicy.getComponentBefore(focusCycleRoot, 94 prevHardCoded); 95 } else if (delegateManager != null) { 96 return delegateManager. 97 getComponentBefore(focusCycleRoot, aComponent); 98 } else { 99 return null; 100 } 101 } 102 if (sanity.contains(hardCoded)) { 103 return null; 105 } 106 sanity.add(hardCoded); 107 } while (!accept(hardCoded)); 108 109 return hardCoded; 110 } 111 public Component getFirstComponent(Container focusCycleRoot) { 112 if (delegatePolicy != null) { 113 return delegatePolicy.getFirstComponent(focusCycleRoot); 114 } else if (delegateManager != null) { 115 return delegateManager.getFirstComponent(focusCycleRoot); 116 } else { 117 return null; 118 } 119 } 120 public Component getLastComponent(Container focusCycleRoot) { 121 if (delegatePolicy != null) { 122 return delegatePolicy.getLastComponent(focusCycleRoot); 123 } else if (delegateManager != null) { 124 return delegateManager.getLastComponent(focusCycleRoot); 125 } else { 126 return null; 127 } 128 } 129 public Component getDefaultComponent(Container focusCycleRoot) { 130 if (delegatePolicy != null) { 131 return delegatePolicy.getDefaultComponent(focusCycleRoot); 132 } else { 133 return getFirstComponent(focusCycleRoot); 134 } 135 } 136 private boolean accept(Component aComponent) { 137 if (!(aComponent.isVisible() && aComponent.isDisplayable() && 138 aComponent.isFocusable() && aComponent.isEnabled())) { 139 return false; 140 } 141 142 if (!(aComponent instanceof Window )) { 146 for (Container enableTest = aComponent.getParent(); 147 enableTest != null; 148 enableTest = enableTest.getParent()) 149 { 150 if (!(enableTest.isEnabled() || enableTest.isLightweight())) { 151 return false; 152 } 153 if (enableTest instanceof Window ) { 154 break; 155 } 156 } 157 } 158 159 return true; 160 } 161 private void writeObject(ObjectOutputStream out) throws IOException { 162 out.defaultWriteObject(); 163 164 if (delegatePolicy instanceof Serializable) { 165 out.writeObject(delegatePolicy); 166 } else { 167 out.writeObject(null); 168 } 169 170 if (delegateManager instanceof Serializable) { 171 out.writeObject(delegateManager); 172 } else { 173 out.writeObject(null); 174 } 175 } 176 private void readObject(ObjectInputStream in) 177 throws IOException, ClassNotFoundException 178 { 179 in.defaultReadObject(); 180 delegatePolicy = (FocusTraversalPolicy )in.readObject(); 181 delegateManager = (DefaultFocusManager )in.readObject(); 182 } 183 } 184 | Popular Tags |