JAPI 0.9.0
Yet another Java API
API Documentation

net.sf.japi.swing
Class ReflectionAction

java.lang.Object
  extended by javax.swing.AbstractAction
      extended by net.sf.japi.swing.ReflectionAction
All Implemented Interfaces:
ActionListener, Serializable, Cloneable, EventListener, Action

public final class ReflectionAction
extends AbstractAction

Action implementation which invokes the desired method using Reflection. Usage example:

  class SomeClass {
      SomeClass() {
          ReflectionAction action = new ReflectionAction();
          action.putValue(REFLECTION_TARGET, this);
          action.putValue(REFLECTION_METHOD_NAME, "myAction");
          new JMenuItem(action);
      }
      void myAction() {
          // do something
      }
  }
 
Note that because of Reflection this Action is slightly slower than implementing your own Action instance, but in most cases this really does not matter at all. Usually you won't use ReflectionAction yourself. Instead you'll let ActionFactory create an instance for you.

Author:
Christian Hujer
See Also:
Serialized Form
Todo:
ReflectionAction should be able to invoke methods with parameters, three variants: Object..., ActionEvent or void

Field Summary
static String REFLECTION_MESSAGE_PROVIDER
          The key used for storing the action factory that provides access to error handlers strings.
static String REFLECTION_METHOD
          The key used for storing the method object to use when invoking the method.
static String REFLECTION_METHOD_NAME
          The key used for storing the method name to use when searching for a method using reflection.
static String REFLECTION_TARGET
          The key used for storing the target object to invoke the method on.
 
Fields inherited from class javax.swing.AbstractAction
changeSupport, enabled
 
Fields inherited from interface javax.swing.Action
ACCELERATOR_KEY, ACTION_COMMAND_KEY, DEFAULT, LONG_DESCRIPTION, MNEMONIC_KEY, NAME, SHORT_DESCRIPTION, SMALL_ICON
 
Constructor Summary
ReflectionAction()
          Create an uninitialized ReflectionAction.
ReflectionAction(String name, Icon icon, String methodName, Object target)
          Defines an Action object with the specified description string and a the specified icon.
ReflectionAction(String methodName, Object target)
          Create a ReflectionAction with method and target.
 
Method Summary
 void actionPerformed(ActionEvent e)
          Invoked when an action occurs. The implementation of this method first looks whether the Action is enabled.
protected  Object clone()
          Clones the abstract action.
 void putValue(String key, Object newValue)
          Sets the Value associated with the specified key. This implementation checks the type of newValue if the key is REFLECTION_METHOD_NAME or REFLECTION_METHOD, so you'll know of errors quite soon.
 
Methods inherited from class javax.swing.AbstractAction
addPropertyChangeListener, firePropertyChange, getKeys, getPropertyChangeListeners, getValue, isEnabled, removePropertyChangeListener, setEnabled
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REFLECTION_TARGET

public static final String REFLECTION_TARGET
The key used for storing the target object to invoke the method on. Value Type: Object.

See Also:
Constant Field Values

REFLECTION_METHOD_NAME

public static final String REFLECTION_METHOD_NAME
The key used for storing the method name to use when searching for a method using reflection. Value Type: String (checked).

See Also:
Constant Field Values

REFLECTION_METHOD

public static final String REFLECTION_METHOD
The key used for storing the method object to use when invoking the method. Value Type: Method (checked).

See Also:
Constant Field Values

REFLECTION_MESSAGE_PROVIDER

public static final String REFLECTION_MESSAGE_PROVIDER
The key used for storing the action factory that provides access to error handlers strings.

See Also:
Constant Field Values
Constructor Detail

ReflectionAction

public ReflectionAction()
Create an uninitialized ReflectionAction.


ReflectionAction

public ReflectionAction(String methodName,
                        Object target)
Create a ReflectionAction with method and target.

Parameters:
methodName - Name of method to invoke
target - Target object to invoke method at

ReflectionAction

public ReflectionAction(String name,
                        Icon icon,
                        String methodName,
                        Object target)
Defines an Action object with the specified description string and a the specified icon.

Parameters:
name - description string
icon - icon
methodName - Name of method to invoke
target - Target object to invoke method at
Method Detail

putValue

public void putValue(String key,
                     Object newValue)
              throws IllegalArgumentException
Sets the Value associated with the specified key. This implementation checks the type of newValue if the key is REFLECTION_METHOD_NAME or REFLECTION_METHOD, so you'll know of errors quite soon.

Specified by:
putValue in interface Action
Overrides:
putValue in class AbstractAction
Parameters:
key - the String that identifies the stored object
newValue - the Object to store using this key
Throws:
IllegalArgumentException - if newValue is of the wrong type
See Also:
Action.putValue(java.lang.String, java.lang.Object)

actionPerformed

public void actionPerformed(ActionEvent e)
Invoked when an action occurs. The implementation of this method first looks whether the Action is enabled. If it isn't, the method simply returns. Otherwise, instance and method are looked up. If both are null, the method again returns. If the method is null, it is reflected upon the instance usign the method name. If the method name is null, the method returns. Finally the method is invoked upon the instance, which may be null for static methods.

Throws:
RuntimeException - with cause in case the invocation of the method threw an exception and there was no handler for that exception.

clone

protected Object clone()
                throws CloneNotSupportedException
Clones the abstract action. This gives the clone its own copy of the key/value list, which is not handled for you by Object.clone().

Overrides:
clone in class AbstractAction
Returns:
a clone of this instance.
Throws:
CloneNotSupportedException - if the object's class does not support the Cloneable interface. Subclasses that override the clone method can also throw this exception to indicate that an instance cannot be cloned.
See Also:
Cloneable

JAPI
Yet another Java API
API Documentation

© 2005-2006 Christian Hujer. All rights reserved. See copyright