Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/classes/javax/swing/ActionMap.java
41153 views
1
/*
2
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
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
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
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
*
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.
24
*/
25
26
package javax.swing;
27
28
import java.io.IOException;
29
import java.io.ObjectInputStream;
30
import java.io.ObjectOutputStream;
31
import java.io.Serial;
32
import java.io.Serializable;
33
import java.util.HashMap;
34
35
/**
36
* <code>ActionMap</code> provides mappings from
37
* <code>Object</code>s
38
* (called <em>keys</em> or <em><code>Action</code> names</em>)
39
* to <code>Action</code>s.
40
* An <code>ActionMap</code> is usually used with an <code>InputMap</code>
41
* to locate a particular action
42
* when a key is pressed. As with <code>InputMap</code>,
43
* an <code>ActionMap</code> can have a parent
44
* that is searched for keys not defined in the <code>ActionMap</code>.
45
* <p>As with <code>InputMap</code> if you create a cycle, eg:
46
* <pre>
47
* ActionMap am = new ActionMap();
48
* ActionMap bm = new ActionMap():
49
* am.setParent(bm);
50
* bm.setParent(am);
51
* </pre>
52
* some of the methods will cause a StackOverflowError to be thrown.
53
*
54
* @see InputMap
55
*
56
* @author Scott Violet
57
* @since 1.3
58
*/
59
@SuppressWarnings("serial")
60
public class ActionMap implements Serializable {
61
/** Handles the mapping between Action name and Action. */
62
private transient ArrayTable arrayTable;
63
/** Parent that handles any bindings we don't contain. */
64
private ActionMap parent;
65
66
67
/**
68
* Creates an <code>ActionMap</code> with no parent and no mappings.
69
*/
70
public ActionMap() {
71
}
72
73
/**
74
* Sets this <code>ActionMap</code>'s parent.
75
*
76
* @param map the <code>ActionMap</code> that is the parent of this one
77
*/
78
public void setParent(ActionMap map) {
79
this.parent = map;
80
}
81
82
/**
83
* Returns this <code>ActionMap</code>'s parent.
84
*
85
* @return the <code>ActionMap</code> that is the parent of this one,
86
* or null if this <code>ActionMap</code> has no parent
87
*/
88
public ActionMap getParent() {
89
return parent;
90
}
91
92
/**
93
* Adds a binding for <code>key</code> to <code>action</code>.
94
* If <code>action</code> is null, this removes the current binding
95
* for <code>key</code>.
96
* <p>In most instances, <code>key</code> will be
97
* <code>action.getValue(NAME)</code>.
98
*
99
* @param key a key
100
* @param action a binding for {@code key}
101
*/
102
public void put(Object key, Action action) {
103
if (key == null) {
104
return;
105
}
106
if (action == null) {
107
remove(key);
108
}
109
else {
110
if (arrayTable == null) {
111
arrayTable = new ArrayTable();
112
}
113
arrayTable.put(key, action);
114
}
115
}
116
117
/**
118
* Returns the binding for <code>key</code>, messaging the
119
* parent <code>ActionMap</code> if the binding is not locally defined.
120
*
121
* @param key a key
122
* @return the binding for {@code key}
123
*/
124
public Action get(Object key) {
125
Action value = (arrayTable == null) ? null :
126
(Action)arrayTable.get(key);
127
128
if (value == null) {
129
ActionMap parent = getParent();
130
131
if (parent != null) {
132
return parent.get(key);
133
}
134
}
135
return value;
136
}
137
138
/**
139
* Removes the binding for <code>key</code> from this <code>ActionMap</code>.
140
*
141
* @param key a key
142
*/
143
public void remove(Object key) {
144
if (arrayTable != null) {
145
arrayTable.remove(key);
146
}
147
}
148
149
/**
150
* Removes all the mappings from this <code>ActionMap</code>.
151
*/
152
public void clear() {
153
if (arrayTable != null) {
154
arrayTable.clear();
155
}
156
}
157
158
/**
159
* Returns the <code>Action</code> names that are bound in this <code>ActionMap</code>.
160
*
161
* @return an array of the keys
162
*/
163
public Object[] keys() {
164
if (arrayTable == null) {
165
return null;
166
}
167
return arrayTable.getKeys(null);
168
}
169
170
/**
171
* Returns the number of bindings in this {@code ActionMap}.
172
*
173
* @return the number of bindings in this {@code ActionMap}
174
*/
175
public int size() {
176
if (arrayTable == null) {
177
return 0;
178
}
179
return arrayTable.size();
180
}
181
182
/**
183
* Returns an array of the keys defined in this <code>ActionMap</code> and
184
* its parent. This method differs from <code>keys()</code> in that
185
* this method includes the keys defined in the parent.
186
*
187
* @return an array of the keys
188
*/
189
public Object[] allKeys() {
190
int count = size();
191
ActionMap parent = getParent();
192
193
if (count == 0) {
194
if (parent != null) {
195
return parent.allKeys();
196
}
197
return keys();
198
}
199
if (parent == null) {
200
return keys();
201
}
202
Object[] keys = keys();
203
Object[] pKeys = parent.allKeys();
204
205
if (pKeys == null) {
206
return keys;
207
}
208
if (keys == null) {
209
// Should only happen if size() != keys.length, which should only
210
// happen if mutated from multiple threads (or a bogus subclass).
211
return pKeys;
212
}
213
214
HashMap<Object, Object> keyMap = new HashMap<Object, Object>();
215
int counter;
216
217
for (counter = keys.length - 1; counter >= 0; counter--) {
218
keyMap.put(keys[counter], keys[counter]);
219
}
220
for (counter = pKeys.length - 1; counter >= 0; counter--) {
221
keyMap.put(pKeys[counter], pKeys[counter]);
222
}
223
return keyMap.keySet().toArray();
224
}
225
226
@Serial
227
private void writeObject(ObjectOutputStream s) throws IOException {
228
s.defaultWriteObject();
229
230
ArrayTable.writeArrayTable(s, arrayTable);
231
}
232
233
@Serial
234
private void readObject(ObjectInputStream s) throws ClassNotFoundException,
235
IOException {
236
s.defaultReadObject();
237
for (int counter = s.readInt() - 1; counter >= 0; counter--) {
238
put(s.readObject(), (Action)s.readObject());
239
}
240
}
241
}
242
243