Using the Macro Class

Questions on using RSyntaxTextArea should go here.

Moderator: robert

Using the Macro Class

Postby Azlan » Thu May 09, 2013 8:08 pm

You state:

robert wrote:Macros are simply recordings of actions in an RSyntaxTextArea that can be played back. You can set up an application so that the user can choose to "record" a set of actions in the editor, then play them back later. The relevant methods on the RSyntaxTextArea class (inherited from RTextArea, but see Javadoc for all RSTA classes here):

  • public static void beginRecordingMacro()
  • public static void endRecordingMacro()
  • public static void loadMacro(Macro)
  • public static Macro getCurrentMacro()

The Macro class encapsulates the set of actions. It can be loaded and saved to disk via its constructors and its saveToFile() method. Macros are saved as simple XML representations of the actions to execute.

The idea is that an application that supported macros would have say a menu item, "Begin Recording Macro". On clicking it, all editor actions (typing, selecting text, etc.) are recorded to a Macro instance (via textArea.beginRecordingMacro()). A second menu item, "End Recording Macro", is clicked, resulting in textArea.endRecordingMacro() being called. The user can then be prompted for macro name, and the macro saved to a file. The application could display a list of previously recorded macros, say in a menu, for the user to play back.

RText used to have this functionality, but it was removed in favor of scripted macros (Groovy and JavaScript). It's kind of a shame really, as the two are not the same functionality, so perhaps I'll add it back some day...


Would a macro simply look like this:

java code:

beginMacro.addActionListener() {
new ActionListener() {
public void actionPerformed(ActionEvent evt) {
textArea.beginRecordingMacro();
}
}
}

stopRecording.addActionListener() {
new ActionListener() {
public void actionPerformed(ActionEvent evt) {
textArea.endRecordingMacro();
}
}
}

saveMacro.addActionListener() {
new ActionListener() {
public void actionPerformed(ActionEvent evt) {
textArea.saveToFile(file);
}
}
}


You also said,
The user can then be prompted for macro name, and the macro saved to a file. The application could display a list of previously recorded macros, say in a menu, for the user to play back.


I don't know where I would start in trying to do that. But, my main question is how do you use the macro class? For recording events, saving them, and letting the user play them back?
Azlan
 
Posts: 14
Joined: Thu Apr 11, 2013 4:54 pm

Re: Using the Macro Class

Postby groggy » Fri May 10, 2013 7:16 pm

I had the same problem, but the aolution is very simple:

you can start macros by using the method beginRecordingMacro() and stop that macro by just calling endRecordingMacro().
to save that macro, you grap the macro from the RTextArea via getCurrentMacro() and then call the saveToFile() method:
Code: Select all
// begin recording
textArea.beginRedordingMacro();
// stop recording
textArea.endRecordingMacro();
// grap the macro
Macro macro = textArea.getCurrentMacro();
// save the macro
macro.saveToFile(String filename);


You can implement the start and end method like you did or use javax.swing.Action's(that way is much cleaner and simplier).
After the user stoped recording the maceo(pushed a button) you can open a filechooser and get a file from there and then call the method saveToFile and put as parameter the choosen file.

To load a macro you have to create a new Macro object and put as parameter a file object that refers to the saved file. Then you call thw method loadMacro on your RTextArea and after that playbackLastMacro();
Code: Select all
Macro macro = new Macro(new File(path to file));
textArea.loadMacro(macro);
textArea.playbackLastMacro();
groggy
 
Posts: 34
Joined: Sun Feb 17, 2013 1:11 am

Re: Using the Macro Class

Postby Azlan » Sat May 11, 2013 12:23 am

i'm very confused right now. i got run macro, and stop macro, but how would I saveToFile(); and use a JFileChooser? And, what is the path-to-file? Is it the previous file's path?
Azlan
 
Posts: 14
Joined: Thu Apr 11, 2013 4:54 pm

Re: Using the Macro Class

Postby robert » Sat May 11, 2013 3:36 am

Saving the current macro with an Action would look something like this (untested, but gives the basic idea):

java code:

import javax.swing.*;

public class SaveCurrentMacroAction extends AbstractAction {

public SaveCurrentMacroAction() {
putValue(NAME, "Save Current Macro...");
}

public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
int rc = chooser.showOpenDialog(null);
if (rc==JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
RTextArea.getCurrentMacro().saveToFile(file.getAbsolutePath());
}
}

}


And loading a macro would look like:

java code:

File file = getSavedMacroFileInSomeWay();
RTextArea.loadMacro(new Macro(file));


When this feature was integrated into RText, it tried to simply loading and saving macros for users by not allowing them to specify the files to store individual Macros in. Rather, it simply asked for a Macro "name", and always loaded/saved macros in a specific folder as name.xml. If you're working on a code editor application, this is probably a good approach, as the user doesn't have to manually keep track of all of their macros, but they're still externalized and technically "portable" from one machine to the next, since they're just XML files. If you want to see RText's old implementation, check out the tagged "1.3.1" version from SVN. A good starting point is SaveMacroDialog.java, which is the dialog that popped up, asked for the "Macro name", and saved it in the right location.
User avatar
robert
 
Posts: 798
Joined: Sat May 10, 2008 5:16 pm

Re: Using the Macro Class

Postby Azlan » Sat May 11, 2013 5:14 pm

Ok, so far the user can start a macro, end it, and save it. When the user saves it, it generates an xml file. But, how do I let the user name it, and load it?

So far for loading the macro, I have this:

java code:

textArea.loadMacro(new Macro(file));


file being the File that the JFileChooser selected. I've also tried this:

java code:

textArea.loadMacro(new Macro(file.getAbsolutePath()));


For each attempt, nothing happens. No stacktrace, nothing.

Also, what extension should I give the macro? Because, when I loaded it(picked it with a JFileChooser) nothing happened.
Azlan
 
Posts: 14
Joined: Thu Apr 11, 2013 4:54 pm

Re: Using the Macro Class

Postby robert » Sat May 11, 2013 8:37 pm

You'll have to prompt the user for the name in some way. Alternatively, you can use the name of the file they select to save the Macro with, stripping off the ".xml" or whatever extension you use. The "name" field is purely arbitrary; the idea was that it was a logical name to display in the UI to represent the macro.

The reason your load doesn't work is because you're using the wrong Macro constructor:

java code:

new Macro(String) // Returns a new, empty Macro with name set to the String's value
new Macro(File) // Loads a Macro from XML, which contains both the Macro name and its actions


See the Javadoc for the Macro class. I'll update it to be a little more explicit about usage for the next release.
User avatar
robert
 
Posts: 798
Joined: Sat May 10, 2008 5:16 pm


Return to Help

Who is online

Users browsing this forum: No registered users and 1 guest

cron