source: other-projects/rsyntax-textarea/src/java/org/fife/ui/rtextarea/RecordableTextAction.java@ 25584

Last change on this file since 25584 was 25584, checked in by davidb, 12 years ago

Initial cut an a text edit area for GLI that supports color syntax highlighting

File size: 7.6 KB
Line 
1/*
2 * 08/19/2004
3 *
4 * RecordableTextAction.java - An action that can be recorded and replayed
5 * in an RTextArea macro.
6 *
7 * This library is distributed under a modified BSD license. See the included
8 * RSyntaxTextArea.License.txt file for details.
9 */
10package org.fife.ui.rtextarea;
11
12import java.awt.event.ActionEvent;
13import java.util.ResourceBundle;
14import javax.swing.Icon;
15import javax.swing.KeyStroke;
16import javax.swing.text.DefaultEditorKit;
17import javax.swing.text.JTextComponent;
18import javax.swing.text.TextAction;
19
20
21/**
22 * The base action used by the actions defined in
23 * {@link RTextAreaEditorKit}. This action is what allows instances of
24 * <code>RTextArea</code> to record keystrokes into "macros;" if an action is
25 * recordable and occurs while the user is recording a macro, it adds itself to
26 * the currently-being-recorded macro.
27 *
28 * @author Robert Futrell
29 * @version 0.5
30 */
31public abstract class RecordableTextAction extends TextAction {
32
33 /**
34 * Whether or not this text action should be recorded in a macro.
35 */
36 private boolean isRecordable;
37
38
39 /**
40 * Constructor.
41 *
42 * @param text The text (name) associated with the action.
43 */
44 public RecordableTextAction(String text) {
45 this(text, null, null, null, null);
46 }
47
48
49 /**
50 * Constructor.
51 *
52 * @param text The text (name) associated with the action.
53 * @param icon The icon associated with the action.
54 * @param desc The description of the action.
55 * @param mnemonic The mnemonic for the action.
56 * @param accelerator The accelerator key for the action.
57 */
58 public RecordableTextAction(String text, Icon icon, String desc,
59 Integer mnemonic, KeyStroke accelerator) {
60 super(text);
61 putValue(SMALL_ICON, icon);
62 putValue(SHORT_DESCRIPTION, desc);
63 putValue(ACCELERATOR_KEY, accelerator);
64 putValue(MNEMONIC_KEY, mnemonic);
65 setRecordable(true);
66 }
67
68
69 /**
70 * This method is final so that you cannot override it and mess up the
71 * macro-recording part of it. The actual meat of the action is in
72 * <code>actionPerformedImpl</code>.
73 *
74 * @param e The action being performed.
75 * @see #actionPerformedImpl
76 */
77 public final void actionPerformed(ActionEvent e) {
78
79 JTextComponent textComponent = getTextComponent(e);
80 if (textComponent instanceof RTextArea) {
81 RTextArea textArea = (RTextArea)textComponent;
82 //System.err.println("Recordable action: " + getMacroID());
83 if (RTextArea.isRecordingMacro() && isRecordable()) {
84 int mod = e.getModifiers();
85 // We ignore keypresses involving ctrl/alt/meta if they are
86 // "default" keypresses - i.e., they aren't some special
87 // action like paste (e.g., paste would return Ctrl+V, but
88 // its action would be "paste-action").
89 String macroID = getMacroID();
90//System.err.println(macroID);
91//System.err.println("... " + (mod&ActionEvent.ALT_MASK));
92//System.err.println("... " + (mod&ActionEvent.CTRL_MASK));
93//System.err.println("... " + (mod&ActionEvent.META_MASK));
94 if (!DefaultEditorKit.defaultKeyTypedAction.equals(macroID)
95 || (
96 (mod&ActionEvent.ALT_MASK)==0 &&
97 (mod&ActionEvent.CTRL_MASK)==0 &&
98 (mod&ActionEvent.META_MASK)==0)
99 ) {
100 String command = e.getActionCommand();
101 RTextArea.addToCurrentMacro(macroID, command);
102 //System.err.println("... recording it!");
103 }
104 }
105 actionPerformedImpl(e, textArea);
106 }
107
108 }
109
110
111 /**
112 * The actual meat of the action. If you wish to subclass this action
113 * and modify its behavior, this is the method to override.
114 *
115 * @param e The action being performed.
116 * @param textArea The text area "receiving" the action.
117 * @see #actionPerformed
118 */
119 public abstract void actionPerformedImpl(ActionEvent e, RTextArea textArea);
120
121
122 /**
123 * Returns the accelerator for this action.
124 *
125 * @return The accelerator.
126 * @see #setAccelerator(KeyStroke)
127 */
128 public KeyStroke getAccelerator() {
129 return (KeyStroke)getValue(ACCELERATOR_KEY);
130 }
131
132
133 /**
134 * Returns the description for this action.
135 *
136 * @return The description.
137 */
138 public String getDescription() {
139 return (String)getValue(SHORT_DESCRIPTION);
140 }
141
142
143 /**
144 * Returns the icon for this action.
145 *
146 * @return The icon.
147 */
148 public Icon getIcon() {
149 return (Icon)getValue(SMALL_ICON);
150 }
151
152
153 /**
154 * Returns the identifier for this macro. This method makes it so that you
155 * can create an instance of the <code>RTextAreaEditorKit.CutAction</code>
156 * action, for example, rename it to "Remove", and it will still be
157 * recorded as a "cut" action. Subclasses should return a unique string
158 * from this method; preferably the name of the action.<p>
159 * If you subclass a <code>RecordableTextAction</code>, you should NOT
160 * override this method; if you do, the action may not be properly
161 * recorded in a macro.
162 *
163 * @return The internally-used macro ID.
164 */
165 public abstract String getMacroID();
166
167
168 /**
169 * Returns the mnemonic for this action.
170 *
171 * @return The mnemonic, or <code>-1</code> if not defined.
172 * @see #setMnemonic(char)
173 * @see #setMnemonic(Integer)
174 */
175 public int getMnemonic() {
176 Integer i = (Integer)getValue(MNEMONIC_KEY);
177 return i!=null ? i.intValue() : -1;
178 }
179
180
181 /**
182 * Returns the name of this action.
183 *
184 * @return The name of this action.
185 * @see #setName(String)
186 */
187 public String getName() {
188 return (String)getValue(NAME);
189 }
190
191
192 /**
193 * Returns whether or not this action will be recorded and replayed in
194 * a macro.
195 *
196 * @return Whether or not this action will be recorded and replayed.
197 * @see #setRecordable
198 */
199 public boolean isRecordable() {
200 return isRecordable;
201 }
202
203
204 /**
205 * Sets the accelerator for this action.
206 *
207 * @param accelerator The new accelerator.
208 * @see #getAccelerator()
209 */
210 public void setAccelerator(KeyStroke accelerator) {
211 putValue(ACCELERATOR_KEY, accelerator);
212 }
213
214
215 /**
216 * Sets the mnemonic for this action.
217 *
218 * @param mnemonic The new mnemonic.
219 * @see #setMnemonic(Integer)
220 * @see #getMnemonic()
221 */
222 public void setMnemonic(char mnemonic) {
223 setMnemonic(new Integer(mnemonic));
224 }
225
226
227 /**
228 * Sets the mnemonic for this action.
229 *
230 * @param mnemonic The new mnemonic.
231 * @see #setMnemonic(char)
232 * @see #getMnemonic()
233 */
234 public void setMnemonic(Integer mnemonic) {
235 putValue(MNEMONIC_KEY, mnemonic);
236 }
237
238
239 /**
240 * Sets the name of this action.
241 *
242 * @param name The new name.
243 * @see #getName()
244 */
245 public void setName(String name) {
246 putValue(NAME, name);
247 }
248
249
250 /**
251 * Sets the name, mnemonic, and description of this action.
252 *
253 * @param msg The resource bundle.
254 * @param keyRoot The root of the keys for the properties.
255 * "<code>.Name</code>", "<code>.Mnemonic</code>", and
256 * "<code>.Desc</code>" will be appended to create the key for each
257 * property.
258 */
259 public void setProperties(ResourceBundle msg, String keyRoot) {
260 setName(msg.getString(keyRoot + ".Name"));
261 setMnemonic(msg.getString(keyRoot + ".Mnemonic").charAt(0));
262 setShortDescription(msg.getString(keyRoot + ".Desc"));
263 }
264
265
266 /**
267 * Sets whether or not this action will be recorded and replayed in
268 * a macro.
269 *
270 * @param recordable Whether or not this action should be recorded
271 * and replayed.
272 * @see #isRecordable
273 */
274 public void setRecordable(boolean recordable) {
275 isRecordable = recordable;
276 }
277
278
279 /**
280 * Sets the short description for this action.
281 *
282 * @param shortDesc The short description for this action.
283 */
284 public void setShortDescription(String shortDesc) {
285 putValue(SHORT_DESCRIPTION, shortDesc);
286 }
287
288
289}
Note: See TracBrowser for help on using the repository browser.