001: /*
002: * This program is free software; you can redistribute it and/or
003: * modify it under the terms of the GNU General Public License
004: * as published by the Free Software Foundation; either version 2
005: * of the License, or any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package org.acm.seguin.ide.jedit.action;
017:
018: import java.awt.event.ActionListener;
019:
020: import javax.swing.Icon;
021: import javax.swing.JMenuItem;
022: import javax.swing.JComponent;
023:
024: import org.gjt.sp.util.Log;
025: import org.gjt.sp.jedit.gui.RolloverButton;
026:
027: import org.acm.seguin.ide.jedit.JRefactory;
028:
029: /**
030: * An action defines an action to be taken when the user presses some menu item
031: * in the tree's context menu or a button on the toolbar.
032: *
033: *@author <a href="mailto:JRefactoryPlugin@ladyshot.demon.co.uk">Mike
034: * Atkinson</a>
035: *@created 23 July 2003
036: *@version $Id: Action.java,v 1.1 2003/09/17 19:52:50 mikeatkinson Exp $
037: *@since 0.0.1
038: */
039: public abstract class Action implements ActionListener, Cloneable {
040:
041: /**
042: * Description of the Field
043: */
044: protected JRefactory viewer;
045: /**
046: * Description of the Field
047: */
048: protected RolloverButton tbButton;
049: /**
050: * Description of the Field
051: */
052: protected JComponent cmItem;
053:
054: /**
055: * Returns a String that will be shown as the text of the menu item or the
056: * tooltip of the toolbar button.
057: *
058: *@return The text value
059: */
060: public abstract String getText();
061:
062: /**
063: * Returns the icon to be shown on the toolbar button. The default
064: * implementation returns "null" so that actions that will only be used in
065: * the context menu don't need to implement this.
066: *
067: *@return The icon value
068: */
069: public Icon getIcon() {
070: return null;
071: }
072:
073: /**
074: * Returns the menu item that triggers this action. This returns a
075: * JComponent, which makes it possible to add virtually anything to the
076: * menu. For example, it's possible to return a sub-menu instead of a
077: * simple menu item. The default implementation returns a menu item, which
078: * is stored in the "cmItem" variable.
079: *
080: *@return The menuItem value
081: */
082: public JComponent getMenuItem() {
083: if (cmItem == null) {
084: cmItem = new JMenuItem(getText());
085: ((JMenuItem) cmItem).addActionListener(this );
086: }
087: return cmItem;
088: }
089:
090: /**
091: * Returns the toolbar button that triggers this action.
092: *
093: *@return The button value
094: */
095: public RolloverButton getButton() {
096: if (tbButton == null) {
097: Icon i = getIcon();
098: if (i != null) {
099: tbButton = new RolloverButton(getIcon());
100: } else {
101: tbButton = new RolloverButton();
102: tbButton.setText(getText());
103: }
104: tbButton.setToolTipText(getText());
105: tbButton.addActionListener(this );
106: }
107: return tbButton;
108: }
109:
110: /**
111: * Clones the current action, returning a copy of it.
112: *
113: *@return Description of the Return Value
114: */
115: public Object clone() {
116: try {
117: return super .clone();
118: } catch (CloneNotSupportedException cnse) {
119: // should not happen
120: Log.log(Log.ERROR, this , cnse);
121: return null;
122: }
123: }
124:
125: /**
126: * Sets the viewer where this action is being used.
127: *
128: *@param viewer The new viewer value
129: */
130: public void setViewer(JRefactory viewer) {
131: this.viewer = viewer;
132: }
133:
134: }
|