author | mduigou |
Tue, 05 Nov 2013 19:44:41 -0800 | |
changeset 21615 | 0231a565a5b7 |
parent 11268 | f0e59c4852de |
child 23010 | 6dadb192ad81 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
package javax.swing; |
|
26 |
||
27 |
import java.io.IOException; |
|
28 |
import java.io.ObjectInputStream; |
|
29 |
import java.io.ObjectOutputStream; |
|
30 |
import java.io.Serializable; |
|
31 |
import java.util.HashMap; |
|
32 |
import java.util.Set; |
|
33 |
||
34 |
/** |
|
35 |
* <code>ActionMap</code> provides mappings from |
|
36 |
* <code>Object</code>s |
|
37 |
* (called <em>keys</em> or <em><code>Action</code> names</em>) |
|
38 |
* to <code>Action</code>s. |
|
39 |
* An <code>ActionMap</code> is usually used with an <code>InputMap</code> |
|
40 |
* to locate a particular action |
|
41 |
* when a key is pressed. As with <code>InputMap</code>, |
|
42 |
* an <code>ActionMap</code> can have a parent |
|
43 |
* that is searched for keys not defined in the <code>ActionMap</code>. |
|
44 |
* <p>As with <code>InputMap</code> if you create a cycle, eg: |
|
45 |
* <pre> |
|
46 |
* ActionMap am = new ActionMap(); |
|
47 |
* ActionMap bm = new ActionMap(): |
|
48 |
* am.setParent(bm); |
|
49 |
* bm.setParent(am); |
|
50 |
* </pre> |
|
51 |
* some of the methods will cause a StackOverflowError to be thrown. |
|
52 |
* |
|
53 |
* @see InputMap |
|
54 |
* |
|
55 |
* @author Scott Violet |
|
56 |
* @since 1.3 |
|
57 |
*/ |
|
11268 | 58 |
@SuppressWarnings("serial") |
2 | 59 |
public class ActionMap implements Serializable { |
60 |
/** Handles the mapping between Action name and Action. */ |
|
61 |
private transient ArrayTable arrayTable; |
|
62 |
/** Parent that handles any bindings we don't contain. */ |
|
63 |
private ActionMap parent; |
|
64 |
||
65 |
||
66 |
/** |
|
67 |
* Creates an <code>ActionMap</code> with no parent and no mappings. |
|
68 |
*/ |
|
69 |
public ActionMap() { |
|
70 |
} |
|
71 |
||
72 |
/** |
|
73 |
* Sets this <code>ActionMap</code>'s parent. |
|
74 |
* |
|
75 |
* @param map the <code>ActionMap</code> that is the parent of this one |
|
76 |
*/ |
|
77 |
public void setParent(ActionMap map) { |
|
78 |
this.parent = map; |
|
79 |
} |
|
80 |
||
81 |
/** |
|
82 |
* Returns this <code>ActionMap</code>'s parent. |
|
83 |
* |
|
84 |
* @return the <code>ActionMap</code> that is the parent of this one, |
|
85 |
* or null if this <code>ActionMap</code> has no parent |
|
86 |
*/ |
|
87 |
public ActionMap getParent() { |
|
88 |
return parent; |
|
89 |
} |
|
90 |
||
91 |
/** |
|
92 |
* Adds a binding for <code>key</code> to <code>action</code>. |
|
93 |
* If <code>action</code> is null, this removes the current binding |
|
94 |
* for <code>key</code>. |
|
95 |
* <p>In most instances, <code>key</code> will be |
|
96 |
* <code>action.getValue(NAME)</code>. |
|
97 |
*/ |
|
98 |
public void put(Object key, Action action) { |
|
99 |
if (key == null) { |
|
100 |
return; |
|
101 |
} |
|
102 |
if (action == null) { |
|
103 |
remove(key); |
|
104 |
} |
|
105 |
else { |
|
106 |
if (arrayTable == null) { |
|
107 |
arrayTable = new ArrayTable(); |
|
108 |
} |
|
109 |
arrayTable.put(key, action); |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
/** |
|
114 |
* Returns the binding for <code>key</code>, messaging the |
|
115 |
* parent <code>ActionMap</code> if the binding is not locally defined. |
|
116 |
*/ |
|
117 |
public Action get(Object key) { |
|
118 |
Action value = (arrayTable == null) ? null : |
|
119 |
(Action)arrayTable.get(key); |
|
120 |
||
121 |
if (value == null) { |
|
122 |
ActionMap parent = getParent(); |
|
123 |
||
124 |
if (parent != null) { |
|
125 |
return parent.get(key); |
|
126 |
} |
|
127 |
} |
|
128 |
return value; |
|
129 |
} |
|
130 |
||
131 |
/** |
|
132 |
* Removes the binding for <code>key</code> from this <code>ActionMap</code>. |
|
133 |
*/ |
|
134 |
public void remove(Object key) { |
|
135 |
if (arrayTable != null) { |
|
136 |
arrayTable.remove(key); |
|
137 |
} |
|
138 |
} |
|
139 |
||
140 |
/** |
|
141 |
* Removes all the mappings from this <code>ActionMap</code>. |
|
142 |
*/ |
|
143 |
public void clear() { |
|
144 |
if (arrayTable != null) { |
|
145 |
arrayTable.clear(); |
|
146 |
} |
|
147 |
} |
|
148 |
||
149 |
/** |
|
150 |
* Returns the <code>Action</code> names that are bound in this <code>ActionMap</code>. |
|
151 |
*/ |
|
152 |
public Object[] keys() { |
|
153 |
if (arrayTable == null) { |
|
154 |
return null; |
|
155 |
} |
|
156 |
return arrayTable.getKeys(null); |
|
157 |
} |
|
158 |
||
159 |
/** |
|
160 |
* Returns the number of bindings in this {@code ActionMap}. |
|
161 |
* |
|
162 |
* @return the number of bindings in this {@code ActionMap} |
|
163 |
*/ |
|
164 |
public int size() { |
|
165 |
if (arrayTable == null) { |
|
166 |
return 0; |
|
167 |
} |
|
168 |
return arrayTable.size(); |
|
169 |
} |
|
170 |
||
171 |
/** |
|
172 |
* Returns an array of the keys defined in this <code>ActionMap</code> and |
|
173 |
* its parent. This method differs from <code>keys()</code> in that |
|
174 |
* this method includes the keys defined in the parent. |
|
175 |
*/ |
|
176 |
public Object[] allKeys() { |
|
177 |
int count = size(); |
|
178 |
ActionMap parent = getParent(); |
|
179 |
||
180 |
if (count == 0) { |
|
181 |
if (parent != null) { |
|
182 |
return parent.allKeys(); |
|
183 |
} |
|
184 |
return keys(); |
|
185 |
} |
|
186 |
if (parent == null) { |
|
187 |
return keys(); |
|
188 |
} |
|
189 |
Object[] keys = keys(); |
|
190 |
Object[] pKeys = parent.allKeys(); |
|
191 |
||
192 |
if (pKeys == null) { |
|
193 |
return keys; |
|
194 |
} |
|
195 |
if (keys == null) { |
|
196 |
// Should only happen if size() != keys.length, which should only |
|
197 |
// happen if mutated from multiple threads (or a bogus subclass). |
|
198 |
return pKeys; |
|
199 |
} |
|
200 |
||
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
2
diff
changeset
|
201 |
HashMap<Object, Object> keyMap = new HashMap<Object, Object>(); |
2 | 202 |
int counter; |
203 |
||
204 |
for (counter = keys.length - 1; counter >= 0; counter--) { |
|
205 |
keyMap.put(keys[counter], keys[counter]); |
|
206 |
} |
|
207 |
for (counter = pKeys.length - 1; counter >= 0; counter--) { |
|
208 |
keyMap.put(pKeys[counter], pKeys[counter]); |
|
209 |
} |
|
210 |
return keyMap.keySet().toArray(); |
|
211 |
} |
|
212 |
||
213 |
private void writeObject(ObjectOutputStream s) throws IOException { |
|
214 |
s.defaultWriteObject(); |
|
215 |
||
216 |
ArrayTable.writeArrayTable(s, arrayTable); |
|
217 |
} |
|
218 |
||
219 |
private void readObject(ObjectInputStream s) throws ClassNotFoundException, |
|
220 |
IOException { |
|
221 |
s.defaultReadObject(); |
|
222 |
for (int counter = s.readInt() - 1; counter >= 0; counter--) { |
|
223 |
put(s.readObject(), (Action)s.readObject()); |
|
224 |
} |
|
225 |
} |
|
226 |
} |