JAPI 0_6-alpha-2
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.

Author:
Christian Hujer
See Also:
Serialized Form

Field Summary
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)
           The implementation of this method first looks whether the Action is enabled.
protected  Object clone()
          
 void putValue(String key, Object newValue)
           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
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
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
Throws:
IllegalArgumentException - if newValue is of the wrong type

actionPerformed

public void actionPerformed(ActionEvent e)
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

clone

protected Object clone()
                throws CloneNotSupportedException

Overrides:
clone in class AbstractAction
Throws:
CloneNotSupportedException

JAPI
Yet another Java API
API Documentation

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