PageRenderTime 67ms CodeModel.GetById 25ms RepoModel.GetById 0ms 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
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
  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. <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>)
  8. import javax.swing.border.*;
  9. <a name="main-routine"></a>// main routine
  10. void prefixSuffixDialog()
  11. {
  12. <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>)
  13. title = &#8220;<span class="quote">Add prefix and suffix to selected lines</span>&#8221;;
  14. dialog = new JDialog(view, title, false);
  15. content = new JPanel(new BorderLayout());
  16. content.setBorder(new EmptyBorder(12, 12, 12, 12));
  17. content.setPreferredSize(new Dimension(320, 160));
  18. dialog.setContentPane(content);
  19. <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>)
  20. fieldPanel = new JPanel(new GridLayout(4, 1, 0, 6));
  21. prefixField = new HistoryTextField(&#8220;<span class="quote">macro.add-prefix</span>&#8221;);
  22. prefixLabel = new JLabel(&#8220;<span class="quote">Prefix to add:</span>&#8221;);
  23. suffixField = new HistoryTextField(&#8220;<span class="quote">macro.add-suffix</span>&#8221;);
  24. suffixLabel = new JLabel(&#8220;<span class="quote">Suffix to add:</span>&#8221;);
  25. fieldPanel.add(prefixLabel);
  26. fieldPanel.add(prefixField);
  27. fieldPanel.add(suffixLabel);
  28. fieldPanel.add(suffixField);
  29. content.add(fieldPanel, &#8220;<span class="quote">Center</span>&#8221;);
  30. <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>)
  31. buttonPanel = new JPanel();
  32. buttonPanel.setLayout(new BoxLayout(buttonPanel,
  33. BoxLayout.X_AXIS));
  34. buttonPanel.setBorder(new EmptyBorder(12, 50, 0, 50));
  35. buttonPanel.add(Box.createGlue());
  36. ok = new JButton(&#8220;<span class="quote">OK</span>&#8221;);
  37. cancel = new JButton(&#8220;<span class="quote">Cancel</span>&#8221;);
  38. ok.setPreferredSize(cancel.getPreferredSize());
  39. dialog.getRootPane().setDefaultButton(ok);
  40. buttonPanel.add(ok);
  41. buttonPanel.add(Box.createHorizontalStrut(6));
  42. buttonPanel.add(cancel);
  43. buttonPanel.add(Box.createGlue());
  44. content.add(buttonPanel, &#8220;<span class="quote">South</span>&#8221;);
  45. <a name="add-listeners"></a> // register this method as an ActionListener for
  46. // 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>)
  47. ok.addActionListener(this);
  48. cancel.addActionListener(this);
  49. prefixField.addActionListener(this);
  50. suffixField.addActionListener(this);
  51. <a name="set-visible"></a> // locate the dialog in the center of the
  52. // 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>)
  53. dialog.pack();
  54. dialog.setLocationRelativeTo(view);
  55. dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  56. dialog.setVisible(true);
  57. <a name="action-listener"></a> // this method will be called when a button is clicked
  58. // 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>)
  59. void actionPerformed(e)
  60. {
  61. if(e.getSource() != cancel)
  62. {
  63. processText();
  64. }
  65. dialog.dispose();
  66. }
  67. <a name="process-text"></a> // this is where the work gets done to insert
  68. // 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>)
  69. void processText()
  70. {
  71. prefix = prefixField.getText();
  72. suffix = suffixField.getText();
  73. if(prefix.length() == 0 &amp;&amp; suffix.length() == 0)
  74. return;
  75. prefixField.addCurrentToHistory();
  76. suffixField.addCurrentToHistory();
  77. <a name="jEdit-calls"></a> // text manipulation begins here using calls
  78. // 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>)
  79. buffer.beginCompoundEdit();
  80. selectedLines = textArea.getSelectedLines();
  81. for(i = 0; i &lt; selectedLines.length; ++i)
  82. {
  83. offsetBOL = textArea.getLineStartOffset(
  84. selectedLines[i]);
  85. textArea.setCaretPosition(offsetBOL);
  86. textArea.goToStartOfWhiteSpace(false);
  87. textArea.goToEndOfWhiteSpace(true);
  88. text = textArea.getSelectedText();
  89. if(text == null) text = "";
  90. textArea.setSelectedText(prefix + text + suffix);
  91. }
  92. buffer.endCompoundEdit();
  93. }
  94. }
  95. <a name="main"></a>// this single line of code is the script's main routine
  96. // (see <a class="xref" href="macro-analysis.html#explain-main" title="The Main Routine">the section called &#8220;The Main Routine&#8221;</a>)
  97. prefixSuffixDialog();
  98. // 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>