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: public class ActionMap implements Serializable {
026: private static final long serialVersionUID = -6277518704513986346L;
027:
028: private ActionMap parent;
029:
030: private HashMap<Object, Action> table;
031:
032: public void put(final Object key, final Action action) {
033: if (action != null) {
034: if (table == null) {
035: table = new HashMap<Object, Action>();
036: }
037: table.put(key, action);
038: } else {
039: remove(key);
040: }
041: }
042:
043: public Action get(final Object key) {
044: Action action = null;
045: if (table != null) {
046: action = table.get(key);
047: }
048: if (action == null && getParent() != null) {
049: action = getParent().get(key);
050: }
051: return action;
052: }
053:
054: public void setParent(final ActionMap parent) {
055: this .parent = parent;
056: }
057:
058: public ActionMap getParent() {
059: return parent;
060: }
061:
062: public void remove(final Object key) {
063: if (table != null) {
064: table.remove(key);
065: }
066: }
067:
068: public Object[] keys() {
069: if (table == null) {
070: return new Object[0];
071: }
072: return table.keySet().toArray(new Object[table.size()]);
073: }
074:
075: public Object[] allKeys() {
076: Object[] keys = keys();
077: if (parent == null) {
078: return keys;
079: }
080: Object[] parentKeys = parent.allKeys();
081: if (keys.length == 0) {
082: return parentKeys;
083: }
084: if (parentKeys.length == 0) {
085: return keys;
086: }
087: HashSet<Object> keySet = new HashSet<Object>(Arrays
088: .asList(keys));
089: keySet.addAll(Arrays.asList(parentKeys));
090: return keySet.toArray(new Object[keySet.size()]);
091: }
092:
093: public void clear() {
094: if (table != null) {
095: table.clear();
096: }
097: }
098:
099: public int size() {
100: return (table != null) ? table.size() : 0;
101: }
102: }
|