Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/swing/ActionMap.java
38829 views
/*1* Copyright (c) 1999, 2011, 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*/24package javax.swing;2526import java.io.IOException;27import java.io.ObjectInputStream;28import java.io.ObjectOutputStream;29import java.io.Serializable;30import java.util.HashMap;31import java.util.Set;3233/**34* <code>ActionMap</code> provides mappings from35* <code>Object</code>s36* (called <em>keys</em> or <em><code>Action</code> names</em>)37* to <code>Action</code>s.38* An <code>ActionMap</code> is usually used with an <code>InputMap</code>39* to locate a particular action40* when a key is pressed. As with <code>InputMap</code>,41* an <code>ActionMap</code> can have a parent42* that is searched for keys not defined in the <code>ActionMap</code>.43* <p>As with <code>InputMap</code> if you create a cycle, eg:44* <pre>45* ActionMap am = new ActionMap();46* ActionMap bm = new ActionMap():47* am.setParent(bm);48* bm.setParent(am);49* </pre>50* some of the methods will cause a StackOverflowError to be thrown.51*52* @see InputMap53*54* @author Scott Violet55* @since 1.356*/57@SuppressWarnings("serial")58public class ActionMap implements Serializable {59/** Handles the mapping between Action name and Action. */60private transient ArrayTable arrayTable;61/** Parent that handles any bindings we don't contain. */62private ActionMap parent;636465/**66* Creates an <code>ActionMap</code> with no parent and no mappings.67*/68public ActionMap() {69}7071/**72* Sets this <code>ActionMap</code>'s parent.73*74* @param map the <code>ActionMap</code> that is the parent of this one75*/76public void setParent(ActionMap map) {77this.parent = map;78}7980/**81* Returns this <code>ActionMap</code>'s parent.82*83* @return the <code>ActionMap</code> that is the parent of this one,84* or null if this <code>ActionMap</code> has no parent85*/86public ActionMap getParent() {87return parent;88}8990/**91* Adds a binding for <code>key</code> to <code>action</code>.92* If <code>action</code> is null, this removes the current binding93* for <code>key</code>.94* <p>In most instances, <code>key</code> will be95* <code>action.getValue(NAME)</code>.96*/97public void put(Object key, Action action) {98if (key == null) {99return;100}101if (action == null) {102remove(key);103}104else {105if (arrayTable == null) {106arrayTable = new ArrayTable();107}108arrayTable.put(key, action);109}110}111112/**113* Returns the binding for <code>key</code>, messaging the114* parent <code>ActionMap</code> if the binding is not locally defined.115*/116public Action get(Object key) {117Action value = (arrayTable == null) ? null :118(Action)arrayTable.get(key);119120if (value == null) {121ActionMap parent = getParent();122123if (parent != null) {124return parent.get(key);125}126}127return value;128}129130/**131* Removes the binding for <code>key</code> from this <code>ActionMap</code>.132*/133public void remove(Object key) {134if (arrayTable != null) {135arrayTable.remove(key);136}137}138139/**140* Removes all the mappings from this <code>ActionMap</code>.141*/142public void clear() {143if (arrayTable != null) {144arrayTable.clear();145}146}147148/**149* Returns the <code>Action</code> names that are bound in this <code>ActionMap</code>.150*/151public Object[] keys() {152if (arrayTable == null) {153return null;154}155return arrayTable.getKeys(null);156}157158/**159* Returns the number of bindings in this {@code ActionMap}.160*161* @return the number of bindings in this {@code ActionMap}162*/163public int size() {164if (arrayTable == null) {165return 0;166}167return arrayTable.size();168}169170/**171* Returns an array of the keys defined in this <code>ActionMap</code> and172* its parent. This method differs from <code>keys()</code> in that173* this method includes the keys defined in the parent.174*/175public Object[] allKeys() {176int count = size();177ActionMap parent = getParent();178179if (count == 0) {180if (parent != null) {181return parent.allKeys();182}183return keys();184}185if (parent == null) {186return keys();187}188Object[] keys = keys();189Object[] pKeys = parent.allKeys();190191if (pKeys == null) {192return keys;193}194if (keys == null) {195// Should only happen if size() != keys.length, which should only196// happen if mutated from multiple threads (or a bogus subclass).197return pKeys;198}199200HashMap<Object, Object> keyMap = new HashMap<Object, Object>();201int counter;202203for (counter = keys.length - 1; counter >= 0; counter--) {204keyMap.put(keys[counter], keys[counter]);205}206for (counter = pKeys.length - 1; counter >= 0; counter--) {207keyMap.put(pKeys[counter], pKeys[counter]);208}209return keyMap.keySet().toArray();210}211212private void writeObject(ObjectOutputStream s) throws IOException {213s.defaultWriteObject();214215ArrayTable.writeArrayTable(s, arrayTable);216}217218private void readObject(ObjectInputStream s) throws ClassNotFoundException,219IOException {220s.defaultReadObject();221for (int counter = s.readInt() - 1; counter >= 0; counter--) {222put(s.readObject(), (Action)s.readObject());223}224}225}226227228