4
* RecordableTextAction.java - An action that can be recorded and replayed
5
* in an RTextArea macro.
7
* This library is distributed under a modified BSD license. See the included
8
* RSyntaxTextArea.License.txt file for details.
10
package org.fife.ui.rtextarea;
12
import java.awt.event.ActionEvent;
13
import java.util.ResourceBundle;
14
import javax.swing.Icon;
15
import javax.swing.KeyStroke;
16
import javax.swing.text.DefaultEditorKit;
17
import javax.swing.text.JTextComponent;
18
import javax.swing.text.TextAction;
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.
28
* @author Robert Futrell
31
public abstract class RecordableTextAction extends TextAction {
34
* Whether or not this text action should be recorded in a macro.
36
private boolean isRecordable;
42
* @param text The text (name) associated with the action.
44
public RecordableTextAction(String text) {
45
this(text, null, null, null, null);
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.
58
public RecordableTextAction(String text, Icon icon, String desc,
59
Integer mnemonic, KeyStroke accelerator) {
61
putValue(SMALL_ICON, icon);
62
putValue(SHORT_DESCRIPTION, desc);
63
putValue(ACCELERATOR_KEY, accelerator);
64
putValue(MNEMONIC_KEY, mnemonic);
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>.
74
* @param e The action being performed.
75
* @see #actionPerformedImpl
77
public final void actionPerformed(ActionEvent e) {
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)
96
(mod&ActionEvent.ALT_MASK)==0 &&
97
(mod&ActionEvent.CTRL_MASK)==0 &&
98
(mod&ActionEvent.META_MASK)==0)
100
String command = e.getActionCommand();
101
RTextArea.addToCurrentMacro(macroID, command);
102
//System.err.println("... recording it!");
105
actionPerformedImpl(e, textArea);
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.
115
* @param e The action being performed.
116
* @param textArea The text area "receiving" the action.
117
* @see #actionPerformed
119
public abstract void actionPerformedImpl(ActionEvent e, RTextArea textArea);
123
* Returns the accelerator for this action.
125
* @return The accelerator.
126
* @see #setAccelerator(KeyStroke)
128
public KeyStroke getAccelerator() {
129
return (KeyStroke)getValue(ACCELERATOR_KEY);
134
* Returns the description for this action.
136
* @return The description.
138
public String getDescription() {
139
return (String)getValue(SHORT_DESCRIPTION);
144
* Returns the icon for this action.
148
public Icon getIcon() {
149
return (Icon)getValue(SMALL_ICON);
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.
163
* @return The internally-used macro ID.
165
public abstract String getMacroID();
169
* Returns the mnemonic for this action.
171
* @return The mnemonic, or <code>-1</code> if not defined.
172
* @see #setMnemonic(char)
173
* @see #setMnemonic(Integer)
175
public int getMnemonic() {
176
Integer i = (Integer)getValue(MNEMONIC_KEY);
177
return i!=null ? i.intValue() : -1;
182
* Returns the name of this action.
184
* @return The name of this action.
185
* @see #setName(String)
187
public String getName() {
188
return (String)getValue(NAME);
193
* Returns whether or not this action will be recorded and replayed in
196
* @return Whether or not this action will be recorded and replayed.
197
* @see #setRecordable(boolean)
199
public boolean isRecordable() {
205
* Sets the accelerator for this action.
207
* @param accelerator The new accelerator.
208
* @see #getAccelerator()
210
public void setAccelerator(KeyStroke accelerator) {
211
putValue(ACCELERATOR_KEY, accelerator);
216
* Sets the mnemonic for this action.
218
* @param mnemonic The new mnemonic.
219
* @see #setMnemonic(Integer)
220
* @see #getMnemonic()
222
public void setMnemonic(char mnemonic) {
223
setMnemonic(Integer.valueOf(mnemonic));
228
* Sets the mnemonic for this action.
230
* @param mnemonic The new mnemonic.
231
* @see #setMnemonic(char)
232
* @see #getMnemonic()
234
public void setMnemonic(Integer mnemonic) {
235
putValue(MNEMONIC_KEY, mnemonic);
240
* Sets the name of this action.
242
* @param name The new name.
245
public void setName(String name) {
246
putValue(NAME, name);
251
* Sets the name, mnemonic, and description of this action.
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
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"));
267
* Sets whether or not this action will be recorded and replayed in
270
* @param recordable Whether or not this action should be recorded
272
* @see #isRecordable()
274
public void setRecordable(boolean recordable) {
275
isRecordable = recordable;
280
* Sets the short description for this action.
282
* @param shortDesc The short description for this action.
284
public void setShortDescription(String shortDesc) {
285
putValue(SHORT_DESCRIPTION, shortDesc);
b'\\ No newline at end of file'