PageRenderTime 42ms CodeModel.GetById 35ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms


HTML | 88 lines | 85 code | 3 blank | 0 comment | 0 complexity | d50e7ec5a2d1b68c76dfa53bbbd39d9a MD5 | raw file
 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();
 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)
22String newText = buffer.getName();
24// today's date
25import java.text.DateFormat;
27String newText = DateFormat.getDateInstance()
28    .format(new Date());
30// a line count for the current buffer
31String newText = "This file contains "
32    + textArea.getLineCount() + " lines.";</pre></td></tr></table></div><p>
33    Here are brief comments on each:
34  </p><div class="itemizedlist"><ul type="disc"><li><p>
35      In the first, the call to <tt class="function">getName()</tt> invokes
36      another method of the <a href="../api/org/gjt/sp/jedit/Buffer.html" target="_top">Buffer</a> class.
37    </p></li><li><p>
38      The syntax of the second example chains the results of
39      several methods. You could write it this way:
40    </p><table border="0" bgcolor="#E0E0E0"><tr><td><pre class="programlisting">import java.text.DateFormat;
41Date d = new Date();
42DateFormat df = DateFormat.getDateInstance();
43String result = df.format(d);
45	  Taking the pieces in order:
46   </p><div class="itemizedlist"><ul type="circle"><li><p>
47         A Java <tt class="classname">Date</tt> object is created using the
48         <tt class="function">new</tt> keyword. The empty parenthesis after
49         <tt class="classname">Date</tt> signify a call on the <i class="glossterm">
50         constructor method</i> of <tt class="classname">Date</tt> having no
51         parameters; here, a <tt class="classname">Date</tt> is created representing
52         the current date and time.
53       </p></li><li><p>
54         <tt class="function">DateFormat.getDateInstance()</tt> is a static method
55         that creates and returns a <tt class="classname">DateFormat</tt> object. As
56         the name implies, <tt class="classname">DateFormat</tt> is a Java class
57         that takes <tt class="classname">Date</tt> objects and produces readable
58         text. The method <tt class="function">getDateInstance()</tt> returns a
59         <tt class="classname">DateFormat</tt> object that parses and formats dates.
60         It will use the default <i class="glossterm">locale</i> or text format
61         specified in the user's Java installation.
62       </p></li><li><p>
63        Finally, <tt class="classname">DateFormat.format()</tt> is called on the
64        new <tt class="classname">DateFormat</tt> object using the
65        <tt class="classname">Date</tt> object as a parameter. The result is a
66        <tt class="classname">String</tt> containing the date in the default
67        locale.
68      </p></li><li><p>
69        Note that the <tt class="classname">Date</tt> class is contained in
70        the <tt class="literal">java.util</tt> package, so an explicit import
71        statement is not required. However, <tt class="classname">DateFormat</tt>
72        is part of the <tt class="literal">java.text</tt> package, which is
73        not automatically imported, so an explicit
74        <tt class="function">import</tt> statement must be used.
75      </p></li></ul></div></li><li><p>
76    The third example shows three items of note:
77	 </p><div class="itemizedlist"><ul type="circle"><li><p>
78       <tt class="function">getLineCount()</tt> is a method in jEdit's
79       <a href="../api/org/gjt/sp/jedit/textarea/JEditTextArea.html" target="_top">JEditTextArea</a> class. It returns an
80       <tt class="classname">int</tt> representing the number of lines in the
81       current text buffer. We call it on <tt class="varname">textArea</tt>, the
82       pre-defined, current <a href="../api/org/gjt/sp/jedit/textarea/JEditTextArea.html" target="_top">JEditTextArea</a> object.
83      </p></li><li><p>
84       The use of the <tt class="function">+</tt> operator (which can be chained,
85       as here) appends objects and string
86       literals to return a single, concatenated <tt class="classname">String</tt>.
87       </p></li></ul></div><p>
88   </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>