0N/A/*
2362N/A * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage javax.swing;
0N/A
0N/Aimport java.io.IOException;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.io.ObjectOutputStream;
0N/Aimport java.io.Serializable;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.Set;
0N/A
0N/A/**
0N/A * <code>InputMap</code> provides a binding between an input event
0N/A * (currently only <code>KeyStroke</code>s are used)
0N/A * and an <code>Object</code>. <code>InputMap</code>s
0N/A * are usually used with an <code>ActionMap</code>,
0N/A * to determine an <code>Action</code> to perform
0N/A * when a key is pressed.
0N/A * An <code>InputMap</code> can have a parent
0N/A * that is searched for bindings not defined in the <code>InputMap</code>.
0N/A * <p>As with <code>ActionMap</code> if you create a cycle, eg:
0N/A * <pre>
0N/A * InputMap am = new InputMap();
0N/A * InputMap bm = new InputMap():
0N/A * am.setParent(bm);
0N/A * bm.setParent(am);
0N/A * </pre>
0N/A * some of the methods will cause a StackOverflowError to be thrown.
0N/A *
0N/A * @author Scott Violet
0N/A * @since 1.3
0N/A */
0N/Apublic class InputMap implements Serializable {
0N/A /** Handles the mapping between KeyStroke and Action name. */
0N/A private transient ArrayTable arrayTable;
0N/A /** Parent that handles any bindings we don't contain. */
0N/A private InputMap parent;
0N/A
0N/A
0N/A /**
0N/A * Creates an <code>InputMap</code> with no parent and no mappings.
0N/A */
0N/A public InputMap() {
0N/A }
0N/A
0N/A /**
0N/A * Sets this <code>InputMap</code>'s parent.
0N/A *
0N/A * @param map the <code>InputMap</code> that is the parent of this one
0N/A */
0N/A public void setParent(InputMap map) {
0N/A this.parent = map;
0N/A }
0N/A
0N/A /**
0N/A * Gets this <code>InputMap</code>'s parent.
0N/A *
0N/A * @return map the <code>InputMap</code> that is the parent of this one,
0N/A * or null if this <code>InputMap</code> has no parent
0N/A */
0N/A public InputMap getParent() {
0N/A return parent;
0N/A }
0N/A
0N/A /**
0N/A * Adds a binding for <code>keyStroke</code> to <code>actionMapKey</code>.
0N/A * If <code>actionMapKey</code> is null, this removes the current binding
0N/A * for <code>keyStroke</code>.
0N/A */
0N/A public void put(KeyStroke keyStroke, Object actionMapKey) {
0N/A if (keyStroke == null) {
0N/A return;
0N/A }
0N/A if (actionMapKey == null) {
0N/A remove(keyStroke);
0N/A }
0N/A else {
0N/A if (arrayTable == null) {
0N/A arrayTable = new ArrayTable();
0N/A }
0N/A arrayTable.put(keyStroke, actionMapKey);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the binding for <code>keyStroke</code>, messaging the
0N/A * parent <code>InputMap</code> if the binding is not locally defined.
0N/A */
0N/A public Object get(KeyStroke keyStroke) {
0N/A if (arrayTable == null) {
0N/A InputMap parent = getParent();
0N/A
0N/A if (parent != null) {
0N/A return parent.get(keyStroke);
0N/A }
0N/A return null;
0N/A }
0N/A Object value = arrayTable.get(keyStroke);
0N/A
0N/A if (value == null) {
0N/A InputMap parent = getParent();
0N/A
0N/A if (parent != null) {
0N/A return parent.get(keyStroke);
0N/A }
0N/A }
0N/A return value;
0N/A }
0N/A
0N/A /**
0N/A * Removes the binding for <code>key</code> from this
0N/A * <code>InputMap</code>.
0N/A */
0N/A public void remove(KeyStroke key) {
0N/A if (arrayTable != null) {
0N/A arrayTable.remove(key);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Removes all the mappings from this <code>InputMap</code>.
0N/A */
0N/A public void clear() {
0N/A if (arrayTable != null) {
0N/A arrayTable.clear();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the <code>KeyStroke</code>s that are bound in this <code>InputMap</code>.
0N/A */
0N/A public KeyStroke[] keys() {
0N/A if (arrayTable == null) {
0N/A return null;
0N/A }
0N/A KeyStroke[] keys = new KeyStroke[arrayTable.size()];
0N/A arrayTable.getKeys(keys);
0N/A return keys;
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of <code>KeyStroke</code> bindings.
0N/A */
0N/A public int size() {
0N/A if (arrayTable == null) {
0N/A return 0;
0N/A }
0N/A return arrayTable.size();
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of the <code>KeyStroke</code>s defined in this
0N/A * <code>InputMap</code> and its parent. This differs from <code>keys()</code> in that
0N/A * this method includes the keys defined in the parent.
0N/A */
0N/A public KeyStroke[] allKeys() {
0N/A int count = size();
0N/A InputMap parent = getParent();
0N/A
0N/A if (count == 0) {
0N/A if (parent != null) {
0N/A return parent.allKeys();
0N/A }
0N/A return keys();
0N/A }
0N/A if (parent == null) {
0N/A return keys();
0N/A }
0N/A KeyStroke[] keys = keys();
0N/A KeyStroke[] pKeys = parent.allKeys();
0N/A
0N/A if (pKeys == null) {
0N/A return keys;
0N/A }
0N/A if (keys == null) {
0N/A // Should only happen if size() != keys.length, which should only
0N/A // happen if mutated from multiple threads (or a bogus subclass).
0N/A return pKeys;
0N/A }
0N/A
625N/A HashMap<KeyStroke, KeyStroke> keyMap = new HashMap<KeyStroke, KeyStroke>();
0N/A int counter;
0N/A
0N/A for (counter = keys.length - 1; counter >= 0; counter--) {
0N/A keyMap.put(keys[counter], keys[counter]);
0N/A }
0N/A for (counter = pKeys.length - 1; counter >= 0; counter--) {
0N/A keyMap.put(pKeys[counter], pKeys[counter]);
0N/A }
0N/A
0N/A KeyStroke[] allKeys = new KeyStroke[keyMap.size()];
0N/A
625N/A return keyMap.keySet().toArray(allKeys);
0N/A }
0N/A
0N/A private void writeObject(ObjectOutputStream s) throws IOException {
0N/A s.defaultWriteObject();
0N/A
0N/A ArrayTable.writeArrayTable(s, arrayTable);
0N/A }
0N/A
0N/A private void readObject(ObjectInputStream s) throws ClassNotFoundException,
0N/A IOException {
0N/A s.defaultReadObject();
0N/A for (int counter = s.readInt() - 1; counter >= 0; counter--) {
0N/A put((KeyStroke)s.readObject(), s.readObject());
0N/A }
0N/A }
0N/A}