org.softsmithy.lib.swing.action
Class ReflectiveXAction

java.lang.Object
  extended by javax.swing.AbstractAction
      extended by org.softsmithy.lib.swing.action.AbstractXAction
          extended by org.softsmithy.lib.swing.action.ReflectiveXAction
All Implemented Interfaces:
ActionListener, Serializable, Cloneable, EventListener, Action, XAction
Direct Known Subclasses:
ReflectiveCustomizerAction

public class ReflectiveXAction
extends AbstractXAction

A trampoline class that reflectivly calls an action method. The method must take an ActionEvent object as its single parameter.

Normally there are two standard ways to implement the ActionListener interface: The first is to let a class implement the ActionListener interface and then add an object of this class to the addActionListener methods of the components firing ActionEvents. You then have to check the source in the actionPerformed method. You end up with a big ugly, unmaintainable and unstable if-then-else- statement. The second approach is to define a class for every such component. You end up with tons of small classes all increasing the memory footprint (typically 3K per class). Now this approach using reflection lets you define a method for each such component avoiding the mentioned drawbacks. The new drawbacks are however:

  1. Reflectivly calling method does not identify typos during compile-time.
  2. Calling a method refectivly is slower (but it's unlikely the user notices it).
More to read about reflective actions: Chapter 6.2.3, p. 73 of the Java Series Book Java Platform Performance Strategies and Tactics by Steve Wilson and Jeff Kesselman.

Note: This class might change package in a future version!
Note: This class might throw different Exceptions in a future version!

Author:
Florian Brunner
See Also:
Serialized Form

Field Summary
 
Fields inherited from class javax.swing.AbstractAction
changeSupport, enabled
 
Fields inherited from interface org.softsmithy.lib.swing.action.XAction
LARGE_DISABLED_ICON, LARGE_DISABLED_SELECTED_ICON, LARGE_ICON, LARGE_PRESSED_ICON, LARGE_ROLLOVER_ICON, LARGE_ROLLOVER_SELECTED_ICON, LARGE_SELECTED_ICON, SMALL_DISABLED_ICON, SMALL_DISABLED_SELECTED_ICON, SMALL_PRESSED_ICON, SMALL_ROLLOVER_ICON, SMALL_ROLLOVER_SELECTED_ICON, SMALL_SELECTED_ICON
 
Fields inherited from interface javax.swing.Action
ACCELERATOR_KEY, ACTION_COMMAND_KEY, DEFAULT, DISPLAYED_MNEMONIC_INDEX_KEY, LARGE_ICON_KEY, LONG_DESCRIPTION, MNEMONIC_KEY, NAME, SELECTED_KEY, SHORT_DESCRIPTION, SMALL_ICON
 
Constructor Summary
ReflectiveXAction(Object target, String methodName)
          Creates a new ReflectiveAction
 
Method Summary
 void actionPerformed(ActionEvent e)
          Invoked when an action occurs.
 
Methods inherited from class org.softsmithy.lib.swing.action.AbstractXAction
getAccelerator, getActionCommand, getLargeDisabledIcon, getLargeDisabledSelectedIcon, getLargeIcon, getLargePressedIcon, getLargeRolloverIcon, getLargeRolloverSelectedIcon, getLargeSelectedIcon, getLongDescription, getMnemonicKey, getName, getShortDescription, getSmallDisabledIcon, getSmallDisabledSelectedIcon, getSmallIcon, getSmallPressedIcon, getSmallRolloverIcon, getSmallRolloverSelectedIcon, getSmallSelectedIcon, setAccelerator, setActionCommand, setLargeDisabledIcon, setLargeDisabledSelectedIcon, setLargeIcon, setLargePressedIcon, setLargeRolloverIcon, setLargeRolloverSelectedIcon, setLargeSelectedIcon, setLongDescription, setMnemonicKey, setName, setShortDescription, setSmallDisabledIcon, setSmallDisabledSelectedIcon, setSmallIcon, setSmallPressedIcon, setSmallRolloverIcon, setSmallRolloverSelectedIcon, setSmallSelectedIcon
 
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
 
Methods inherited from interface javax.swing.Action
addPropertyChangeListener, getValue, isEnabled, putValue, removePropertyChangeListener, setEnabled
 

Constructor Detail

ReflectiveXAction

public ReflectiveXAction(Object target,
                         String methodName)
                  throws NoSuchMethodException
Creates a new ReflectiveAction

Parameters:
target - the object with the specified method
methodName - the method name (must be public and take an ActionEvent obect as its single parameter)
Throws:
NoSuchMethodException - if no such method found
Method Detail

actionPerformed

public void actionPerformed(ActionEvent e)
Invoked when an action occurs. Springs to the method specified in the constructor.

Parameters:
e - an ActionEvent object


Copyright © 2002-2011 SoftSmithy. All Rights Reserved.