ActionMap.java 4.87 KB
Newer Older
1
/* ActionMap.java --
2
   Copyright (C) 2002, 2004 Free Software Foundation, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

package javax.swing;

40 41 42 43 44 45 46 47 48 49
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
50

Graydon Hoare committed
51

52 53
/**
 * @author	Andrew Selkirk
Graydon Hoare committed
54
 * @author Michael Koch
55
 */
Graydon Hoare committed
56 57
public class ActionMap
  implements Serializable
58
{
Graydon Hoare committed
59
  private static final long serialVersionUID = -6277518704513986346L;
60 61 62 63 64 65 66 67 68

	/**
	 * actionMap
	 */
	private Map actionMap = new HashMap();

	/**
	 * parent
	 */
Graydon Hoare committed
69
  private ActionMap parent;
70 71

	/**
Graydon Hoare committed
72
   * Creates a new <code>ActionMap</code> instance.
73
	 */
Graydon Hoare committed
74 75 76
  public ActionMap()
  {
  }
77 78

	/**
Graydon Hoare committed
79 80 81 82 83
   * Returns an action associated with an object.
   *
   * @param key the key of the enty
   *
   * @return the action associated with key, may be null
84
	 */
Graydon Hoare committed
85 86 87
  public Action get(Object key)
  {
    Object result = actionMap.get(key);
88

Graydon Hoare committed
89 90
    if (result == null && parent != null)
      result = parent.get(key);
91 92

		return (Action) result;
Graydon Hoare committed
93
  }
94 95

	/**
Graydon Hoare committed
96 97 98 99 100
   * Puts a new <code>Action</code> into the <code>ActionMap</code>.
   * If action is null an existing entry will be removed.
   *
   * @param key the key for the entry
   * @param action the action.
101
	 */
Graydon Hoare committed
102 103 104
  public void put(Object key, Action action)
  {
    if (action == null)
105
			actionMap.remove(key);
Graydon Hoare committed
106
    else
107
			actionMap.put(key, action);
Graydon Hoare committed
108
  }
109 110

	/**
Graydon Hoare committed
111 112 113
   * Remove an entry from the <code>ActionMap</code>.
   *
   * @param key the key of the entry to remove
114
	 */
Graydon Hoare committed
115 116
  public void remove(Object key)
  {
117
		actionMap.remove(key);
Graydon Hoare committed
118
  }
119 120

	/**
Graydon Hoare committed
121 122 123
   * Returns the parent of this <code>ActionMap</code>.
   *
   * @return the parent, may be null.
124
	 */
Graydon Hoare committed
125 126
  public ActionMap getParent()
  {
127
		return parent;
Graydon Hoare committed
128
  }
129 130

	/**
Graydon Hoare committed
131 132 133
   * Sets a parent for this <code>ActionMap</code>.
   *
   * @param parentMap the new parent
134
	 */
Graydon Hoare committed
135 136
  public void setParent(ActionMap parentMap)
  {
137
		parent = parentMap;
Graydon Hoare committed
138
  }
139 140

	/**
Graydon Hoare committed
141 142 143
   * Returns the number of entries in this <code>ActionMap</code>.
   *
   * @return the number of entries
144
	 */
Graydon Hoare committed
145 146
  public int size()
  {
147
		return actionMap.size();
Graydon Hoare committed
148
  }
149 150

	/**
Graydon Hoare committed
151
   * Clears the <code>ActionMap</code>.
152
	 */
Graydon Hoare committed
153 154
  public void clear()
  {
155
		actionMap.clear();
Graydon Hoare committed
156
  }
157 158

	/**
Graydon Hoare committed
159 160 161
   * Returns all keys of entries in this <code>ActionMap</code>.
   *
   * @return an array of keys
162
	 */
Graydon Hoare committed
163 164 165 166
  public Object[] keys()
  {
    return actionMap.keySet().toArray();
  }
167 168

	/**
Graydon Hoare committed
169 170 171 172
   * Returns all keys of entries in this <code>ActionMap</code>
   * and all its parents.
   *
   * @return an array of keys
173
	 */
Graydon Hoare committed
174 175 176
  public Object[] allKeys()
  {
    Set set = new HashSet();
177

Graydon Hoare committed
178
    if (parent != null)
179 180
			set.addAll(Arrays.asList(parent.allKeys()));

Graydon Hoare committed
181 182 183
    set.addAll(actionMap.keySet());
    return set.toArray();
  }
184 185 186

	/**
	 * writeObject
Graydon Hoare committed
187 188 189 190
   *
   * @param stream the stream to write to
   *
   * @exception IOException If an error occurs
191
	 */
Graydon Hoare committed
192 193 194
  private void writeObject(ObjectOutputStream stream)
    throws IOException
  {
195
		// TODO
Graydon Hoare committed
196
  }
197 198 199

	/**
	 * readObject
Graydon Hoare committed
200 201 202 203 204
   *
   * @param stream the stream to read from
   *
   * @exception ClassNotFoundException If the serialized class cannot be found
   * @exception IOException If an error occurs
205
	 */
Graydon Hoare committed
206 207 208
  private void readObject(ObjectInputStream stream)
    throws ClassNotFoundException, IOException
  {
209
		// TODO
Graydon Hoare committed
210 211
  }
}