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

/www/tags/NOV_07_2009/htdocs/42docs/users-guide/something-useful.html

#
HTML | 88 lines | 85 code | 3 blank | 0 comment | 0 complexity | d50e7ec5a2d1b68c76dfa53bbbd39d9a 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>Now For Something Useful</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="macro-basics.html" title="Chapter 13. Macro Basics"><link rel="previous" href="dynamic-typing.html" title="BeanShell Dynamic Typing"><link rel="next" href="dialog-macro.html" title="Chapter 14. A Dialog-Based 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">Now For Something Useful</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="dynamic-typing.html">Prev</a> </td><th width="60%" align="center">Chapter 13. Macro Basics</th><td width="20%" align="right"> <a accesskey="n" href="dialog-macro.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="something-useful"></a>Now For Something Useful</h2></div></div><div></div></div><p>
  2. Here is a macro that inserts the path of the current buffer in
  3. the text:
  4. </p><div class="informalexample"><table border="0" bgcolor="#E0E0E0"><tr><td><pre class="programlisting">String newText = buffer.getPath();
  5. textArea.setSelectedText(newText);</pre></td></tr></table></div><p>
  6. Unlike in our first macro example, here we are calling class methods on
  7. particular objects. First, we call <tt class="function">getPath()</tt> on the
  8. current <a href="../api/org/gjt/sp/jedit/Buffer.html" target="_top">Buffer</a> object to get the full path of the
  9. text file currently being edited. Next, we call
  10. <tt class="function">setSelectedText()</tt> on the current text display
  11. component, specifying the text to be inserted as a parameter.
  12. </p><p>
  13. In precise terms, the <tt class="function">setSelectedText()</tt> method
  14. substitutes the contents of the <tt class="classname">String</tt>
  15. parameter for a range of selected text that includes the current caret
  16. position. If no text is selected at the caret position, the effect
  17. of this operation is simply to insert the new text at that position.
  18. </p><p>
  19. Here's a few alternatives to the full file path that you could
  20. use to insert various useful things:
  21. </p><div class="informalexample"><table border="0" bgcolor="#E0E0E0"><tr><td><pre class="programlisting">// the file name (without full path)
  22. String newText = buffer.getName();
  23. // today's date
  24. import java.text.DateFormat;
  25. String newText = DateFormat.getDateInstance()
  26. .format(new Date());
  27. // a line count for the current buffer
  28. String newText = "This file contains "
  29. + textArea.getLineCount() + " lines.";</pre></td></tr></table></div><p>
  30. Here are brief comments on each:
  31. </p><div class="itemizedlist"><ul type="disc"><li><p>
  32. In the first, the call to <tt class="function">getName()</tt> invokes
  33. another method of the <a href="../api/org/gjt/sp/jedit/Buffer.html" target="_top">Buffer</a> class.
  34. </p></li><li><p>
  35. The syntax of the second example chains the results of
  36. several methods. You could write it this way:
  37. </p><table border="0" bgcolor="#E0E0E0"><tr><td><pre class="programlisting">import java.text.DateFormat;
  38. Date d = new Date();
  39. DateFormat df = DateFormat.getDateInstance();
  40. String result = df.format(d);
  41. </pre></td></tr></table><p>
  42. Taking the pieces in order:
  43. </p><div class="itemizedlist"><ul type="circle"><li><p>
  44. A Java <tt class="classname">Date</tt> object is created using the
  45. <tt class="function">new</tt> keyword. The empty parenthesis after
  46. <tt class="classname">Date</tt> signify a call on the <i class="glossterm">
  47. constructor method</i> of <tt class="classname">Date</tt> having no
  48. parameters; here, a <tt class="classname">Date</tt> is created representing
  49. the current date and time.
  50. </p></li><li><p>
  51. <tt class="function">DateFormat.getDateInstance()</tt> is a static method
  52. that creates and returns a <tt class="classname">DateFormat</tt> object. As
  53. the name implies, <tt class="classname">DateFormat</tt> is a Java class
  54. that takes <tt class="classname">Date</tt> objects and produces readable
  55. text. The method <tt class="function">getDateInstance()</tt> returns a
  56. <tt class="classname">DateFormat</tt> object that parses and formats dates.
  57. It will use the default <i class="glossterm">locale</i> or text format
  58. specified in the user's Java installation.
  59. </p></li><li><p>
  60. Finally, <tt class="classname">DateFormat.format()</tt> is called on the
  61. new <tt class="classname">DateFormat</tt> object using the
  62. <tt class="classname">Date</tt> object as a parameter. The result is a
  63. <tt class="classname">String</tt> containing the date in the default
  64. locale.
  65. </p></li><li><p>
  66. Note that the <tt class="classname">Date</tt> class is contained in
  67. the <tt class="literal">java.util</tt> package, so an explicit import
  68. statement is not required. However, <tt class="classname">DateFormat</tt>
  69. is part of the <tt class="literal">java.text</tt> package, which is
  70. not automatically imported, so an explicit
  71. <tt class="function">import</tt> statement must be used.
  72. </p></li></ul></div></li><li><p>
  73. The third example shows three items of note:
  74. </p><div class="itemizedlist"><ul type="circle"><li><p>
  75. <tt class="function">getLineCount()</tt> is a method in jEdit's
  76. <a href="../api/org/gjt/sp/jedit/textarea/JEditTextArea.html" target="_top">JEditTextArea</a> class. It returns an
  77. <tt class="classname">int</tt> representing the number of lines in the
  78. current text buffer. We call it on <tt class="varname">textArea</tt>, the
  79. pre-defined, current <a href="../api/org/gjt/sp/jedit/textarea/JEditTextArea.html" target="_top">JEditTextArea</a> object.
  80. </p></li><li><p>
  81. The use of the <tt class="function">+</tt> operator (which can be chained,
  82. as here) appends objects and string
  83. literals to return a single, concatenated <tt class="classname">String</tt>.
  84. </p></li></ul></div><p>
  85. </p></li></ul></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="dynamic-typing.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="macro-basics.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="dialog-macro.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">BeanShell Dynamic Typing </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 14. A Dialog-Based Macro</td></tr></table></div></body></html>