public final class Accelerator extends Object
A class that represents an accelerator for invoking an action.
An accelerator is a key (or a combination of keys, typically associated with
some modifier keys like SHIFT or ALT) that triggers an action when pressed.
Thus it is a keyboard short cut that has the same effect as clicking a tool
bar button or selecting a menu element. When a FormAction
is created
(defined by an ActionData
object) an accelerator can be specified.
This can have effect on GUI elements associated with this action. For
instance, menus will typically display the keyboard combinations that
correspond to the menu elements.
This class has the same purpose as javax.swing.KeyStroke
:
serving as an abstract description of a combination of key presses. However,
it is more tailored towards the builder approach followed by this library.
This means that the main use case of this class is being created indirectly
in a builder script (mostly using a text representation) and then being
passed to an implementation of ActionManager
. A concrete
ActionManager
implementation is responsible for converting a
generic Accelerator
object into a platform-specific
representation of a key stroke.
Instances of this class store a set of modifiers (like SHIFT or CONTROL) that must be pressed together with the key. The actual key can be specified in the following different ways:
getKey()
method, which returns a Character
.getSpecialKey()
method will return a non-null value.VK_XXX
constants
of the KeyEvent
class). If such a code is set, it can be queried
using the getKeyCode()
method. Note that this variant is not
portable.Implementation note: Instances of this class are immutable and can be shared among multiple threads.
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object obj)
Compares this object with another one.
|
static Accelerator |
getInstance(Character key,
Set<Modifiers> modifiers)
Returns an
Accelerator for the specified printable key and
the (optional) modifiers. |
static Accelerator |
getInstance(Integer keyCode,
Set<Modifiers> modifiers)
Returns an
Accelerator for the specified platform-specific
key code and the (optional) modifiers. |
static Accelerator |
getInstance(Keys key,
Set<Modifiers> modifiers)
Returns an
Accelerator for the specified special key and the
(optional) modifiers. |
Character |
getKey()
Returns the character.
|
Integer |
getKeyCode()
Returns the key code.
|
Set<Modifiers> |
getModifiers()
Returns a set with the modifiers set for this accelerator.
|
Keys |
getSpecialKey()
Returns the special key.
|
int |
hashCode()
Returns a hash code for this object.
|
static Accelerator |
parse(String s)
Returns an
Accelerator instance from the specified string
representation. |
String |
toString()
Returns a string representation of this object.
|
public Set<Modifiers> getModifiers()
public Keys getSpecialKey()
public Character getKey()
public Integer getKeyCode()
public String toString()
parse(String)
method, i.e. they can be used for creating Accelerator
instances. They are normalized in the following way:
enum
constants are declared and happens to be
alphabetic order).public boolean equals(Object obj)
public int hashCode()
public static Accelerator getInstance(Keys key, Set<Modifiers> modifiers)
Accelerator
for the specified special key and the
(optional) modifiers.key
- the special key for this accelerator (must not be null)modifiers
- a set with modifiers (can be null)Accelerator
instanceIllegalArgumentException
- if the key is undefinedpublic static Accelerator getInstance(Character key, Set<Modifiers> modifiers)
Accelerator
for the specified printable key and
the (optional) modifiers.key
- the character for this accelerator (must not be null)modifiers
- a set with modifiers (can be null)Accelerator
instanceIllegalArgumentException
- if the key is undefinedpublic static Accelerator getInstance(Integer keyCode, Set<Modifiers> modifiers)
Accelerator
for the specified platform-specific
key code and the (optional) modifiers.keyCode
- the special key code for this accelerator (must not be
null)modifiers
- a set with modifiers (can be null)Accelerator
instanceIllegalArgumentException
- if the key is undefinedpublic static Accelerator parse(String s)
Returns an Accelerator
instance from the specified string
representation. The string must be a valid representation of an
Accelerator
instance as it is returned by the
toString()
method. Otherwise an
IllegalArgumentException
exception will be thrown.
Valid strings have the following form:
acceleratorString ::= (<modifier>)* keySpec keySpec ::= <character> | <specialKey> | <keyCode>With other words: The string can contain multiple components all separated by whitespace. The last component defines the actual keys, all others are interpreted as modifiers. They must conform to literals of the
Modifiers
enumeration (case does not matter). For the last
component, there are the following possibilities:
Keys
enumeration, the
corresponding special key is set (the comparison is not case sensitive).Here are some examples:
"A"
control A
Shift CONTROL A
Alt Backspace
F1
42
CONTROL 5
CONTROL 05
If the whole string is null or empty, null is returned.
s
- the string to be parsed (can be null)Accelerator
instanceIllegalArgumentException
- if the string cannot be parsedCopyright © 2016 The JGUIraffe Team. All rights reserved.