com.gargoylesoftware.base.gui
Class ReflectedAction

java.lang.Object
  extended byjavax.swing.AbstractAction
      extended bycom.gargoylesoftware.base.gui.ReflectedAction
All Implemented Interfaces:
Action, ActionListener, Cloneable, EventListener, Serializable

public class ReflectedAction
extends AbstractAction

A swing "action" that uses reflection to find and invoke a given target method.

ReflectedAction is a Swing "action" that uses reflection to call a specific target method when the action is fired. Actions are often implemented using inner classes which can be very inefficient in terms of performance and deployment size. This action is a tiny bit slower when it is actually fired but is smaller and generally faster otherwise due to the fact that those inner classes are no longer needed.

 Action searchAction = new ReflectedAction(this, "performSearch");
 

Version:
$Revision: 1.5 $
Author:
Mike Bowler
See Also:
Serialized Form

Field Summary
 
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
ReflectedAction(Object object, String methodName)
          Create a reflected action with an object and the name of the method that will be invoked on that object when the action is fired.
 
Method Summary
 void actionPerformed(ActionEvent event)
          The action has been fired so now we use reflection to invoke the method that was specified previously.
protected  void assertNotNull(String fieldName, Object object)
          Throw an exception if the specified object is null
protected  void exceptionThrown(Exception exception)
          Callback that will be invoked if an exception is thrown during the reflected method call.
 
Methods inherited from class javax.swing.AbstractAction
addPropertyChangeListener, clone, firePropertyChange, getKeys, getPropertyChangeListeners, getValue, isEnabled, putValue, removePropertyChangeListener, setEnabled
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReflectedAction

public ReflectedAction(Object object,
                       String methodName)
                throws IllegalArgumentException
Create a reflected action with an object and the name of the method that will be invoked on that object when the action is fired.

Parameters:
object - The object upon which we will invoke a method when the action is fired
methodName - The name of the method that we will invoke. This method must take either no parameters or one parameter which is an ActionEvent.
Throws:
IllegalArgumentException - if there are no matching methods.
Method Detail

actionPerformed

public void actionPerformed(ActionEvent event)
The action has been fired so now we use reflection to invoke the method that was specified previously.

Parameters:
event - The ActionEvent

exceptionThrown

protected void exceptionThrown(Exception exception)
Callback that will be invoked if an exception is thrown during the reflected method call.

Override this to provide custom error handling. Default behaviour is to print a stack trace.

Parameters:
exception - The exception

assertNotNull

protected final void assertNotNull(String fieldName,
                                   Object object)
Throw an exception if the specified object is null

Parameters:
fieldName - The name of the paremeter we are checking
object - The value of the parameter we are checking


Copyright © 1998-2005 Gargoyle Software Inc.. All Rights Reserved.