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