/*
* ConfigAction.java
*
* Copyright (c) 2001-2002 Digi International
* This program and the information contained in it is confidential and
* proprietary to Digi International and may not be used, copied, or re-
* produced without the prior written permission of Digi International.
*
*/
package com.digi.config.ui;
import com.digi.config.util.SystemLog;
import com.digi.config.util.ConfigResource;
import java.awt.event.ActionEvent;
import java.io.Serializable;
import java.lang.reflect.Method;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.KeyStroke;
/**
* Defines Actions used in Config
*
* This type of action differs from AbstractAction in that it automatically
* defines the name, icon, tool tip, and accelerator key information by pulling
* it from the config resource bundles.
*
**/
public class ConfigAction extends AbstractAction {
String actionKey;
Object actionTarget;
String actionMethodName;
Method actionMethod;
/**
* Construct a new ConfigAction. The actionMethodName signature defaults to
* public void do+actionKey+Action() throws Exception
*
* @param actionKey Used to access the action name, icon, tool tip text,
* and accelerator from the config resource bundles.
*
The following resources are accessed:
*
ConfigUiResourceBundle:
*
* - actionKey+"Menu" - the name to display on menu's or buttons attached
* to this action
* - actionKey+"Tip" - the tool tip text to associate with controls
* attached to this action
* - actionKey+"Accelerator" - the accelerator key to use with this
* action
* - actionKey+"Mnemonic" - the mnemonic to use with this
* action
*
* ConfigImgResourceBundle:
*
* - actionKey+"Image" - the icon to associated with controls attached to
* this action
*
*
* @param actionTarget - the object to invoke against when the action is
* performed
*/
public ConfigAction(String actionKey, Object actionTarget)
throws Exception {
this(actionKey, actionTarget, "do" +actionKey+"Action");
}
/**
* Construct a new ConfigAction
*
* @param actionKey Used to access the action name, icon, tool tip text,
* and accelerator from the config resource bundles.
* The following resources are accessed:
*
ConfigUiResourceBundle:
*
* - actionKey+"Menu" - the name to display on menu's or buttons attached
* to this action
* - actionKey+"Tip" - the tool tip text to associate with controls
* attached to this action
* - actionKey+"Accelerator" - the accelerator key to use with this
* action
* - actionKey+"Mnemonic" - the mnemonic to use with this
* action
*
* ConfigImgResourceBundle:
*
* - actionKey+"Image" - the icon to associated with controls attached to
* this action
*
*
* @param actionTarget - the object to invoke against when the action is
* performed
* @param actionMethodName - the method to invoke when the action is
* performed. This must be a parameterless method that returns void.
*/
public ConfigAction(String actionKey, Object actionTarget,
String actionMethodName) throws Exception {
super();
this.actionKey=actionKey;
this.actionTarget=actionTarget;
this.actionMethodName=actionMethodName;
actionMethod=actionTarget.getClass().getMethod(actionMethodName, null);
// Define the Key attributes for any config action
this.putValue(Action.NAME,
ConfigResource.getUiRbString(actionKey+"Menu"));
String tip = ConfigResource.getUiRbString(actionKey+"Tip");
if (!tip.equals(actionKey+"Tip")) {
this.putValue(Action.SHORT_DESCRIPTION, tip);
}
String key = ConfigResource.getUiRbString(actionKey+"Accelerator");
if (!key.equals(actionKey+"Accelerator")) {
this.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(key));
}
String mnemonic = ConfigResource.getUiRbString(actionKey+"Mnemonic");
if (!mnemonic.equals(actionKey+"Mnemonic")) {
this.putValue(Action.MNEMONIC_KEY, new Integer(mnemonic.charAt(0)));
}
this.putValue(Action.SMALL_ICON,
ConfigResource.getImageRbIcon(actionKey+"Image"));
}
/**
* Use the action information to update a component with the appropriate
* decorations.
* Name, small icon, and mnemonic are used by Swing but others must be set
* @param component is the ui part that needs decorating
*/
public void addDecorations(javax.swing.JComponent component) {
String tip = (String)getValue(Action.SHORT_DESCRIPTION);
if (tip!=null) {
component.setToolTipText(tip);
}
}
/**
* Use the action information to update a component with the appropriate
* decorations.
* Name, small icon, and mnemonic are used by Swing but others must be set.
* Only menu items can have accelerators
* @param component is the ui part that needs decorating
*/
public void addDecorations(javax.swing.JMenuItem component) {
String tip = (String)getValue(Action.SHORT_DESCRIPTION);
if (tip!=null) {
component.setToolTipText(tip);
}
KeyStroke key = (KeyStroke)getValue(Action.ACCELERATOR_KEY);
if (key!=null) {
component.setAccelerator(key);
}
}
/**
* Use the action information to update a component with the appropriate
* decorations.
* Name, small icon, and mnemonic are used by Swing but others must be set
* Only menu items can have accelerators
* @param component is the ui part that needs decorating
* @param iconKey is which icon to use (Action.SMALL_ICON or null)
*
*/
public void addDecorations(javax.swing.JMenuItem component, String iconKey) {
if (iconKey==null) {
component.setIcon(null);
}
addDecorations(component);
}
/**
* If subclasses don't override this method, then try to perform the action method
* through introspection. The name of the invoked method will be the one specified
* on the ConfigAction constructor
*
*/
public void actionPerformed(ActionEvent e) {
String opName=(String)this.getValue(Action.NAME);
try {
actionMethod.invoke(actionTarget, null);
}
catch (Exception exc) {
SystemLog.log("Op1Fail",
new Serializable[] { opName}, exc);
}
//SystemLog.log("Op1Succ", new Serializable[] {opName});
}
}