15.2. Listing of the Macro

The macro script follows. You can find it in the jEdit distribution in the Text subdirectory of the macros directory. You can also try it out by invoking Macros>Text>Add Prefix and Suffix.

// beginning of Add_Prefix_and_Suffix.bsh

// import statement (see Section 15.3.1, “Import Statements”)
import javax.swing.border.*;

// main routine
void prefixSuffixDialog()
{
    // create dialog object (see Section 15.3.2, “Create the Dialog”)
    title = Add prefix and suffix to selected lines;
    dialog = new JDialog(view, title, false);
    content = new JPanel(new BorderLayout());
    content.setBorder(new EmptyBorder(12, 12, 12, 12));
    content.setPreferredSize(new Dimension(320, 160));
    dialog.setContentPane(content);

    // add the text fields (see Section 15.3.3, “Create the Text Fields”)
    fieldPanel = new JPanel(new GridLayout(4, 1, 0, 6));
    prefixField = new HistoryTextField(macro.add-prefix);
    prefixLabel = new JLabel(Prefix to add:);
    suffixField = new HistoryTextField(macro.add-suffix);
    suffixLabel = new JLabel(Suffix to add:);
    fieldPanel.add(prefixLabel);
    fieldPanel.add(prefixField);
    fieldPanel.add(suffixLabel);
    fieldPanel.add(suffixField);
    content.add(fieldPanel, Center);

    // add a panel containing the buttons (see Section 15.3.4, “Create the Buttons”)
    buttonPanel = new JPanel();
    buttonPanel.setLayout(new BoxLayout(buttonPanel,
        BoxLayout.X_AXIS));
    buttonPanel.setBorder(new EmptyBorder(12, 50, 0, 50));
    buttonPanel.add(Box.createGlue());
    ok = new JButton(OK);
    cancel = new JButton(Cancel);
    ok.setPreferredSize(cancel.getPreferredSize());
    dialog.getRootPane().setDefaultButton(ok);
    buttonPanel.add(ok);
    buttonPanel.add(Box.createHorizontalStrut(6));
    buttonPanel.add(cancel);
    buttonPanel.add(Box.createGlue());
    content.add(buttonPanel, South);

    // register this method as an ActionListener for
    // the buttons and text fields (see Section 15.3.5, “Register the Action Listeners”)
    ok.addActionListener(this);
    cancel.addActionListener(this);
    prefixField.addActionListener(this);
    suffixField.addActionListener(this);

    // locate the dialog in the center of the
    // editing pane and make it visible (see Section 15.3.6, “Make the Dialog Visible”)
    dialog.pack();
    dialog.setLocationRelativeTo(view);
    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    dialog.setVisible(true);

    // this method will be called when a button is clicked
    // or when ENTER is pressed (see Section 15.3.7, “The Action Listener”)
    void actionPerformed(e)
    {
        if(e.getSource() != cancel)
        {
            processText();
        }
        dialog.dispose();
    }

    // this is where the work gets done to insert
    // the prefix and suffix (see Section 15.3.8, “Get the User's Input”)
    void processText()
    {
        prefix = prefixField.getText();
        suffix = suffixField.getText();
        if(prefix.length() == 0 && suffix.length() == 0)
            return;
        prefixField.addCurrentToHistory();
        suffixField.addCurrentToHistory();

        // text manipulation begins here using calls
        // to jEdit methods  (see Section 15.3.9, “Call jEdit Methods to Manipulate Text”)
        buffer.beginCompoundEdit();
        selectedLines = textArea.getSelectedLines();
        for(i = 0; i < selectedLines.length; ++i)
        {
            offsetBOL = textArea.getLineStartOffset(
                selectedLines[i]);
            textArea.setCaretPosition(offsetBOL);
            textArea.goToStartOfWhiteSpace(false);
            textArea.goToEndOfWhiteSpace(true);
            text = textArea.getSelectedText();
            if(text == null) text = "";
            textArea.setSelectedText(prefix + text + suffix);
        }
        buffer.endCompoundEdit();
    }
}

// this single line of code is the script's main routine
// (see Section 15.3.10, “The Main Routine”)
prefixSuffixDialog();

// end of Add_Prefix_and_Suffix.bsh