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 40 public class ActionMap implements Serializable { 41 42 private transient ArrayTable arrayTable; 43 44 private ActionMap parent; 45 46 47 50 public ActionMap() { 51 } 52 53 58 public void setParent(ActionMap map) { 59 this.parent = map; 60 } 61 62 68 public ActionMap getParent() { 69 return parent; 70 } 71 72 79 public void put(Object key, Action action) { 80 if (key == null) { 81 return; 82 } 83 if (action == null) { 84 remove(key); 85 } 86 else { 87 if (arrayTable == null) { 88 arrayTable = new ArrayTable (); 89 } 90 arrayTable.put(key, action); 91 } 92 } 93 94 98 public Action get(Object key) { 99 Action value = (arrayTable == null) ? null : 100 (Action )arrayTable.get(key); 101 102 if (value == null) { 103 ActionMap parent = getParent(); 104 105 if (parent != null) { 106 return parent.get(key); 107 } 108 } 109 return value; 110 } 111 112 115 public void remove(Object key) { 116 if (arrayTable != null) { 117 arrayTable.remove(key); 118 } 119 } 120 121 124 public void clear() { 125 if (arrayTable != null) { 126 arrayTable.clear(); 127 } 128 } 129 130 133 public Object [] keys() { 134 if (arrayTable == null) { 135 return null; 136 } 137 return arrayTable.getKeys(null); 138 } 139 140 143 public int size() { 144 if (arrayTable == null) { 145 return 0; 146 } 147 return arrayTable.size(); 148 } 149 150 155 public Object [] allKeys() { 156 int count = size(); 157 ActionMap parent = getParent(); 158 159 if (count == 0) { 160 if (parent != null) { 161 return parent.allKeys(); 162 } 163 return keys(); 164 } 165 if (parent == null) { 166 return keys(); 167 } 168 Object [] keys = keys(); 169 Object [] pKeys = parent.allKeys(); 170 171 if (pKeys == null) { 172 return keys; 173 } 174 if (keys == null) { 175 return pKeys; 178 } 179 180 HashMap keyMap = new HashMap (); 181 int counter; 182 183 for (counter = keys.length - 1; counter >= 0; counter--) { 184 keyMap.put(keys[counter], keys[counter]); 185 } 186 for (counter = pKeys.length - 1; counter >= 0; counter--) { 187 keyMap.put(pKeys[counter], pKeys[counter]); 188 } 189 return keyMap.keySet().toArray(); 190 } 191 192 private void writeObject(ObjectOutputStream s) throws IOException { 193 s.defaultWriteObject(); 194 195 ArrayTable.writeArrayTable(s, arrayTable); 196 } 197 198 private void readObject(ObjectInputStream s) throws ClassNotFoundException , 199 IOException { 200 s.defaultReadObject(); 201 for (int counter = s.readInt() - 1; counter >= 0; counter--) { 202 put(s.readObject(), (Action )s.readObject()); 203 } 204 } 205 } 206 | Popular Tags |