javoids
Class KeyMap

java.lang.Object
  extended by javoids.KeyMap
All Implemented Interfaces:
java.io.Serializable

public final class KeyMap
extends java.lang.Object
implements java.io.Serializable

A class to handle keyboard mappings of key presses to actions.

Author:
mallette
See Also:
Serialized Form

Nested Class Summary
static class KeyMap.Action
          A list of possible actions that can be mapped to key presses in the game.
 
Field Summary
protected  java.util.Hashtable<KeyMap.Action,java.lang.Integer> actionKey
          a mapping of actions to key codes
protected static java.util.Hashtable<KeyMap.Action,java.lang.String> actionMnemonic
          a mapping of an action to a mnemonic
private static java.util.HashSet<KeyMap.Action> actions
          the valid actions
protected  java.util.Hashtable<java.lang.Integer,KeyMap.Action> keyAction
          a mapping of key codes to actions
protected static java.util.Hashtable<java.lang.Integer,java.lang.String> keyMnemonic
          a mapping of key code to a mnemonic
private static java.util.HashSet<java.lang.Integer> keys
          the valid key codes
private static int MAX_KEYS
          the maximum number of key presses combinations that can be stored
private static java.util.Hashtable<java.lang.String,KeyMap.Action> mnemonicAction
          a mapping of mnemonic to an action
private static java.util.Hashtable<java.lang.String,java.lang.Integer> mnemonicKey
          a mapping of mnemonic to key code
private  java.util.Set<KeyMap.Action> pressedButtons
          a set of actions to perform
private  java.util.Set<java.lang.Integer> pressedKeys
          a set of pressed keys to process
private static long serialVersionUID
          This is the version used for serializing/deserializing (storing/retrieving) this object
 
Constructor Summary
KeyMap()
          Constructor (default)
KeyMap(KeyMap keyMap)
          Constructor (copy)
 
Method Summary
static boolean containsActionMnemonic(KeyMap.Action action)
           
static boolean containsMnemonicKey(java.lang.String mnemonic)
           
 void copyValues(KeyMap destination, KeyMap source)
          Copy the source keymap to the destination keymap
 boolean depressKey(java.lang.Integer key)
          Remove an action from the list of keys that were pressed (to stop processing the action)
 void depressMouse(KeyMap.Action action)
          Remove an action from the list of mouse buttons that were pressed (to stop processing the action)
 KeyMap.Action getAction(java.lang.Integer key)
           
static KeyMap.Action getAction(java.lang.String mnemonic)
           
 java.util.Collection<KeyMap.Action> getActions()
           
static java.lang.Integer getKey(java.lang.String mnemonic)
           
static java.lang.String getMnemonic(KeyMap.Action action)
           
 boolean load(java.net.URL url)
          Load a list of keys and actions
(package private)  void loadMnemonic(java.lang.String key, java.lang.String data)
          Store an action related to the mneonic in the key for the action in the data variables.
 java.util.Collection<KeyMap.Action> pressedButtons()
           
 java.util.Collection<java.lang.Integer> pressedKeys()
           
 void pressKey(java.lang.Integer key)
          Add an action to the list of keys that were pressed (for processing actions)
 void pressMouse(KeyMap.Action action)
          Add an action to the list of mouse buttons that were pressed (for processing actions)
 void putAction(java.lang.Integer key, KeyMap.Action action)
           
static void putMnemonicAction(java.lang.String mnemonic, KeyMap.Action action)
           
static void putMnemonicKey(java.lang.String mnemonic, int key)
           
 void reset()
          reset the keymap to its default values
 void save(java.net.URL url)
          Save a list of keys and actions
 java.lang.String toString()
          Provide a String representation of this object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
This is the version used for serializing/deserializing (storing/retrieving) this object

See Also:
Constant Field Values

MAX_KEYS

private static int MAX_KEYS
the maximum number of key presses combinations that can be stored


keys

private static java.util.HashSet<java.lang.Integer> keys
the valid key codes


actions

private static java.util.HashSet<KeyMap.Action> actions
the valid actions


mnemonicKey

private static java.util.Hashtable<java.lang.String,java.lang.Integer> mnemonicKey
a mapping of mnemonic to key code


mnemonicAction

private static java.util.Hashtable<java.lang.String,KeyMap.Action> mnemonicAction
a mapping of mnemonic to an action


keyMnemonic

protected static java.util.Hashtable<java.lang.Integer,java.lang.String> keyMnemonic
a mapping of key code to a mnemonic


actionMnemonic

protected static java.util.Hashtable<KeyMap.Action,java.lang.String> actionMnemonic
a mapping of an action to a mnemonic


pressedKeys

private transient java.util.Set<java.lang.Integer> pressedKeys
a set of pressed keys to process


pressedButtons

private transient java.util.Set<KeyMap.Action> pressedButtons
a set of actions to perform


actionKey

protected java.util.Hashtable<KeyMap.Action,java.lang.Integer> actionKey
a mapping of actions to key codes


keyAction

protected java.util.Hashtable<java.lang.Integer,KeyMap.Action> keyAction
a mapping of key codes to actions

Constructor Detail

KeyMap

public KeyMap()
Constructor (default)


KeyMap

public KeyMap(KeyMap keyMap)
Constructor (copy)

Parameters:
keyMap - the keymap to copy
Method Detail

copyValues

public void copyValues(KeyMap destination,
                       KeyMap source)
Copy the source keymap to the destination keymap

Parameters:
destination - the destination keymap
source - the source keymap

reset

public void reset()
reset the keymap to its default values


getAction

public KeyMap.Action getAction(java.lang.Integer key)
Parameters:
key - the key associated with the action
Returns:
the action associated with the key

getKey

public static java.lang.Integer getKey(java.lang.String mnemonic)
Parameters:
mnemonic - the mnemonic associated with the keycode
Returns:
the key code associated with the mnemonic

getAction

public static KeyMap.Action getAction(java.lang.String mnemonic)
Parameters:
mnemonic - the mnemonic associated with the action
Returns:
the action associated with the mnemonic

getMnemonic

public static java.lang.String getMnemonic(KeyMap.Action action)
Parameters:
action - the action associated with the mnemonic
Returns:
the mnemonic associated with the action

putAction

public void putAction(java.lang.Integer key,
                      KeyMap.Action action)
Parameters:
key - the key code to associate with an action
action - the action that is associated to the key code

putMnemonicKey

public static void putMnemonicKey(java.lang.String mnemonic,
                                  int key)
Parameters:
mnemonic - the mnemonic to associate with an action
key - the key code that is associated to the key code

putMnemonicAction

public static void putMnemonicAction(java.lang.String mnemonic,
                                     KeyMap.Action action)
Parameters:
mnemonic - the mnemonic to associate with an action
action - the action that is associated to the key code

pressMouse

public void pressMouse(KeyMap.Action action)
Add an action to the list of mouse buttons that were pressed (for processing actions)

Parameters:
action - the action to perform

depressMouse

public void depressMouse(KeyMap.Action action)
Remove an action from the list of mouse buttons that were pressed (to stop processing the action)

Parameters:
action - the action that is not being performed anymore

pressKey

public void pressKey(java.lang.Integer key)
Add an action to the list of keys that were pressed (for processing actions)

Parameters:
key - the key code to process

depressKey

public boolean depressKey(java.lang.Integer key)
Remove an action from the list of keys that were pressed (to stop processing the action)

Parameters:
key - the action that is not being performed anymore
Returns:
whether the key was removed from the list of pressed keys/actions or not

pressedKeys

public java.util.Collection<java.lang.Integer> pressedKeys()
Returns:
a list of pressed keys

pressedButtons

public java.util.Collection<KeyMap.Action> pressedButtons()
Returns:
the list of pressed mouse buttons

getActions

public java.util.Collection<KeyMap.Action> getActions()
Returns:
the list of available actions

containsMnemonicKey

public static boolean containsMnemonicKey(java.lang.String mnemonic)
Parameters:
mnemonic - the mnemonic to look for
Returns:
if the mnemonic is in the mapping of mnemonics to keys

containsActionMnemonic

public static boolean containsActionMnemonic(KeyMap.Action action)
Parameters:
action - the mnemonic to look for
Returns:
if the mnemonic is in the mapping of action to mnemonics

loadMnemonic

void loadMnemonic(java.lang.String key,
                  java.lang.String data)
Store an action related to the mneonic in the key for the action in the data variables.

Parameters:
key - the mnemonic to look up
data - the mnemonic for an action

load

public boolean load(java.net.URL url)
Load a list of keys and actions

Parameters:
url - the url to load the key bindings from
Returns:
if the file was successfully loaded or not

save

public void save(java.net.URL url)
Save a list of keys and actions

Parameters:
url - the url to save the key bindings to

toString

public java.lang.String toString()
Provide a String representation of this object.

Overrides:
toString in class java.lang.Object
Returns:
String A representation of the object for debugging.