001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.swing;
019:
020: import java.io.Serializable;
021: import java.util.Arrays;
022: import java.util.HashMap;
023: import java.util.HashSet;
024:
025: /**
026: * <p>
027: * <i>InputMap</i>
028: * </p>
029: * <h3>Implementation Notes:</h3>
030: * <ul>
031: * <li>The <code>serialVersionUID</code> fields are explicitly declared as a performance
032: * optimization, not as a guarantee of serialization compatibility.</li>
033: * </ul>
034: */
035: public class InputMap implements Serializable {
036: private static final long serialVersionUID = -6824008057073482094L;
037:
038: private InputMap parent;
039:
040: private HashMap<KeyStroke, Object> table;
041:
042: public void put(KeyStroke keyStroke, Object key) {
043: if (keyStroke == null) {
044: return;
045: }
046: if (key != null) {
047: if (table == null) {
048: table = new HashMap<KeyStroke, Object>();
049: }
050: table.put(keyStroke, key);
051: } else {
052: remove(keyStroke);
053: }
054: }
055:
056: public Object get(KeyStroke keyStroke) {
057: Object key = null;
058: if (table != null) {
059: key = table.get(keyStroke);
060: }
061: if (key == null && getParent() != null) {
062: key = getParent().get(keyStroke);
063: }
064: return key;
065: }
066:
067: public void remove(KeyStroke keyStroke) {
068: if (table != null) {
069: table.remove(keyStroke);
070: }
071: }
072:
073: public KeyStroke[] keys() {
074: if (table == null) {
075: return new KeyStroke[0];
076: }
077: return table.keySet().toArray(new KeyStroke[table.size()]);
078: }
079:
080: public KeyStroke[] allKeys() {
081: KeyStroke[] keys = keys();
082: if (parent == null) {
083: return keys;
084: }
085: KeyStroke[] parentKeys = parent.allKeys();
086: if (keys.length == 0) {
087: return parentKeys;
088: }
089: if (parentKeys.length == 0) {
090: return keys;
091: }
092: HashSet<KeyStroke> keySet = new HashSet<KeyStroke>(Arrays
093: .asList(keys));
094: keySet.addAll(Arrays.asList(parentKeys));
095: return keySet.toArray(new KeyStroke[keySet.size()]);
096: }
097:
098: public void setParent(InputMap parent) {
099: this .parent = parent;
100: }
101:
102: public InputMap getParent() {
103: return parent;
104: }
105:
106: public void clear() {
107: if (table != null) {
108: table.clear();
109: }
110: }
111:
112: public int size() {
113: return (table != null) ? table.size() : 0;
114: }
115: }
|