001: /*
002: * EditAction.java - jEdit action listener
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 1998, 2003 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit;
024:
025: //{{{ Imports
026: import java.awt.event.ActionEvent;
027: import java.awt.event.ActionListener;
028: import java.awt.Component;
029:
030: import org.gjt.sp.util.Log;
031:
032: //}}}
033:
034: /**
035: * An action that can be bound to a menu item, tool bar button or keystroke.
036: *
037: * @see jEdit#getAction(String)
038: * @see jEdit#getActionNames()
039: * @see ActionSet
040: *
041: * @author Slava Pestov
042: * @version $Id: EditAction.java 11177 2007-12-01 09:50:50Z k_satoda $
043: */
044: public abstract class EditAction {
045: //{{{ Private members
046: private String name;
047:
048: protected Object[] args;
049:
050: //}}}
051:
052: //{{{ EditAction constructors
053: /**
054: * Creates a new edit action with the specified name.
055: * @param name The action name
056: */
057: public EditAction(String name) {
058: this .name = name;
059: }
060:
061: public EditAction(String name, Object[] newArgs) {
062: this .name = name;
063: this .args = newArgs;
064: } //}}}
065:
066: //{{{ getName() method
067: /**
068: * Returns the internal name of this action.
069: */
070: public String getName() {
071: return name;
072: } //}}}
073:
074: // {{{ setName() method
075: /**
076: * Changes the name of an action
077: * @param newName
078: * @since jEdit 4.3pre4
079: */
080: public void setName(String newName) {
081: name = newName;
082: }// }}}
083:
084: //{{{ getLabel() method
085: /**
086: * Returns the action's label. This returns the
087: * value of the property named by {@link #getName()} suffixed
088: * with <code>.label</code>.
089: *
090: */
091: public String getLabel() {
092: if (args != null) {
093: return jEdit.getProperty(name + ".label", args);
094: }
095: return jEdit.getProperty(name + ".label");
096: } //}}}
097:
098: //{{{ getMouseOverText() method
099: /**
100: * Returns the action's mouse over message. This returns the
101: * value of the property named by {@link #getName()} suffixed
102: * with <code>.mouse-over</code>.
103: */
104: public final String getMouseOverText() {
105: return jEdit.getProperty(name + ".mouse-over");
106: } //}}}
107:
108: //{{{ invoke() method
109: /**
110: * Invokes the action. This is an implementation of the Command pattern,
111: * and concrete actions should override this.
112: *
113: * @param view The view
114: * @since jEdit 2.7pre2
115: * abstract since jEdit 4.3pre7
116: */
117: abstract public void invoke(View view);
118:
119: /**
120: *
121: * @param view
122: * @param newArgs new argument list
123: * @since jEdit 4.3pre7
124: */
125: final public void invoke(View view, Object[] newArgs) {
126: args = newArgs;
127: invoke(view);
128: } //}}}
129:
130: //{{{ getView() method
131: /**
132: * @deprecated Call <code>GUIUtilities.getView()</code> instead.
133: */
134: public static View getView(Component comp) {
135: // moved to GUIUtilities as it makes no sense being here.
136: return GUIUtilities.getView(comp);
137: } //}}}
138:
139: //{{{ isToggle() method
140: /**
141: * Returns if this edit action should be displayed as a check box
142: * in menus. This returns the
143: * value of the property named by {@link #getName()} suffixed
144: * with <code>.toggle</code>.
145: *
146: * @since jEdit 2.2pre4
147: */
148: public final boolean isToggle() {
149: return jEdit.getBooleanProperty(name + ".toggle");
150: } //}}}
151:
152: //{{{ isSelected() method
153: /**
154: * If this edit action is a toggle, returns if it is selected or not.
155: * @param comp The component
156: * @since jEdit 4.2pre1
157: */
158: public boolean isSelected(Component comp) {
159: return false;
160: } //}}}
161:
162: //{{{ noRepeat() method
163: /**
164: * Returns if this edit action should not be repeated. Returns false
165: * by default.
166: * @since jEdit 2.7pre2
167: */
168: public boolean noRepeat() {
169: return false;
170: } //}}}
171:
172: //{{{ noRecord() method
173: /**
174: * Returns if this edit action should not be recorded. Returns false
175: * by default.
176: * @since jEdit 2.7pre2
177: */
178: public boolean noRecord() {
179: return false;
180: } //}}}
181:
182: //{{{ noRememberLast() method
183: /**
184: * Returns if this edit action should not be remembered as the most
185: * recently invoked action.
186: * @since jEdit 4.2pre1
187: */
188: public boolean noRememberLast() {
189: return false;
190: } //}}}
191:
192: //{{{ getCode() method
193: /**
194: * Returns the BeanShell code that will replay this action.
195: * BeanShellAction.getCode() returns something more interesting for Actions that were loaded
196: * from the actions.xml file.
197: * You do not need to override this method if your action name is unique,
198: * this EditAction was added to an ActionSet and that to an ActionContext of jEdit.
199: *
200: * concrete since jEdit 4.3pre7
201: * @since jEdit 2.7pre2
202: *
203: */
204: public String getCode() {
205: return "jEdit.getAction(" + name + ").invoke(view); ";
206: }
207:
208: //}}}
209:
210: //{{{ toString() method
211: public String toString() {
212: return name;
213: } //}}}
214:
215: //{{{ Wrapper class
216: /**
217: * 'Wrap' EditActions in this class to turn them into AWT
218: * ActionListeners, that can be attached to buttons, menu items, etc.
219: */
220: public static class Wrapper implements ActionListener {
221:
222: private ActionContext context;
223: private String actionName;
224:
225: /**
226: * Creates a new action listener wrapper.
227: * @since jEdit 4.2pre1
228: */
229: public Wrapper(ActionContext context, String actionName) {
230: this .context = context;
231: this .actionName = actionName;
232: }
233:
234: /**
235: * Called when the user selects this action from a menu.
236: * It passes the action through the
237: * {@link org.gjt.sp.jedit.gui.InputHandler#invokeAction(EditAction)}
238: * method, which performs any recording or repeating.
239: *
240: * @param evt The action event
241: */
242: public void actionPerformed(ActionEvent evt) {
243: EditAction action = context.getAction(actionName);
244: if (action == null) {
245: Log.log(Log.WARNING, this , "Unknown action: "
246: + actionName);
247: } else
248: context.invokeAction(evt, action);
249: }
250:
251: } //}}}
252: }
|