products/sources/formale sprachen/Isabelle/Tools/jEdit/dist/doc/users-guide/something-useful.html |
 |
<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.79.1"><link rel="home" href="index.html" title="jEdit 5.6 User's Guide"><link rel="up" href="macro-basics.html" title="Chapter 13. Macro Basics"><link rel="prev" 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="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="something-useful"></a>Now For Something Useful</h2></div></div></div><p>Here is a macro that inserts the path of the current buffer in the
text:</p><div class="informalexample"><pre class="programlisting">String newText = buffer.getPath();
textArea.setSelectedText(newText);</pre></div><p>Unlike in our first macro example, here we are calling class
methods on particular objects. First, we call
<code class="function">getPath()</code> on the current <a class="ulink" href="../api/org/gjt/sp/jedit/Buffer.html" target="_top">Buffer</a> object to get
the full path of the text file currently being edited. Next, we call
<code class="function">setSelectedText()</code> on the current text display
component, specifying the text to be inserted as a parameter.</p><p>In precise terms, the <code class="function">setSelectedText()</code>
method substitutes the contents of the <code class="classname">String</code>
parameter for a range of selected text that includes the current caret
position. If no text is selected at the caret position, the effect of
this operation is simply to insert the new text at that position.</p><p>Here's a few alternatives to the full file path that you could use
to insert various useful things:</p><div class="informalexample"><pre class="programlisting">// the file name (without full path)
String newText = buffer.getName();
// today's date
import java.text.DateFormat;
String newText = DateFormat.getDateInstance()
.format(new Date());
// a line count for the current buffer
String newText = "This file contains "
+ textArea.getLineCount() + " lines.";</pre></div><p>Here are brief comments on each:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>In the first, the call to <code class="function">getName()</code>
invokes another method of the <a class="ulink" href="../api/org/gjt/sp/jedit/Buffer.html" target="_top">Buffer</a>
class.</p></li><li class="listitem"><p>The syntax of the second example chains the results of
several methods. You could write it this way:</p><pre class="programlisting">import java.text.DateFormat;
Date d = new Date();
DateFormat df = DateFormat.getDateInstance();
String result = df.format(d);
</pre><p>Taking the pieces in order:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"><p>A Java <code class="classname">Date</code> object is
created using the <code class="function">new</code> keyword. The
empty parenthesis after <code class="classname">Date</code>
signify a call on the <em class="glossterm"> constructor
method</em> of <code class="classname">Date</code> having
no parameters; here, a <code class="classname">Date</code> is
created representing the current date and time.</p></li><li class="listitem"><p><code class="function">DateFormat.getDateInstance()</code>
is a static method that creates and returns a
<code class="classname">DateFormat</code> object. As the name
implies, <code class="classname">DateFormat</code> is a Java
class that takes <code class="classname">Date</code> objects and
produces readable text. The method
<code class="function">getDateInstance()</code> returns a
<code class="classname">DateFormat</code> object that parses and
formats dates. It will use the default
<em class="glossterm">locale</em> or text format specified
in the user's Java installation.Finally,
<code class="classname">DateFormat.format()</code> is called on
the new <code class="classname">DateFormat</code> object using
the <code class="classname">Date</code> object as a parameter.
The result is a <code class="classname">String</code> containing
the date in the default locale.</p></li><li class="listitem"><p>Note that the <code class="classname">Date</code> class is
contained in the <code class="literal">java.util</code> package,
so an explicit import statement is not required.
However, <code class="classname">DateFormat</code> is part of
the <code class="literal">java.text</code> package, which is not
automatically imported, so an explicit
<code class="function">import</code> statement must be
used.</p></li></ul></div></li><li class="listitem"><p>The third example shows three items of note: </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"><p><code class="function">getLineCount()</code> is a method
in jEdit's JEditTextArea
class. It returns an <code class="classname">int</code>
representing the number of lines in the current text
buffer. We call it on <code class="varname">textArea</code>,
the pre-defined, current <a class="ulink" href="../api/org/gjt/sp/jedit/textarea/JEditTextArea.html" target="_top">JEditTextArea</a>
object.</p></li><li class="listitem"><p>The use of the <code class="function">+</code> operator
(which can be chained, as here) appends objects and
string literals to return a single, concatenated
<code class="classname">String</code>.</p></li></ul></div></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>
¤ Dauer der Verarbeitung: 0.2 Sekunden
(vorverarbeitet)
¤
|
Haftungshinweis
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.
|