public class KeyBindingManager
extends java.lang.Object
KeyListener
so it can be added as a key listener
to any component.
The inputMap uses KeyStrokes
as it's keys, and stores Objects
of type Set. The Set contains Strings and the set will guarantee they are not repetead.
Each String is then used as a key into the HashMap actionMap to retrieve a KeyBindings object. Each key bindings object has a list of actions which can then be performed.
This model is similar to jawa.swing.InputMap and java.swing.ActionMap. However, secondary InputMaps allow two-stroke key bindings. Additionally, the KeybindingManager has been enveloped in an object which can then be inserted into the event hierarchy in different ways, instead of having to set a Component's InputMap and ActionMap.
Two-stroke bindings:
The KeyBindingManager has a HashMap prefixedInputMapMaps. A prefixStroke is used as a key to this table to obtain an inputMap (HashMap) based on the prefixStroke. From here it is the same as before with the inputMap and actionMap: A KeyStroke is then used as a key to find a List of Strings. The Strings are then used as a key into actionMap to get a KeyBindings object and perform the associated action. There is only one actionMap.
Modifier and Type | Class and Description |
---|---|
static class |
KeyBindingManager.KeyMaps
Class that converts internal key mappings to InputMap and ActionMap objects.
|
Constructor and Description |
---|
KeyBindingManager(java.lang.String prefPrefix,
java.util.prefs.Preferences prefs)
Construct a new KeyBindingManager that can act as a KeyListener
on a Component.
|
Modifier and Type | Method and Description |
---|---|
void |
addActionListener(java.lang.String actionDesc,
java.awt.event.ActionListener action)
Add an action listener on actionDesc
|
void |
addDefaultKeyBinding(java.lang.String actionDesc,
KeyStrokePair pair)
Adds a default KeyBinding.
|
void |
addUserKeyBinding(java.lang.String actionDesc,
KeyStrokePair pair)
Adds a user specified KeyBindings.
|
void |
deleteEmptyBindings()
Check if there are any bindings that do not have any
associated actions.
|
void |
finished()
Called when disposing of this manager, allows memory used to
be reclaimed by removing static references to this.
|
java.util.List<KeyBindings> |
getConflictingKeyBindings(KeyStrokePair pair)
Get a list of KeyBindings that conflict with the key combo
prefixStroke, stroke . |
static java.util.List<KeyBindings> |
getConflictsAllManagers(KeyStrokePair pair)
Get a list of conflicting key bindings from all KeyBindingManagers.
|
boolean |
getEnabled(java.lang.String actionDesc)
Get the enabled state of the action described by 'actionDesc'.
|
KeyBindings |
getKeyBindings(java.lang.String actionDesc)
Get bindings for action string
|
KeyBindingManager.KeyMaps |
getKeyMaps()
Method to return an object that has real InputMap and ActionMap objects.
|
void |
printKeyBindings()
Method to print existing KeyStrokes in std output for help
|
boolean |
processKeyEvent(java.awt.event.KeyEvent e)
Process a KeyEvent by finding what actionListeners should be
activated as a result of the event.
|
void |
removeKeyBinding(java.lang.String actionDesc,
KeyStrokePair k)
Removes a key binding from the active bindings, and writes new bindings
set to preferences.
|
void |
resetKeyBindings(java.lang.String actionDesc)
Set
actionDesc |
void |
restoreSavedBindings(boolean initialCall)
Restored saved bindings from preferences.
|
void |
setEnabled(java.lang.String actionDesc,
boolean b)
Sets the enabled state of the action to 'b'.
|
void |
setEventSource(java.lang.String actionDesc,
java.lang.Object source)
Set the faked event source of the KeyBindings object.
|
static boolean |
validKeyEvent(java.awt.event.KeyEvent e)
Says whether or not KeyBindingManager will bind to this key event
|
public KeyBindingManager(java.lang.String prefPrefix, java.util.prefs.Preferences prefs)
public void finished()
public void printKeyBindings()
public static boolean validKeyEvent(java.awt.event.KeyEvent e)
e
- the KeyEventpublic boolean processKeyEvent(java.awt.event.KeyEvent e)
e
- the KeyEventpublic static java.util.List<KeyBindings> getConflictsAllManagers(KeyStrokePair pair)
pair
- the keystrokepairpublic void addDefaultKeyBinding(java.lang.String actionDesc, KeyStrokePair pair)
k.actionDesc
, those are used instead. Note that k
cannot be null, but it's stroke and prefixStroke can be null. However,
it's actionDesc and action must be valid.actionDesc
- the action descriptionpair
- a key stroke pairpublic void addUserKeyBinding(java.lang.String actionDesc, KeyStrokePair pair)
actionDesc
- the action descriptionpair
- a key stroke pairpublic void addActionListener(java.lang.String actionDesc, java.awt.event.ActionListener action)
actionDesc
- the action descriptionaction
- the action listener to addpublic void removeKeyBinding(java.lang.String actionDesc, KeyStrokePair k)
actionDesc
- the describing actionk
- the KeyStrokePair to removepublic void resetKeyBindings(java.lang.String actionDesc)
actionDesc to use default KeyBindings
actionDesc
- the action descriptionpublic KeyBindings getKeyBindings(java.lang.String actionDesc)
actionDesc
- string describing action (KeyBinding.actionDesc)public KeyBindingManager.KeyMaps getKeyMaps()
public void setEventSource(java.lang.String actionDesc, java.lang.Object source)
actionDesc
- the action description used to find the KeyBindings object.source
- the object to use as the source of the event. (Event.getSource()).public java.util.List<KeyBindings> getConflictingKeyBindings(KeyStrokePair pair)
prefixStroke, stroke
. A conflict is registered if:
an existing stroke is the same as prefixStroke
; or
an existing stroke is the same stroke
(if prefixStroke
is null);
or an existing prefixStroke,stroke combo is the same as
prefixStroke,stroke
.
The returned list consists of newly created KeyBindings objects, not KeyBindings objects that are used in the key manager database. This is because not all KeyStrokePairs in an existing KeyBindings object will necessarily conflict. However, there may be more than one KeyStrokePair in a returned KeyBindings object from the list if more than one KeyStrokePair does actually conflict.
Returns an empty list if there are no conflicts.
pair
- the KeyStrokePairKeyBindings
. Empty list if no conflicts.public void setEnabled(java.lang.String actionDesc, boolean b)
actionDesc
- the describing actionb
- true to enable, false to disable.public boolean getEnabled(java.lang.String actionDesc)
actionDesc
- the describing action.public void deleteEmptyBindings()
public void restoreSavedBindings(boolean initialCall)