| 1 |
772 |
jeremybenn |
/* InputMap.java --
|
| 2 |
|
|
Copyright (C) 2002, 2004, 2006, Free Software Foundation, Inc.
|
| 3 |
|
|
|
| 4 |
|
|
This file is part of GNU Classpath.
|
| 5 |
|
|
|
| 6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
| 7 |
|
|
it under the terms of the GNU General Public License as published by
|
| 8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
| 9 |
|
|
any later version.
|
| 10 |
|
|
|
| 11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
| 12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
| 14 |
|
|
General Public License for more details.
|
| 15 |
|
|
|
| 16 |
|
|
You should have received a copy of the GNU General Public License
|
| 17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
| 18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
| 19 |
|
|
02110-1301 USA.
|
| 20 |
|
|
|
| 21 |
|
|
Linking this library statically or dynamically with other modules is
|
| 22 |
|
|
making a combined work based on this library. Thus, the terms and
|
| 23 |
|
|
conditions of the GNU General Public License cover the whole
|
| 24 |
|
|
combination.
|
| 25 |
|
|
|
| 26 |
|
|
As a special exception, the copyright holders of this library give you
|
| 27 |
|
|
permission to link this library with independent modules to produce an
|
| 28 |
|
|
executable, regardless of the license terms of these independent
|
| 29 |
|
|
modules, and to copy and distribute the resulting executable under
|
| 30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
| 31 |
|
|
independent module, the terms and conditions of the license of that
|
| 32 |
|
|
module. An independent module is a module which is not derived from
|
| 33 |
|
|
or based on this library. If you modify this library, you may extend
|
| 34 |
|
|
this exception to your version of the library, but you are not
|
| 35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
| 36 |
|
|
exception statement from your version. */
|
| 37 |
|
|
|
| 38 |
|
|
package javax.swing;
|
| 39 |
|
|
|
| 40 |
|
|
import java.io.Serializable;
|
| 41 |
|
|
import java.util.Arrays;
|
| 42 |
|
|
import java.util.HashMap;
|
| 43 |
|
|
import java.util.HashSet;
|
| 44 |
|
|
import java.util.Map;
|
| 45 |
|
|
import java.util.Set;
|
| 46 |
|
|
|
| 47 |
|
|
/**
|
| 48 |
|
|
* Maps {@link KeyStroke}s to arbitrary objects, usually Strings. This
|
| 49 |
|
|
* is used in combination with {@link ActionMap}s.
|
| 50 |
|
|
*
|
| 51 |
|
|
* If a component receives an input event, this is looked up in
|
| 52 |
|
|
* the component's <code>InputMap</code>. The result is an object which
|
| 53 |
|
|
* serves as a key to the component's <code>ActionMap</code>. Finally
|
| 54 |
|
|
* the <code>Action</code> that is stored is executed.
|
| 55 |
|
|
*
|
| 56 |
|
|
* @author Andrew Selkirk
|
| 57 |
|
|
* @author Michael Koch
|
| 58 |
|
|
*
|
| 59 |
|
|
* @since 1.3
|
| 60 |
|
|
*/
|
| 61 |
|
|
public class InputMap
|
| 62 |
|
|
implements Serializable
|
| 63 |
|
|
{
|
| 64 |
|
|
private static final long serialVersionUID = -5429059542008604257L;
|
| 65 |
|
|
|
| 66 |
|
|
/**
|
| 67 |
|
|
* Storage for the KeyStroke --> Object mappings.
|
| 68 |
|
|
*/
|
| 69 |
|
|
private Map inputMap;
|
| 70 |
|
|
|
| 71 |
|
|
/**
|
| 72 |
|
|
* An optional parent map.
|
| 73 |
|
|
*/
|
| 74 |
|
|
private InputMap parent;
|
| 75 |
|
|
|
| 76 |
|
|
/**
|
| 77 |
|
|
* Creates a new <code>InputMap</code> instance. This default instance
|
| 78 |
|
|
* contains no mappings and has no parent.
|
| 79 |
|
|
*/
|
| 80 |
|
|
public InputMap()
|
| 81 |
|
|
{
|
| 82 |
|
|
// nothing to do
|
| 83 |
|
|
}
|
| 84 |
|
|
|
| 85 |
|
|
/**
|
| 86 |
|
|
* Returns the binding for the specified keystroke, if there is one.
|
| 87 |
|
|
*
|
| 88 |
|
|
* @param keystroke the key of the entry (<code>null</code> is ignored).
|
| 89 |
|
|
*
|
| 90 |
|
|
* @return The binding associated with the specified keystroke (or
|
| 91 |
|
|
* <code>null</code>).
|
| 92 |
|
|
*/
|
| 93 |
|
|
public Object get(KeyStroke keystroke)
|
| 94 |
|
|
{
|
| 95 |
|
|
Object result = null;
|
| 96 |
|
|
if (inputMap != null)
|
| 97 |
|
|
result = inputMap.get(keystroke);
|
| 98 |
|
|
|
| 99 |
|
|
if (result == null && parent != null)
|
| 100 |
|
|
result = parent.get(keystroke);
|
| 101 |
|
|
return result;
|
| 102 |
|
|
}
|
| 103 |
|
|
|
| 104 |
|
|
/**
|
| 105 |
|
|
* Puts a new entry into the <code>InputMap</code>. If
|
| 106 |
|
|
* <code>actionMapKey</code> is <code>null</code> any existing entry will be
|
| 107 |
|
|
* removed.
|
| 108 |
|
|
*
|
| 109 |
|
|
* @param keystroke the keystroke for the entry (<code>null</code> is
|
| 110 |
|
|
* ignored).
|
| 111 |
|
|
* @param actionMapKey the action (<code>null</code> permitted).
|
| 112 |
|
|
*/
|
| 113 |
|
|
public void put(KeyStroke keystroke, Object actionMapKey)
|
| 114 |
|
|
{
|
| 115 |
|
|
if (keystroke == null)
|
| 116 |
|
|
return;
|
| 117 |
|
|
if (inputMap == null)
|
| 118 |
|
|
inputMap = new HashMap();
|
| 119 |
|
|
if (actionMapKey == null)
|
| 120 |
|
|
inputMap.remove(keystroke);
|
| 121 |
|
|
else
|
| 122 |
|
|
inputMap.put(keystroke, actionMapKey);
|
| 123 |
|
|
}
|
| 124 |
|
|
|
| 125 |
|
|
/**
|
| 126 |
|
|
* Removes an entry from this <code>InputMap</code>. Note that this will
|
| 127 |
|
|
* not remove any entry from the parent map, if there is one.
|
| 128 |
|
|
*
|
| 129 |
|
|
* @param keystroke the key of the entry to remove (<code>null</code> is
|
| 130 |
|
|
* ignored).
|
| 131 |
|
|
*/
|
| 132 |
|
|
public void remove(KeyStroke keystroke)
|
| 133 |
|
|
{
|
| 134 |
|
|
if (inputMap != null)
|
| 135 |
|
|
inputMap.remove(keystroke);
|
| 136 |
|
|
}
|
| 137 |
|
|
|
| 138 |
|
|
/**
|
| 139 |
|
|
* Returns the parent of this <code>InputMap</code>. The default value
|
| 140 |
|
|
* is <code>null</code>.
|
| 141 |
|
|
*
|
| 142 |
|
|
* @return The parent map (possibly <code>null</code>).
|
| 143 |
|
|
*
|
| 144 |
|
|
* @see #setParent(InputMap)
|
| 145 |
|
|
*/
|
| 146 |
|
|
public InputMap getParent()
|
| 147 |
|
|
{
|
| 148 |
|
|
return parent;
|
| 149 |
|
|
}
|
| 150 |
|
|
|
| 151 |
|
|
/**
|
| 152 |
|
|
* Sets a parent for this <code>InputMap</code>. If a parent is specified,
|
| 153 |
|
|
* the {@link #get(KeyStroke)} method will look in the parent if it cannot
|
| 154 |
|
|
* find an entry in this map.
|
| 155 |
|
|
*
|
| 156 |
|
|
* @param parentMap the new parent (<code>null</code> permitted).
|
| 157 |
|
|
*
|
| 158 |
|
|
* @see #getParent()
|
| 159 |
|
|
*/
|
| 160 |
|
|
public void setParent(InputMap parentMap)
|
| 161 |
|
|
{
|
| 162 |
|
|
parent = parentMap;
|
| 163 |
|
|
}
|
| 164 |
|
|
|
| 165 |
|
|
/**
|
| 166 |
|
|
* Returns the number of entries in this <code>InputMap</code>. This count
|
| 167 |
|
|
* does not include any entries from the parent map, if there is one.
|
| 168 |
|
|
*
|
| 169 |
|
|
* @return The number of entries.
|
| 170 |
|
|
*/
|
| 171 |
|
|
public int size()
|
| 172 |
|
|
{
|
| 173 |
|
|
int result = 0;
|
| 174 |
|
|
if (inputMap != null)
|
| 175 |
|
|
result = inputMap.size();
|
| 176 |
|
|
return result;
|
| 177 |
|
|
}
|
| 178 |
|
|
|
| 179 |
|
|
/**
|
| 180 |
|
|
* Clears the entries from this <code>InputMap</code>. The parent map, if
|
| 181 |
|
|
* there is one, is not cleared.
|
| 182 |
|
|
*/
|
| 183 |
|
|
public void clear()
|
| 184 |
|
|
{
|
| 185 |
|
|
if (inputMap != null)
|
| 186 |
|
|
inputMap.clear();
|
| 187 |
|
|
}
|
| 188 |
|
|
|
| 189 |
|
|
/**
|
| 190 |
|
|
* Returns all keys of entries in this <code>InputMap</code>. This does not
|
| 191 |
|
|
* include keys defined in the parent, if there is one (use the
|
| 192 |
|
|
* {@link #allKeys()} method for that case).
|
| 193 |
|
|
* <br><br>
|
| 194 |
|
|
* Following the behaviour of the reference implementation, this method will
|
| 195 |
|
|
* return <code>null</code> when no entries have been added to the map,
|
| 196 |
|
|
* and a zero length array if entries have been added but subsequently
|
| 197 |
|
|
* removed (or cleared) from the map.
|
| 198 |
|
|
*
|
| 199 |
|
|
* @return An array of keys (may be <code>null</code> or have zero length).
|
| 200 |
|
|
*/
|
| 201 |
|
|
public KeyStroke[] keys()
|
| 202 |
|
|
{
|
| 203 |
|
|
if (inputMap != null)
|
| 204 |
|
|
{
|
| 205 |
|
|
KeyStroke[] array = new KeyStroke[size()];
|
| 206 |
|
|
return (KeyStroke[]) inputMap.keySet().toArray(array);
|
| 207 |
|
|
}
|
| 208 |
|
|
return null;
|
| 209 |
|
|
}
|
| 210 |
|
|
|
| 211 |
|
|
/**
|
| 212 |
|
|
* Returns all keys of entries in this <code>InputMap</code> and all its
|
| 213 |
|
|
* parents.
|
| 214 |
|
|
*
|
| 215 |
|
|
* @return An array of keys (may be <code>null</code> or have zero length).
|
| 216 |
|
|
*/
|
| 217 |
|
|
public KeyStroke[] allKeys()
|
| 218 |
|
|
{
|
| 219 |
|
|
Set set = new HashSet();
|
| 220 |
|
|
|
| 221 |
|
|
if (parent != null)
|
| 222 |
|
|
{
|
| 223 |
|
|
Object[] parentKeys = parent.allKeys();
|
| 224 |
|
|
if (parentKeys != null)
|
| 225 |
|
|
set.addAll(Arrays.asList(parentKeys));
|
| 226 |
|
|
}
|
| 227 |
|
|
if (inputMap != null)
|
| 228 |
|
|
set.addAll(inputMap.keySet());
|
| 229 |
|
|
if (set.size() == 0)
|
| 230 |
|
|
return null;
|
| 231 |
|
|
KeyStroke[] array = new KeyStroke[set.size()];
|
| 232 |
|
|
return (KeyStroke[]) set.toArray(array);
|
| 233 |
|
|
}
|
| 234 |
|
|
|
| 235 |
|
|
}
|