PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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