Chapter 15. A Dialog-Based Macro

Table of Contents

15.1. Use of the Macro
15.2. Listing of the Macro
15.3. Analysis of the Macro
15.3.1. Import Statements
15.3.2. Create the Dialog
15.3.3. Create the Text Fields
15.3.4. Create the Buttons
15.3.5. Register the Action Listeners
15.3.6. Make the Dialog Visible
15.3.7. The Action Listener
15.3.8. Get the User's Input
15.3.9. Call jEdit Methods to Manipulate Text
15.3.10. The Main Routine

Now we will look at a more complicated macro which will demonstrate some useful techniques and BeanShell features.

15.1. Use of the Macro

Our new example adds prefix and suffix text to a series of selected lines. This macro can be used to reduce typing for a series of text items that must be preceded and following by identical text. In Java, for example, if we are interested in making a series of calls to StringBuffer.append() to construct a lengthy, formatted string, we could type the parameter for each call on successive lines as follows:

profileString_1
secretThing.toString()
name
address
addressSupp
city
state/province
country

Our macro would ask for input for the common prefix and suffix to be applied to each line; in this case, the prefix is ourStringBuffer.append( and the suffix is );. After selecting these lines and running the macro, the resulting text would look like this:

ourStringBuffer.append(profileString_1);
ourStringBuffer.append(secretThing.toString());
ourStringBuffer.append(name);
ourStringBuffer.append(address);
ourStringBuffer.append(addressSupp);
ourStringBuffer.append(city);
ourStringBuffer.append(state/province);
ourStringBuffer.append(country);