001: /*
002: * BaseActionCommand.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing.actions;
023:
024: import java.awt.event.ActionEvent;
025:
026: import javax.swing.AbstractAction;
027: import javax.swing.Icon;
028: import javax.swing.SwingUtilities;
029:
030: /* ----------------------------------------------------------
031: * CVS NOTE: Changes to the CVS repository prior to the
032: * release of version 3.0.0beta1 has meant a
033: * resetting of CVS revision numbers.
034: * ----------------------------------------------------------
035: */
036:
037: /** <p>This is the base class for the action library. All
038: * actions will inherit from this class. The CommandAction
039: * defines a generic implementation of actionPerformed.
040: * Here actionPerformed simply calls the execute method
041: * on its command object.<br>
042: *
043: * A developer can use this type directly by passing in
044: * the command, string, and action. However, convenience
045: * implementations are available that already provide the
046: * string and icon. The developer simply needs to provide
047: * the proper command.<br>
048: *
049: * @author Takis Diakoumis
050: * @version $Revision: 1.5 $
051: * @date $Date: 2006/06/03 01:23:02 $
052: */
053: public class BaseActionCommand extends AbstractAction {
054:
055: /** The action ID */
056: private String actionId;
057:
058: /** The associated command for execution */
059: protected BaseCommand command;
060:
061: /** Whether the accelerator is editable */
062: private boolean acceleratorEditable;
063:
064: /** <p>Constructs a new command */
065: public BaseActionCommand() {
066: super ();
067: }
068:
069: /** <p>This constructor creates an action without an icon.
070: *
071: * @param command the command for this action to act upon
072: * @param name the action's name
073: */
074: public BaseActionCommand(BaseCommand command, String name) {
075: super (name);
076: setCommand(command);
077: }
078:
079: /** <p>This constructor creates an action with an icon but no name.
080: * (for buttons that require only an icon)
081: *
082: * @param command the command for this action to act upon
083: * @param icon the action's icon
084: */
085: public BaseActionCommand(BaseCommand command, Icon icon) {
086: super (null, icon);
087: setCommand(command);
088: }
089:
090: /** <p>This constructor creates an action with an icon.
091: *
092: * @param command the command for this action to act upon
093: * @param name the action's name
094: * @param icon the action's icon
095: */
096: public BaseActionCommand(BaseCommand command, String name, Icon icon) {
097: super (name, icon);
098: setCommand(command);
099: }
100:
101: /** <p>ActionPerformed is what executed the command.<br>
102: * ActionPerformed is called whenever the action is acted upon.
103: *
104: * @param e the action event
105: */
106: public void actionPerformed(final ActionEvent e) {
107: SwingUtilities.invokeLater(new Runnable() {
108: public void run() {
109: getCommand().execute(e);
110: }
111: });
112: }
113:
114: /** <p>This method retrieves the encapsulated command.
115: *
116: * @return the command
117: */
118: public final BaseCommand getCommand() {
119: return command;
120: }
121:
122: /** <p>Sets the action's command object to the specified value.
123: *
124: * @param newValue the command for this action to act upon
125: */
126: protected final void setCommand(BaseCommand newValue) {
127: this .command = newValue;
128: }
129:
130: /** <p>Sets the action's command object using the
131: * class name specified which is loaded using
132: * <code>Class.forName(...)</code>.
133: *
134: * @param the command's fully qualified class name
135: */
136: public void setCommand(String className) {
137:
138: try {
139: Class _class = Class.forName(className, true, ClassLoader
140: .getSystemClassLoader());
141: Object object = _class.newInstance();
142: command = (BaseCommand) object;
143: } catch (ClassNotFoundException e) {
144: e.printStackTrace();
145: throw new InternalError();
146: } catch (Exception e) {
147: e.printStackTrace();
148: }
149:
150: }
151:
152: /** <p>Returns whether this action has an associated
153: * accelerator key combination.
154: *
155: * @return true | false
156: */
157: public boolean hasAccelerator() {
158: return getValue(ACCELERATOR_KEY) != null;
159: }
160:
161: /** <p>Returns this command's action ID.
162: *
163: * @return the action id
164: */
165: public String getActionId() {
166: return actionId;
167: }
168:
169: /** <p>Sets this command's action ID to the
170: * specified value.
171: *
172: * @param the action ID
173: */
174: public void setActionId(String actionId) {
175: this .actionId = actionId;
176: }
177:
178: public boolean isAcceleratorEditable() {
179: return acceleratorEditable;
180: }
181:
182: public void setAcceleratorEditable(boolean acceleratorEditable) {
183: this.acceleratorEditable = acceleratorEditable;
184: }
185:
186: }
|