Path: blob/master/src/java.desktop/share/classes/javax/swing/InputMap.java
41153 views
/*1* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.swing;2627import java.io.IOException;28import java.io.ObjectInputStream;29import java.io.ObjectOutputStream;30import java.io.Serial;31import java.io.Serializable;32import java.util.HashMap;3334/**35* {@code InputMap} provides a binding between an input event (currently only36* {@code KeyStroke}s are used) and an {@code Object}. {@code InputMap}s are37* usually used with an {@code ActionMap}, to determine an {@code Action} to38* perform when a key is pressed. An {@code InputMap} can have a parent that39* is searched for bindings not defined in the {@code InputMap}.40* <p>As with {@code ActionMap} if you create a cycle, eg:41* <pre>42* InputMap am = new InputMap();43* InputMap bm = new InputMap():44* am.setParent(bm);45* bm.setParent(am);46* </pre>47* some of the methods will cause a StackOverflowError to be thrown.48*49* @author Scott Violet50* @since 1.351*/52@SuppressWarnings("serial")53public class InputMap implements Serializable {54/** Handles the mapping between KeyStroke and Action name. */55private transient ArrayTable arrayTable;56/** Parent that handles any bindings we don't contain. */57private InputMap parent;585960/**61* Creates an {@code InputMap} with no parent and no mappings.62*/63public InputMap() {64}6566/**67* Sets this {@code InputMap}'s parent.68*69* @param map the {@code InputMap} that is the parent of this one70*/71public void setParent(InputMap map) {72this.parent = map;73}7475/**76* Gets this {@code InputMap}'s parent.77*78* @return map the {@code InputMap} that is the parent of this one,79* or null if this {@code InputMap} has no parent80*/81public InputMap getParent() {82return parent;83}8485/**86* Adds a binding for {@code keyStroke} to {@code actionMapKey}.87* If {@code actionMapKey} is null, this removes the current binding88* for {@code keyStroke}.89*90* @param keyStroke a {@code KeyStroke}91* @param actionMapKey an action map key92*/93public void put(KeyStroke keyStroke, Object actionMapKey) {94if (keyStroke == null) {95return;96}97if (actionMapKey == null) {98remove(keyStroke);99}100else {101if (arrayTable == null) {102arrayTable = new ArrayTable();103}104arrayTable.put(keyStroke, actionMapKey);105}106}107108/**109* Returns the binding for {@code keyStroke}, messaging the110* parent {@code InputMap} if the binding is not locally defined.111*112* @param keyStroke the {@code KeyStroke} for which to get the binding113* @return the binding for {@code keyStroke}114*/115public Object get(KeyStroke keyStroke) {116if (arrayTable == null) {117InputMap parent = getParent();118119if (parent != null) {120return parent.get(keyStroke);121}122return null;123}124Object value = arrayTable.get(keyStroke);125126if (value == null) {127InputMap parent = getParent();128129if (parent != null) {130return parent.get(keyStroke);131}132}133return value;134}135136/**137* Removes the binding for {@code key} from this {@code InputMap}.138*139* @param key the {@code KeyStroke} for which to remove the binding140*/141public void remove(KeyStroke key) {142if (arrayTable != null) {143arrayTable.remove(key);144}145}146147/**148* Removes all the mappings from this {@code InputMap}.149*/150public void clear() {151if (arrayTable != null) {152arrayTable.clear();153}154}155156/**157* Returns the {@code KeyStroke}s that are bound in this {@code InputMap}.158*159* @return an array of the {@code KeyStroke}s that are bound in this160* {@code InputMap}161*/162public KeyStroke[] keys() {163if (arrayTable == null) {164return null;165}166KeyStroke[] keys = new KeyStroke[arrayTable.size()];167arrayTable.getKeys(keys);168return keys;169}170171/**172* Returns the number of {@code KeyStroke} bindings.173*174* @return the number of {@code KeyStroke} bindings175*/176public int size() {177if (arrayTable == null) {178return 0;179}180return arrayTable.size();181}182183/**184* Returns an array of the {@code KeyStroke}s defined in this185* {@code InputMap} and its parent. This differs from {@code keys()}186* in that this method includes the keys defined in the parent.187*188* @return an array of the {@code KeyStroke}s defined in this189* {@code InputMap} and its parent190*/191public KeyStroke[] allKeys() {192int count = size();193InputMap parent = getParent();194195if (count == 0) {196if (parent != null) {197return parent.allKeys();198}199return keys();200}201if (parent == null) {202return keys();203}204KeyStroke[] keys = keys();205KeyStroke[] pKeys = parent.allKeys();206207if (pKeys == null) {208return keys;209}210if (keys == null) {211// Should only happen if size() != keys.length, which should only212// happen if mutated from multiple threads (or a bogus subclass).213return pKeys;214}215216HashMap<KeyStroke, KeyStroke> keyMap = new HashMap<KeyStroke, KeyStroke>();217int counter;218219for (counter = keys.length - 1; counter >= 0; counter--) {220keyMap.put(keys[counter], keys[counter]);221}222for (counter = pKeys.length - 1; counter >= 0; counter--) {223keyMap.put(pKeys[counter], pKeys[counter]);224}225226KeyStroke[] allKeys = new KeyStroke[keyMap.size()];227228return keyMap.keySet().toArray(allKeys);229}230231@Serial232private void writeObject(ObjectOutputStream s) throws IOException {233s.defaultWriteObject();234235ArrayTable.writeArrayTable(s, arrayTable);236}237238@Serial239private void readObject(ObjectInputStream s) throws ClassNotFoundException,240IOException {241s.defaultReadObject();242for (int counter = s.readInt() - 1; counter >= 0; counter--) {243put((KeyStroke)s.readObject(), s.readObject());244}245}246}247248249