PageRenderTime 134ms CodeModel.GetById 123ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 1ms

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

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