@ThreadSafe
public class ActionMapper
extends java.lang.Object
Configurable prioritized mappings from user key and mouse actions to handlers.
An ActionMapper maintains zero or more ordered ActionMapper.ActionMap
s which
map action specs to ActionMapper.Action
s. The set of maps and the content
of each map are mutable, and maps and actions can be individually
disabled/enabled.
An ActionMapper can be added to zero or more AWT components, and itself can be enabled/disabled.
The only effect of disabling an ActionMapper.Action
, ActionMapper.ActionMap
, or
ActionMapper
is that corresponding ActionMapper.Action.run(java.lang.String, java.awt.Component, vona.ui.ActionMapper.ComponentState)
calls are not
made.
Normal action specs (see below for wildcard specs) are case-sensitive strings of the pattern
[S-][C-][A-]{ks|{1|2|3}-mc|[1-][2-][3-]mm|mw}where
KeyEvent.getKeyText()
for the
desired key code, converted to lower case and with spaces replaced by
underscoresKeyEvent.getKeyChar()
, in which case the S- prefix is not
allowed;There are three wildcard action specs:
any other key|*-mc|*-mmIf present, these match any keypress, mouse click, or mouse move action, respectively, that is not matched by any non-wildcard spec.
A user key or mouse action on any attached AWT component generates an
action spec which is looked up in each ActionMapper.ActionMap
in order. The
first enabled ActionMapper.Action
found, if any, is executed, in the AWT event
thread. ActionMapper.Action
s in earlier maps therefore take priority over those
with the same spec in later maps. This can be used to implement
e.g. layered input modes and submodes.
This class is designed to be thread safe:
ActionMapper.Action.run(java.lang.String, java.awt.Component, vona.ui.ActionMapper.ComponentState)
is always called from within the AWT event thread,
and the passed ActionMapper.ComponentState
s (from componentStateMap
) are
also confined to the AWT event thread.actionMaps
is internally synchronized.ActionMapper.ActionMap
are guarded by that map.ActionMapper.Action
is ActionMapper.Action.enabled
,
which is volatile
.RXSInteractor
), or wait for official bugfixRXSInteractor
to use ActionMapperCopyright (C) 2007 Marsette A. Vona, III
This program 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 of the License, or (at your option) any later version.
This program 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 this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Modifier and Type | Class and Description |
---|---|
static class |
ActionMapper.Action
An action.
|
static class |
ActionMapper.ActionMap
a map taking action specs to
ActionMapper.Action s |
static class |
ActionMapper.ApplyingAction
Generic
ActionMapper.Action impl that delegates to an object implementing an
apply(Object[]) method. |
static class |
ActionMapper.ComponentState
Collects the state of an AWT
Component . |
Modifier and Type | Field and Description |
---|---|
protected java.util.List<ActionMapper.ActionMap> |
actionMaps
The currently installed
ActionMapper.ActionMap s, in insertion order from
highest to lowest priority. |
protected java.awt.event.ComponentListener |
componentListener
listener for
ComponentEvent s |
protected java.util.Map<java.awt.Component,ActionMapper.ComponentState> |
componentStateMap
map from AWT
Component to ActionMapper.ComponentState |
private static java.lang.String |
cvsid |
static int |
DBG_ALL
|
static int |
DBG_COMPONENT_STATE
|
static int |
DBG_ENABLED
|
static int |
DBG_MOUSE_MOTION
|
int |
dbgFlags
enable for debug spew
|
boolean |
enabled
enable for this entire ActionMapper
|
static int |
ET_KEYPRESS
event type indices
|
static int |
ET_MOUSE_CLICK
event type indices
|
static int |
ET_MOUSE_MOTION
event type indices
|
static int |
ET_MOUSE_WHEEL
event type indices
|
static java.lang.String[] |
EVENT_TYPE_NAME
event type names indexed by event type
|
static java.util.Map<java.lang.Integer,java.lang.String> |
eventTypeFromID
mapping from AWT event ID to human readable event type
|
protected java.awt.event.FocusListener |
focusListener
listener for
FocusEvent s |
protected java.awt.event.KeyListener |
keyListener
listener for
KeyEvent s |
protected boolean[] |
mostRecentActionResult
The most recent action result for each event type.
|
protected java.lang.String[] |
mostRecentActionSpec
The most recently handled action spec for each event type, or null if
none.
|
protected java.lang.ref.WeakReference<java.awt.Component>[] |
mostRecentComponent
The most recently handled component for each event type, or null if
none.
|
protected int[] |
mostRecentEventID
The most recent event ID for each event type for each event type.
|
protected long[] |
mostRecentTimestamp
The most recent timestamp for each event type.
|
protected java.awt.event.MouseListener |
mouseListener
listener for
MouseEvent s |
protected java.awt.event.MouseMotionListener |
mouseMotionListener
listener for
MouseEvent s related to motion |
protected java.awt.event.MouseWheelListener |
mouseWheelListener
listener for
MouseWheelEvent s |
static int |
NUM_EVENT_TYPES
number of event types
|
static java.lang.String[] |
WILDCARD_SPEC
wildcard action specs indexed by event type
|
Constructor and Description |
---|
ActionMapper() |
Modifier and Type | Method and Description |
---|---|
boolean |
addComponent(java.awt.Component c)
Add an AWT
Component to the listen list. |
void |
dump()
covers
dumpActions() |
void |
dump(java.io.PrintStream s)
covers
dumpActions(PrintStream) |
void |
dumpActions()
Covers
dumpActions(PrintStream, String, boolean) , uses default
nomenclature, System.out , and sorts. |
void |
dumpActions(boolean sort)
Covers
dumpActions(PrintStream, String, boolean) , uses default
nomenclature and System.out . |
void |
dumpActions(java.io.PrintStream s)
Covers
dumpActions(PrintStream, String, boolean) , uses default
nomenclature and sorts. |
void |
dumpActions(java.io.PrintStream s,
boolean sort)
Covers
dumpActions(PrintStream, String, boolean) , uses default
nomenclature. |
void |
dumpActions(java.io.PrintStream s,
java.lang.String actionMapName,
boolean sort)
Dump all and only enabled
ActionMapper.Action s. |
void |
dumpActions(java.lang.String actionMapName)
Covers
dumpActions(PrintStream, String, boolean) , uses System.out and sorts. |
void |
dumpActions(java.lang.String actionMapName,
boolean sort)
Covers
dumpActions(PrintStream, String, boolean) , uses System.out . |
static java.lang.String |
getBaseSpec(java.lang.String spec)
extract the base spec
|
static int |
getEventType(java.lang.String spec)
get the event type for spec
|
protected static java.lang.String |
getKeyText(int code)
Fix
KeyEvent.getKeyText() on OS X. |
ActionMapper.ActionMap |
getMap(int index)
get the map at the specified index
|
boolean |
handleAction(java.lang.String baseSpec,
java.awt.Component c,
ActionMapper.ComponentState s,
int eventID,
long timestamp,
int eventType)
Covers
handleAction(String, Component, ComponentState,
int, long, int, boolean) , does not ignore shift down. |
boolean |
handleAction(java.lang.String baseSpec,
java.awt.Component c,
ActionMapper.ComponentState s,
int eventID,
long timestamp,
int eventType,
boolean ignoreShiftDown)
Add modifier specs to make an action spec from baseSpec,
lookupAction(java.lang.String, boolean) , and execute the ActionMapper.Action , if any. |
protected boolean |
handleKeyReleased(java.awt.Component c,
ActionMapper.ComponentState s,
int code,
int eventID,
long timestamp)
|
protected boolean |
handleKeyTyped(java.awt.Component c,
ActionMapper.ComponentState s,
char ch,
int eventID,
long timestamp)
|
protected boolean |
handleMouseClick(java.awt.Component c,
ActionMapper.ComponentState s,
int btn,
int eventID,
long timestamp)
|
protected boolean |
handleMouseMove(java.awt.Component c,
ActionMapper.ComponentState s,
int eventID,
long timestamp)
|
protected boolean |
handleMouseWheelMove(java.awt.Component c,
ActionMapper.ComponentState s,
int eventID,
long timestamp)
|
static boolean |
isWildcard(java.lang.String spec)
check if spec is a wildcard
|
ActionMapper.Action |
lookupAction(java.lang.String spec)
lookupAction(String, boolean) , use wildcards |
ActionMapper.Action |
lookupAction(java.lang.String spec,
boolean useWildcards)
Match an action spec to the corresponding
ActionMapper.Action . |
protected ActionMapper.ComponentState |
lookupComponentState(java.awt.Component c)
|
int |
numComponents()
get the current number of components
|
int |
numMaps()
get the current number of maps
|
ActionMapper.ActionMap |
popMap()
Remove the first (i.e.
|
void |
pushMap(ActionMapper.ActionMap m)
Add an
ActionMapper.ActionMap as the first (i.e. |
boolean |
removeComponent(java.awt.Component c)
Remove an AWT
Component from the listen list. |
private static final java.lang.String cvsid
public static final int DBG_ENABLED
public static final int DBG_MOUSE_MOTION
public static final int DBG_COMPONENT_STATE
public static final int DBG_ALL
public volatile int dbgFlags
public volatile boolean enabled
public static final int ET_KEYPRESS
public static final int ET_MOUSE_MOTION
public static final int ET_MOUSE_CLICK
public static final int ET_MOUSE_WHEEL
public static final int NUM_EVENT_TYPES
public static final java.lang.String[] EVENT_TYPE_NAME
public static final java.lang.String[] WILDCARD_SPEC
public static final java.util.Map<java.lang.Integer,java.lang.String> eventTypeFromID
protected java.lang.ref.WeakReference<java.awt.Component>[] mostRecentComponent
The most recently handled component for each event type, or null if none.
Confined to the GUI event thread.
protected java.lang.String[] mostRecentActionSpec
The most recently handled action spec for each event type, or null if none.
Confined to the GUI event thread.
protected int[] mostRecentEventID
The most recent event ID for each event type for each event type.
Confined to the GUI event thread.
protected long[] mostRecentTimestamp
The most recent timestamp for each event type.
Confined to the GUI event thread.
protected boolean[] mostRecentActionResult
The most recent action result for each event type.
Confined to the GUI event thread.
protected final java.util.List<ActionMapper.ActionMap> actionMaps
The currently installed ActionMapper.ActionMap
s, in insertion order from
highest to lowest priority.
protected final java.util.Map<java.awt.Component,ActionMapper.ComponentState> componentStateMap
Component
to ActionMapper.ComponentState
protected final java.awt.event.ComponentListener componentListener
ComponentEvent
sprotected final java.awt.event.FocusListener focusListener
FocusEvent
sprotected final java.awt.event.KeyListener keyListener
KeyEvent
sprotected final java.awt.event.MouseListener mouseListener
MouseEvent
sprotected final java.awt.event.MouseMotionListener mouseMotionListener
MouseEvent
s related to motionprotected final java.awt.event.MouseWheelListener mouseWheelListener
MouseWheelEvent
spublic boolean addComponent(java.awt.Component c)
Add an AWT Component
to the listen list.
public boolean removeComponent(java.awt.Component c)
Remove an AWT Component
from the listen list.
public void pushMap(ActionMapper.ActionMap m)
Add an ActionMapper.ActionMap
as the first (i.e. highest priority) map in
the list.
public ActionMapper.ActionMap popMap()
Remove the first (i.e. highest priority) ActionMapper.ActionMap
, if any.
public int numMaps()
public ActionMapper.ActionMap getMap(int index)
public int numComponents()
public ActionMapper.Action lookupAction(java.lang.String spec, boolean useWildcards)
Match an action spec to the corresponding ActionMapper.Action
.
See class header doc for detailed semantics.
public ActionMapper.Action lookupAction(java.lang.String spec)
lookupAction(String, boolean)
, use wildcardspublic void dumpActions(java.io.PrintStream s, java.lang.String actionMapName, boolean sort)
Dump all and only enabled ActionMapper.Action
s.
An ActionMapper.Action
is enabled only if its own enable flag, the enable
flag of its enclosing ActionMapper.ActionMap
, and the enabled
flag of
this ActionMapper are all set.
If more than one ActionMapper.Action
is enabled for a given spec then only
the highest-priority ActionMapper.Action
is dumped.
actionMapName
- if non-null then this is the application-specific
nomenclature for an ActionMapper.ActionMap
(e.g. "mode"); if null then the
default nomenclature "action map" is usedsort
- whether to sort the actions by insertion order (sort =
false
) or using ActionMapper.Action.COMPARATOR
(sort = true
)public void dumpActions(java.io.PrintStream s, boolean sort)
Covers dumpActions(PrintStream, String, boolean)
, uses default
nomenclature.
public void dumpActions(java.io.PrintStream s)
Covers dumpActions(PrintStream, String, boolean)
, uses default
nomenclature and sorts.
public void dump(java.io.PrintStream s)
dumpActions(PrintStream)
public void dumpActions(java.lang.String actionMapName, boolean sort)
Covers dumpActions(PrintStream, String, boolean)
, uses System.out
.
public void dumpActions(java.lang.String actionMapName)
Covers dumpActions(PrintStream, String, boolean)
, uses System.out
and sorts.
public void dumpActions(boolean sort)
Covers dumpActions(PrintStream, String, boolean)
, uses default
nomenclature and System.out
.
public void dumpActions()
Covers dumpActions(PrintStream, String, boolean)
, uses default
nomenclature, System.out
, and sorts.
public void dump()
dumpActions()
protected ActionMapper.ComponentState lookupComponentState(java.awt.Component c)
Fetch the ActionMapper.ComponentState
for AWT Component
c
from componentStateMap
.
Issues a warning if the state is not found.
Sets the component dimensions if unset.
ActionMapper.ComponentState
corresponding to c or null if
not foundprotected static java.lang.String getKeyText(int code)
Fix KeyEvent.getKeyText()
on OS X.
protected boolean handleKeyReleased(java.awt.Component c, ActionMapper.ComponentState s, int code, int eventID, long timestamp)
protected boolean handleKeyTyped(java.awt.Component c, ActionMapper.ComponentState s, char ch, int eventID, long timestamp)
protected boolean handleMouseClick(java.awt.Component c, ActionMapper.ComponentState s, int btn, int eventID, long timestamp)
protected boolean handleMouseMove(java.awt.Component c, ActionMapper.ComponentState s, int eventID, long timestamp)
protected boolean handleMouseWheelMove(java.awt.Component c, ActionMapper.ComponentState s, int eventID, long timestamp)
public boolean handleAction(java.lang.String baseSpec, java.awt.Component c, ActionMapper.ComponentState s, int eventID, long timestamp, int eventType, boolean ignoreShiftDown)
Add modifier specs to make an action spec from baseSpec, lookupAction(java.lang.String, boolean)
, and execute the ActionMapper.Action
, if any.
ActionMapper.Action.run(java.lang.String, java.awt.Component, vona.ui.ActionMapper.ComponentState)
returns true and ActionMapper.Action.consuming
. false if no ActionMapper.Action
was found, ActionMapper.Action.run(java.lang.String, java.awt.Component, vona.ui.ActionMapper.ComponentState)
returned false, ActionMapper.Action.consuming
false, or if the
action spec is a repeat of the most recent one but with a different event
ID (this avoids duplication in cases where a single physical action
triggers multiple events, such as a keypress that triggers both
KeyReleased and KeyTyped events)public boolean handleAction(java.lang.String baseSpec, java.awt.Component c, ActionMapper.ComponentState s, int eventID, long timestamp, int eventType)
Covers handleAction(String, Component, ComponentState,
int, long, int, boolean)
, does not ignore shift down.
public static java.lang.String getBaseSpec(java.lang.String spec)
public static boolean isWildcard(java.lang.String spec)
public static int getEventType(java.lang.String spec)