de.matthiasmann.twl
Class ActionMap

java.lang.Object
  extended by de.matthiasmann.twl.ActionMap

public class ActionMap
extends java.lang.Object

The action map class implements mappings from action names to methods using reflection.


Nested Class Summary
static interface ActionMap.Action
          Annotation used for automatic handler registration
 
Field Summary
static int FLAG_ON_PRESSED
          Invocation flag Invoke the method on the first key pressed event.
static int FLAG_ON_RELEASE
          Invocation flag Invoke the method on a key release event.
static int FLAG_ON_REPEAT
          Invocation flag Invoke the method also on a repeated key pressed event.
 
Constructor Summary
ActionMap()
           
 
Method Summary
 void addMapping(java.lang.Object target)
          Add action mapping for all public methods of the specified class which are annotated with the Action annotation.
 void addMapping(java.lang.String action, java.lang.Class<?> targetClass, java.lang.String methodName, java.lang.Object[] params, int flags)
          Add an action mapping for the specified action to the given public static method.
 void addMapping(java.lang.String action, java.lang.Object target, java.lang.reflect.Method method, java.lang.Object[] params, int flags)
          Add an action mapping for the specified action to the given method.
 void addMapping(java.lang.String action, java.lang.Object target, java.lang.String methodName, java.lang.Object[] params, int flags)
          Add an action mapping for the specified action to the given public instance method.
protected  void addMappingImpl(java.lang.String action, java.lang.Object target, java.lang.reflect.Method method, java.lang.Object[] params, int flags)
           
 boolean invoke(java.lang.String action, Event event)
          Invoke the mapping for the given action if one is defined and it's flags match the passed event.
 boolean invokeDirect(java.lang.String action)
          Invoke the mapping for the given action if one is defined without checking any flags.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FLAG_ON_PRESSED

public static final int FLAG_ON_PRESSED
Invocation flag Invoke the method on the first key pressed event.

See Also:
addMapping(java.lang.String, java.lang.Object, java.lang.reflect.Method, java.lang.Object[], int), FLAG_ON_REPEAT, Constant Field Values

FLAG_ON_RELEASE

public static final int FLAG_ON_RELEASE
Invocation flag Invoke the method on a key release event.

See Also:
addMapping(java.lang.String, java.lang.Object, java.lang.reflect.Method, java.lang.Object[], int), Constant Field Values

FLAG_ON_REPEAT

public static final int FLAG_ON_REPEAT
Invocation flag Invoke the method also on a repeated key pressed event.

See Also:
addMapping(java.lang.String, java.lang.Object, java.lang.reflect.Method, java.lang.Object[], int), FLAG_ON_PRESSED, Constant Field Values
Constructor Detail

ActionMap

public ActionMap()
Method Detail

invoke

public boolean invoke(java.lang.String action,
                      Event event)
Invoke the mapping for the given action if one is defined and it's flags match the passed event.

Parameters:
action - the action name
event - the event which caused the invocation
Returns:
true if a mapping was found, false if no mapping was found.
Throws:
java.lang.NullPointerException - when either action or event is null
See Also:
addMapping(java.lang.String, java.lang.Object, java.lang.reflect.Method, java.lang.Object[], int)

invokeDirect

public boolean invokeDirect(java.lang.String action)
Invoke the mapping for the given action if one is defined without checking any flags.

Parameters:
action - the action name
Returns:
true if a mapping was found, false if no mapping was found.
Throws:
java.lang.NullPointerException - when action is null
See Also:
addMapping(java.lang.String, java.lang.Object, java.lang.reflect.Method, java.lang.Object[], int)

addMapping

public void addMapping(java.lang.String action,
                       java.lang.Object target,
                       java.lang.String methodName,
                       java.lang.Object[] params,
                       int flags)
                throws java.lang.IllegalArgumentException
Add an action mapping for the specified action to the given public instance method. Parameters can be passed to the method to differentiate between different actions using the same handler method. NOTE: if multiple methods are compatible to the given parameters then it's undefined which method will be selected. No overload resolution is performed besides a simple parameter compatibility check.

Parameters:
action - the action name
target - the target object
methodName - the method name
params - parameters passed to the method
flags - flags to control on which events the method should be invoked
Throws:
java.lang.IllegalArgumentException - if no matching method was found
java.lang.NullPointerException - when action, target or params is null
See Also:
ClassUtils.isParamsCompatible(java.lang.Class[], java.lang.Object[]), FLAG_ON_PRESSED, FLAG_ON_RELEASE, FLAG_ON_REPEAT

addMapping

public void addMapping(java.lang.String action,
                       java.lang.Class<?> targetClass,
                       java.lang.String methodName,
                       java.lang.Object[] params,
                       int flags)
                throws java.lang.IllegalArgumentException
Add an action mapping for the specified action to the given public static method. Parameters can be passed to the method to differentiate between different actions using the same handler method. NOTE: if multiple methods are compatible to the given parameters then it's undefined which method will be selected. No overload resolution is performed besides a simple parameter compatibility check.

Parameters:
action - the action name
targetClass - the target class
methodName - the method name
params - parameters passed to the method
flags - flags to control on which events the method should be invoked
Throws:
java.lang.NullPointerException - when action, targetClass or params is null
java.lang.IllegalArgumentException - if no matching method was found
See Also:
ClassUtils.isParamsCompatible(java.lang.Class[], java.lang.Object[]), FLAG_ON_PRESSED, FLAG_ON_RELEASE, FLAG_ON_REPEAT

addMapping

public void addMapping(java.lang.String action,
                       java.lang.Object target,
                       java.lang.reflect.Method method,
                       java.lang.Object[] params,
                       int flags)
Add an action mapping for the specified action to the given method. Parameters can be passed to the method to differentiate between different actions using the same handler method.

Parameters:
action - the action name
target - the target object. Can be null when the method is static
method - the method to invoke
params - the parameters to pass to the method
flags - flags to control on which events the method should be invoked
Throws:
java.lang.NullPointerException - when action, method or params is null
java.lang.IllegalArgumentException -
  • when the method is not public
  • when the method does not belong to the target object
  • when the parameters do not match the arguments
See Also:
ClassUtils.isParamsCompatible(java.lang.Class[], java.lang.Object[]), FLAG_ON_PRESSED, FLAG_ON_RELEASE, FLAG_ON_REPEAT

addMapping

public void addMapping(java.lang.Object target)
Add action mapping for all public methods of the specified class which are annotated with the Action annotation.

Parameters:
target - the target class
See Also:
ActionMap.Action

addMappingImpl

protected void addMappingImpl(java.lang.String action,
                              java.lang.Object target,
                              java.lang.reflect.Method method,
                              java.lang.Object[] params,
                              int flags)