1 7 package javax.swing; 8 9 import java.io.IOException ; 10 import java.io.ObjectInputStream ; 11 import java.io.ObjectOutputStream ; 12 import java.io.Serializable ; 13 import java.util.HashMap ; 14 import java.util.Set ; 15 16 38 public class InputMap implements Serializable { 39 40 private transient ArrayTable arrayTable; 41 42 private InputMap parent; 43 44 45 48 public InputMap() { 49 } 50 51 56 public void setParent(InputMap map) { 57 this.parent = map; 58 } 59 60 66 public InputMap getParent() { 67 return parent; 68 } 69 70 75 public void put(KeyStroke keyStroke, Object actionMapKey) { 76 if (keyStroke == null) { 77 return; 78 } 79 if (actionMapKey == null) { 80 remove(keyStroke); 81 } 82 else { 83 if (arrayTable == null) { 84 arrayTable = new ArrayTable (); 85 } 86 arrayTable.put(keyStroke, actionMapKey); 87 } 88 } 89 90 94 public Object get(KeyStroke keyStroke) { 95 if (arrayTable == null) { 96 InputMap parent = getParent(); 97 98 if (parent != null) { 99 return parent.get(keyStroke); 100 } 101 return null; 102 } 103 Object value = arrayTable.get(keyStroke); 104 105 if (value == null) { 106 InputMap parent = getParent(); 107 108 if (parent != null) { 109 return parent.get(keyStroke); 110 } 111 } 112 return value; 113 } 114 115 119 public void remove(KeyStroke key) { 120 if (arrayTable != null) { 121 arrayTable.remove(key); 122 } 123 } 124 125 128 public void clear() { 129 if (arrayTable != null) { 130 arrayTable.clear(); 131 } 132 } 133 134 137 public KeyStroke [] keys() { 138 if (arrayTable == null) { 139 return null; 140 } 141 KeyStroke [] keys = new KeyStroke [arrayTable.size()]; 142 arrayTable.getKeys(keys); 143 return keys; 144 } 145 146 149 public int size() { 150 if (arrayTable == null) { 151 return 0; 152 } 153 return arrayTable.size(); 154 } 155 156 161 public KeyStroke [] allKeys() { 162 int count = size(); 163 InputMap parent = getParent(); 164 165 if (count == 0) { 166 if (parent != null) { 167 return parent.allKeys(); 168 } 169 return keys(); 170 } 171 if (parent == null) { 172 return keys(); 173 } 174 KeyStroke [] keys = keys(); 175 KeyStroke [] pKeys = parent.allKeys(); 176 177 if (pKeys == null) { 178 return keys; 179 } 180 if (keys == null) { 181 return pKeys; 184 } 185 186 HashMap keyMap = new HashMap (); 187 int counter; 188 189 for (counter = keys.length - 1; counter >= 0; counter--) { 190 keyMap.put(keys[counter], keys[counter]); 191 } 192 for (counter = pKeys.length - 1; counter >= 0; counter--) { 193 keyMap.put(pKeys[counter], pKeys[counter]); 194 } 195 196 KeyStroke [] allKeys = new KeyStroke [keyMap.size()]; 197 198 return (KeyStroke [])keyMap.keySet().toArray(allKeys); 199 } 200 201 private void writeObject(ObjectOutputStream s) throws IOException { 202 s.defaultWriteObject(); 203 204 ArrayTable.writeArrayTable(s, arrayTable); 205 } 206 207 private void readObject(ObjectInputStream s) throws ClassNotFoundException , 208 IOException { 209 s.defaultReadObject(); 210 for (int counter = s.readInt() - 1; counter >= 0; counter--) { 211 put((KeyStroke )s.readObject(), s.readObject()); 212 } 213 } 214 } 215 | Popular Tags |