KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > EditAction


1 /*
2  * EditAction.java - jEdit action listener
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 1998, 2003 Slava Pestov
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22
23 package org.gjt.sp.jedit;
24
25 //{{{ Imports
26
import java.awt.event.ActionEvent JavaDoc;
27 import java.awt.event.ActionListener JavaDoc;
28 import java.awt.Component JavaDoc;
29
30 import org.gjt.sp.util.Log;
31 //}}}
32

33 /**
34  * An action that can be bound to a menu item, tool bar button or keystroke.
35  *
36  * @see jEdit#getAction(String)
37  * @see jEdit#getActionNames()
38  * @see ActionSet
39  *
40  * @author Slava Pestov
41  * @version $Id: EditAction.java 6884 2006-09-06 02:38:55Z ezust $
42  *
43  * TODO: refactor common shortcut1 and shortcut2 code here.
44  *
45  */

46 public abstract class EditAction
47 {
48     //{{{ Private members
49
private String JavaDoc name;
50     
51     protected Object JavaDoc[] args;
52
53     //}}}
54

55     //{{{ EditAction constructors
56
/**
57      * Creates a new edit action with the specified name.
58      * @param name The action name
59      */

60     public EditAction(String JavaDoc name)
61     {
62         this.name = name;
63     }
64     
65     public EditAction(String JavaDoc name, Object JavaDoc[] newArgs) {
66         this.name = name;
67         this.args = newArgs;
68     }
69         
70     //{{{ getName() method
71
/**
72      * Returns the internal name of this action.
73      */

74     public String JavaDoc getName()
75     {
76         return name;
77     } //}}}
78

79     /**
80      * Changes the name of an action
81      * @param newName
82      * @since jEdit 4.3pre4
83      */

84     public void setName(String JavaDoc newName) {
85         name = newName;
86     }
87
88     //{{{ getLabel() method
89
/**
90      * Returns the action's label. This returns the
91      * value of the property named by {@link #getName()} suffixed
92      * with <code>.label</code>.
93      *
94      */

95     public String JavaDoc getLabel()
96     {
97         if (args != null) {
98             return jEdit.getProperty(name + ".label", args);
99         }
100         return jEdit.getProperty(name + ".label");
101     } //}}}
102

103     //{{{ getMouseOverText() method
104
/**
105      * Returns the action's mouse over message. This returns the
106      * value of the property named by {@link #getName()} suffixed
107      * with <code>.mouse-over</code>.
108      */

109     public final String JavaDoc getMouseOverText()
110     {
111         return jEdit.getProperty(name + ".mouse-over");
112     } //}}}
113

114     //{{{ invoke() method
115
/**
116      * Invokes the action. This is an implementation of the Command pattern,
117      * and concrete actions should override this.
118      *
119      * @param view The view
120      * @since jEdit 2.7pre2
121      * abstract since jEdit 4.3pre7
122      */

123     abstract public void invoke(View view);
124
125     /**
126      *
127      * @param view
128      * @param newArgs new argument list
129      * @since jEdit 4.3pre7
130      */

131     final public void invoke(View view, Object JavaDoc[] newArgs) {
132         args = newArgs;
133         invoke(view);
134     }
135     
136     //{{{ getView() method
137
/**
138      * @deprecated Call <code>GUIUtilities.getView()</code> instead.
139      */

140     public static View getView(Component JavaDoc comp)
141     {
142         // moved to GUIUtilities as it makes no sense being here.
143
return GUIUtilities.getView(comp);
144     } //}}}
145

146     //{{{ isToggle() method
147
/**
148      * Returns if this edit action should be displayed as a check box
149      * in menus. This returns the
150      * value of the property named by {@link #getName()} suffixed
151      * with <code>.toggle</code>.
152      *
153      * @since jEdit 2.2pre4
154      */

155     public final boolean isToggle()
156     {
157         return jEdit.getBooleanProperty(name + ".toggle");
158     } //}}}
159

160     //{{{ isSelected() method
161
/**
162      * If this edit action is a toggle, returns if it is selected or not.
163      * @param comp The component
164      * @since jEdit 4.2pre1
165      */

166     public boolean isSelected(Component JavaDoc comp)
167     {
168         return false;
169     } //}}}
170

171     //{{{ noRepeat() method
172
/**
173      * Returns if this edit action should not be repeated. Returns false
174      * by default.
175      * @since jEdit 2.7pre2
176      */

177     public boolean noRepeat()
178     {
179         return false;
180     } //}}}
181

182     //{{{ noRecord() method
183
/**
184      * Returns if this edit action should not be recorded. Returns false
185      * by default.
186      * @since jEdit 2.7pre2
187      */

188     public boolean noRecord()
189     {
190         return false;
191     } //}}}
192

193     //{{{ noRememberLast() method
194
/**
195      * Returns if this edit action should not be remembered as the most
196      * recently invoked action.
197      * @since jEdit 4.2pre1
198      */

199     public boolean noRememberLast()
200     {
201         return false;
202     } //}}}
203

204     //{{{ getCode() method
205
/**
206      * Returns the BeanShell code that will replay this action.
207      * BeanShellAction.getCode() returns something more interesting for Actions that were loaded
208      * from the actions.xml file.
209      * You do not need to override this method if your action name is unique,
210      * this EditAction was added to an ActionSet and that to an ActionContext of jEdit.
211      *
212      * concrete since jEdit 4.3pre7
213      * @since jEdit 2.7pre2
214      *
215      */

216     public String JavaDoc getCode()
217     {
218         return "jEdit.getAction(" + name + ").invoke(view); ";
219     }
220     //}}}
221

222     //{{{ toString() method
223
public String JavaDoc toString()
224     {
225         return name;
226     } //}}}
227

228     //{{{ Wrapper class
229
/**
230      * 'Wrap' EditActions in this class to turn them into AWT
231      * ActionListeners, that can be attached to buttons, menu items, etc.
232      */

233     public static class Wrapper implements ActionListener JavaDoc
234     {
235
236         private ActionContext context;
237         private String JavaDoc actionName;
238         
239         /**
240          * Creates a new action listener wrapper.
241          * @since jEdit 4.2pre1
242          */

243         public Wrapper(ActionContext context, String JavaDoc actionName)
244         {
245             this.context = context;
246             this.actionName = actionName;
247         }
248
249         /**
250          * Called when the user selects this action from a menu.
251          * It passes the action through the
252          * {@link org.gjt.sp.jedit.gui.InputHandler#invokeAction(EditAction)}
253          * method, which performs any recording or repeating.
254          *
255          * @param evt The action event
256          */

257         public void actionPerformed(ActionEvent JavaDoc evt)
258         {
259             EditAction action = context.getAction(actionName);
260             if(action == null)
261             {
262                 Log.log(Log.WARNING,this,"Unknown action: "
263                     + actionName);
264             }
265             else
266                 context.invokeAction(evt,action);
267         }
268
269
270     } //}}}
271
}
272
Popular Tags