Path: blob/master/src/java.desktop/share/classes/javax/swing/ActionMap.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>ActionMap</code> provides mappings from36* <code>Object</code>s37* (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 action41* when a key is pressed. As with <code>InputMap</code>,42* an <code>ActionMap</code> can have a parent43* 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 InputMap54*55* @author Scott Violet56* @since 1.357*/58@SuppressWarnings("serial")59public class ActionMap implements Serializable {60/** Handles the mapping between Action name and Action. */61private transient ArrayTable arrayTable;62/** Parent that handles any bindings we don't contain. */63private ActionMap parent;646566/**67* Creates an <code>ActionMap</code> with no parent and no mappings.68*/69public ActionMap() {70}7172/**73* Sets this <code>ActionMap</code>'s parent.74*75* @param map the <code>ActionMap</code> that is the parent of this one76*/77public void setParent(ActionMap map) {78this.parent = map;79}8081/**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 parent86*/87public ActionMap getParent() {88return parent;89}9091/**92* Adds a binding for <code>key</code> to <code>action</code>.93* If <code>action</code> is null, this removes the current binding94* for <code>key</code>.95* <p>In most instances, <code>key</code> will be96* <code>action.getValue(NAME)</code>.97*98* @param key a key99* @param action a binding for {@code key}100*/101public void put(Object key, Action action) {102if (key == null) {103return;104}105if (action == null) {106remove(key);107}108else {109if (arrayTable == null) {110arrayTable = new ArrayTable();111}112arrayTable.put(key, action);113}114}115116/**117* Returns the binding for <code>key</code>, messaging the118* parent <code>ActionMap</code> if the binding is not locally defined.119*120* @param key a key121* @return the binding for {@code key}122*/123public Action get(Object key) {124Action value = (arrayTable == null) ? null :125(Action)arrayTable.get(key);126127if (value == null) {128ActionMap parent = getParent();129130if (parent != null) {131return parent.get(key);132}133}134return value;135}136137/**138* Removes the binding for <code>key</code> from this <code>ActionMap</code>.139*140* @param key a key141*/142public void remove(Object key) {143if (arrayTable != null) {144arrayTable.remove(key);145}146}147148/**149* Removes all the mappings from this <code>ActionMap</code>.150*/151public void clear() {152if (arrayTable != null) {153arrayTable.clear();154}155}156157/**158* Returns the <code>Action</code> names that are bound in this <code>ActionMap</code>.159*160* @return an array of the keys161*/162public Object[] keys() {163if (arrayTable == null) {164return null;165}166return arrayTable.getKeys(null);167}168169/**170* Returns the number of bindings in this {@code ActionMap}.171*172* @return the number of bindings in this {@code ActionMap}173*/174public int size() {175if (arrayTable == null) {176return 0;177}178return arrayTable.size();179}180181/**182* Returns an array of the keys defined in this <code>ActionMap</code> and183* its parent. This method differs from <code>keys()</code> in that184* this method includes the keys defined in the parent.185*186* @return an array of the keys187*/188public Object[] allKeys() {189int count = size();190ActionMap parent = getParent();191192if (count == 0) {193if (parent != null) {194return parent.allKeys();195}196return keys();197}198if (parent == null) {199return keys();200}201Object[] keys = keys();202Object[] pKeys = parent.allKeys();203204if (pKeys == null) {205return keys;206}207if (keys == null) {208// Should only happen if size() != keys.length, which should only209// happen if mutated from multiple threads (or a bogus subclass).210return pKeys;211}212213HashMap<Object, Object> keyMap = new HashMap<Object, Object>();214int counter;215216for (counter = keys.length - 1; counter >= 0; counter--) {217keyMap.put(keys[counter], keys[counter]);218}219for (counter = pKeys.length - 1; counter >= 0; counter--) {220keyMap.put(pKeys[counter], pKeys[counter]);221}222return keyMap.keySet().toArray();223}224225@Serial226private void writeObject(ObjectOutputStream s) throws IOException {227s.defaultWriteObject();228229ArrayTable.writeArrayTable(s, arrayTable);230}231232@Serial233private void readObject(ObjectInputStream s) throws ClassNotFoundException,234IOException {235s.defaultReadObject();236for (int counter = s.readInt() - 1; counter >= 0; counter--) {237put(s.readObject(), (Action)s.readObject());238}239}240}241242243