/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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
- <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
- distribution in the <code class="filename">Text</code> subdirectory of the
- <code class="filename">macros</code> directory. You can also try it out by
- invoking
- <span class="guimenu"><strong>Macros</strong></span>><span class="guisubmenu"><strong>Text</strong></span>><span class="guimenuitem"><strong>Add
- Prefix and Suffix</strong></span>.</p><div class="informalexample"><pre class="programlisting">// beginning of Add_Prefix_and_Suffix.bsh
- <a name="imports"></a>// import statement (see <a class="xref" href="macro-analysis.html#explain-imports" title="Import Statements">the section called “Import Statements”</a>)
- import javax.swing.border.*;
- <a name="main-routine"></a>// main routine
- void prefixSuffixDialog()
- {
- <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 “Create the Dialog”</a>)
- title = “<span class="quote">Add prefix and suffix to selected lines</span>”;
- 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);
- <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 “Create the Text Fields”</a>)
- fieldPanel = new JPanel(new GridLayout(4, 1, 0, 6));
- prefixField = new HistoryTextField(“<span class="quote">macro.add-prefix</span>”);
- prefixLabel = new JLabel(“<span class="quote">Prefix to add:</span>”);
- suffixField = new HistoryTextField(“<span class="quote">macro.add-suffix</span>”);
- suffixLabel = new JLabel(“<span class="quote">Suffix to add:</span>”);
- fieldPanel.add(prefixLabel);
- fieldPanel.add(prefixField);
- fieldPanel.add(suffixLabel);
- fieldPanel.add(suffixField);
- content.add(fieldPanel, “<span class="quote">Center</span>”);
- <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 “Create the Buttons”</a>)
- 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(“<span class="quote">OK</span>”);
- cancel = new JButton(“<span class="quote">Cancel</span>”);
- 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, “<span class="quote">South</span>”);
- <a name="add-listeners"></a> // register this method as an ActionListener for
- // the buttons and text fields (see <a class="xref" href="macro-analysis.html#explain-add-listeners" title="Register the Action Listeners">the section called “Register the Action Listeners”</a>)
- ok.addActionListener(this);
- cancel.addActionListener(this);
- prefixField.addActionListener(this);
- suffixField.addActionListener(this);
- <a name="set-visible"></a> // locate the dialog in the center of the
- // 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 “Make the Dialog Visible”</a>)
- dialog.pack();
- dialog.setLocationRelativeTo(view);
- dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
- dialog.setVisible(true);
- <a name="action-listener"></a> // this method will be called when a button is clicked
- // or when ENTER is pressed (see <a class="xref" href="macro-analysis.html#explain-action-listener" title="The Action Listener">the section called “The Action Listener”</a>)
- void actionPerformed(e)
- {
- if(e.getSource() != cancel)
- {
- processText();
- }
- dialog.dispose();
- }
- <a name="process-text"></a> // this is where the work gets done to insert
- // the prefix and suffix (see <a class="xref" href="macro-analysis.html#explain-process-text" title="Get the User's Input">the section called “Get the User's Input”</a>)
- void processText()
- {
- prefix = prefixField.getText();
- suffix = suffixField.getText();
- if(prefix.length() == 0 && suffix.length() == 0)
- return;
- prefixField.addCurrentToHistory();
- suffixField.addCurrentToHistory();
- <a name="jEdit-calls"></a> // text manipulation begins here using calls
- // to jEdit methods (see <a class="xref" href="macro-analysis.html#explain-jedit-calls" title="Call jEdit Methods to Manipulate Text">the section called “Call jEdit Methods to Manipulate Text”</a>)
- 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();
- }
- }
- <a name="main"></a>// this single line of code is the script's main routine
- // (see <a class="xref" href="macro-analysis.html#explain-main" title="The Main Routine">the section called “The Main Routine”</a>)
- prefixSuffixDialog();
- // 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>