PageRenderTime 7ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/www/tags/NOV_07_2009/htdocs/users-guide/add-prefix-and-suffix.html

#
HTML | 109 lines | 98 code | 11 blank | 0 comment | 0 complexity | 3cb00e8e1aef8389c4b65d6308ebddd6 MD5 | raw file
  1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Listing of the Macro</title><meta name="generator" content="DocBook XSL Stylesheets V1.73.2"><link rel="start" href="index.html" title="jEdit 4.3 User's Guide"><link rel="up" href="dialog-macro.html" title="Chapter 14. A Dialog-Based Macro"><link rel="prev" href="dialog-macro-intro.html" title="Use of the Macro"><link rel="next" href="macro-analysis.html" title="Analysis of the Macro"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Listing of the Macro</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="dialog-macro-intro.html">Prev</a> </td><th width="60%" align="center">Chapter 14. A Dialog-Based Macro</th><td width="20%" align="right"> <a accesskey="n" href="macro-analysis.html">Next</a></td></tr></table><hr></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="add-prefix-and-suffix"></a>Listing of the Macro</h2></div></div></div><p>The macro script follows. You can find it in the jEdit
  2        distribution in the <code class="filename">Text</code> subdirectory of the
  3        <code class="filename">macros</code> directory. You can also try it out by
  4        invoking
  5        <span class="guimenu"><strong>Macros</strong></span>&gt;<span class="guisubmenu"><strong>Text</strong></span>&gt;<span class="guimenuitem"><strong>Add
  6        Prefix and Suffix</strong></span>.</p><div class="informalexample"><pre class="programlisting">// beginning of Add_Prefix_and_Suffix.bsh
  7
  8<a name="imports"></a>// import statement (see <a class="xref" href="macro-analysis.html#explain-imports" title="Import Statements">the section called &#8220;Import Statements&#8221;</a>)
  9import javax.swing.border.*;
 10
 11<a name="main-routine"></a>// main routine
 12void prefixSuffixDialog()
 13{
 14<a name="create-dialog"></a>    // create dialog object (see <a class="xref" href="macro-analysis.html#explain-create-dialog" title="Create the Dialog">the section called &#8220;Create the Dialog&#8221;</a>)
 15    title = &#8220;<span class="quote">Add prefix and suffix to selected lines</span>&#8221;;
 16    dialog = new JDialog(view, title, false);
 17    content = new JPanel(new BorderLayout());
 18    content.setBorder(new EmptyBorder(12, 12, 12, 12));
 19    content.setPreferredSize(new Dimension(320, 160));
 20    dialog.setContentPane(content);
 21
 22<a name="fields-panel"></a>    // add the text fields (see <a class="xref" href="macro-analysis.html#explain-fields-panel" title="Create the Text Fields">the section called &#8220;Create the Text Fields&#8221;</a>)
 23    fieldPanel = new JPanel(new GridLayout(4, 1, 0, 6));
 24    prefixField = new HistoryTextField(&#8220;<span class="quote">macro.add-prefix</span>&#8221;);
 25    prefixLabel = new JLabel(&#8220;<span class="quote">Prefix to add:</span>&#8221;);
 26    suffixField = new HistoryTextField(&#8220;<span class="quote">macro.add-suffix</span>&#8221;);
 27    suffixLabel = new JLabel(&#8220;<span class="quote">Suffix to add:</span>&#8221;);
 28    fieldPanel.add(prefixLabel);
 29    fieldPanel.add(prefixField);
 30    fieldPanel.add(suffixLabel);
 31    fieldPanel.add(suffixField);
 32    content.add(fieldPanel, &#8220;<span class="quote">Center</span>&#8221;);
 33
 34<a name="button-panel"></a>    // add a panel containing the buttons (see <a class="xref" href="macro-analysis.html#explain-button-panel" title="Create the Buttons">the section called &#8220;Create the Buttons&#8221;</a>)
 35    buttonPanel = new JPanel();
 36    buttonPanel.setLayout(new BoxLayout(buttonPanel,
 37        BoxLayout.X_AXIS));
 38    buttonPanel.setBorder(new EmptyBorder(12, 50, 0, 50));
 39    buttonPanel.add(Box.createGlue());
 40    ok = new JButton(&#8220;<span class="quote">OK</span>&#8221;);
 41    cancel = new JButton(&#8220;<span class="quote">Cancel</span>&#8221;);
 42    ok.setPreferredSize(cancel.getPreferredSize());
 43    dialog.getRootPane().setDefaultButton(ok);
 44    buttonPanel.add(ok);
 45    buttonPanel.add(Box.createHorizontalStrut(6));
 46    buttonPanel.add(cancel);
 47    buttonPanel.add(Box.createGlue());
 48    content.add(buttonPanel, &#8220;<span class="quote">South</span>&#8221;);
 49
 50<a name="add-listeners"></a>    // register this method as an ActionListener for
 51    // the buttons and text fields (see <a class="xref" href="macro-analysis.html#explain-add-listeners" title="Register the Action Listeners">the section called &#8220;Register the Action Listeners&#8221;</a>)
 52    ok.addActionListener(this);
 53    cancel.addActionListener(this);
 54    prefixField.addActionListener(this);
 55    suffixField.addActionListener(this);
 56
 57<a name="set-visible"></a>    // locate the dialog in the center of the
 58    // editing pane and make it visible (see <a class="xref" href="macro-analysis.html#explain-set-visible" title="Make the Dialog Visible">the section called &#8220;Make the Dialog Visible&#8221;</a>)
 59    dialog.pack();
 60    dialog.setLocationRelativeTo(view);
 61    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
 62    dialog.setVisible(true);
 63
 64<a name="action-listener"></a>    // this method will be called when a button is clicked
 65    // or when ENTER is pressed (see <a class="xref" href="macro-analysis.html#explain-action-listener" title="The Action Listener">the section called &#8220;The Action Listener&#8221;</a>)
 66    void actionPerformed(e)
 67    {
 68        if(e.getSource() != cancel)
 69        {
 70            processText();
 71        }
 72        dialog.dispose();
 73    }
 74
 75<a name="process-text"></a>    // this is where the work gets done to insert
 76    // the prefix and suffix (see <a class="xref" href="macro-analysis.html#explain-process-text" title="Get the User's Input">the section called &#8220;Get the User's Input&#8221;</a>)
 77    void processText()
 78    {
 79        prefix = prefixField.getText();
 80        suffix = suffixField.getText();
 81        if(prefix.length() == 0 &amp;&amp; suffix.length() == 0)
 82            return;
 83        prefixField.addCurrentToHistory();
 84        suffixField.addCurrentToHistory();
 85
 86<a name="jEdit-calls"></a>        // text manipulation begins here using calls
 87        // to jEdit methods  (see <a class="xref" href="macro-analysis.html#explain-jedit-calls" title="Call jEdit Methods to Manipulate Text">the section called &#8220;Call jEdit Methods to Manipulate Text&#8221;</a>)
 88        buffer.beginCompoundEdit();
 89        selectedLines = textArea.getSelectedLines();
 90        for(i = 0; i &lt; selectedLines.length; ++i)
 91        {
 92            offsetBOL = textArea.getLineStartOffset(
 93                selectedLines[i]);
 94            textArea.setCaretPosition(offsetBOL);
 95            textArea.goToStartOfWhiteSpace(false);
 96            textArea.goToEndOfWhiteSpace(true);
 97            text = textArea.getSelectedText();
 98            if(text == null) text = "";
 99            textArea.setSelectedText(prefix + text + suffix);
100        }
101        buffer.endCompoundEdit();
102    }
103}
104
105<a name="main"></a>// this single line of code is the script's main routine
106// (see <a class="xref" href="macro-analysis.html#explain-main" title="The Main Routine">the section called &#8220;The Main Routine&#8221;</a>)
107prefixSuffixDialog();
108
109// end of Add_Prefix_and_Suffix.bsh</pre></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="dialog-macro-intro.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="dialog-macro.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="macro-analysis.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Use of the Macro </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Analysis of the Macro</td></tr></table></div></body></html>