<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Advanced BeanShell Techniques</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-tips.html" title="Chapter 15. Macro Tips and Techniques"><link rel="prev" href="scripts-command-line.html" title="Running Scripts from the Command Line"><link rel="next" href="macro-tips-debugging.html" title="Debugging Macros"></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">Advanced BeanShell Techniques</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="scripts-command-line.html">Prev</a> </td><th width="60%" align="center">Chapter 15. Macro Tips and Techniques</th><td width="20%" align="right"> <a accesskey="n" href="macro-tips-debugging.html">Next</a></td></tr></table><hr></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="macro-tips-BeanShell"></a>Advanced BeanShell Techniques</h2></div></div></div><p>BeanShell has a few advanced features that we haven't mentioned
yet. They will be discussed in this section.</p><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="macro-tips-BeanShell-convenience"></a>BeanShell's Convenience Syntax We noted earlier that BeanShell syntax does not require that
variables be declared or defined with their type, and that variables
that are not typed when first used can have values of differing
types assigned to them. In addition to this <span class="quote">“<span class="quote">loose</span>”</span>
syntax, BeanShell allows a <span class="quote">“<span class="quote">convenience</span>”</span> syntax for
dealing with the properties of JavaBeans. They may be accessed or
set as if they were data members. They may also be accessed using
the name of the property enclosed in quotation marks and curly
brackets. For example, the following statement are all equivalent,
assuming <code class="varname">btn</code> is a <code class="classname">JButton</code>
instance:</p><div class="informalexample"><pre class="programlisting">b.setText("Choose");
b.text = "Choose";
b{"text"} = "Choose";
</pre></div><p>The last form can also be used to access a key-value pair of a
<code class="classname">Hashtable</code> object.</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="macro-tips-BeanShell-keywords"></a>Special BeanShell Keywords</h3></div></div></div><p>BeanShell uses special keywords to refer to variables or
methods defined in the current or an enclosing block's scope: The keyword this refers to the
current scope.</p></li><li class="listitem"><p>The keyword <code class="function">super</code> refers to the
immediately enclosing scope.</p></li><li class="listitem"><p>The keyword <code class="function">global</code> refers to the
top-level scope of the macro script.</p></li></ul></div><p>The following script illustrates the use of these
keywords:</p><div class="informalexample"><pre class="programlisting">a = "top\n";
foo() {
a = "middle\n";
bar() {
a = "bottom\n";
textArea.setSelectedText(global.a);
textArea.setSelectedText(super.a);
// equivalent to textArea.setSelectedText(this.a):
textArea.setSelectedText(a);
}
bar();
}
foo();</pre></div><p>When the script is run, the following text is inserted in the
current buffer:</p><pre class="screen">top
middle
bottom</pre></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="macro-tips-BeanShell-class"></a>Implementing Classes and Interfaces</h3></div></div></div><p>As discussed in the macro example in <a class="xref" href="dialog-macro.html" title="Chapter 14. A Dialog-Based Macro">Chapter 14, <i>A Dialog-Based Macro</i></a>, scripted objects can implicitly implement
Java interfaces such as <code class="classname">ActionListener</code>. For
example:</p><pre class="programlisting">myRunnable() {
run() {
System.out.println("Hello world!");
}
return this;
}
Runnable r = myRunnable();
new Thread(r).start();</pre><p>Frequently it will not be necessary to implement all of the
methods of a particular interface in order to specify the behavior
of a scripted object. To prevent BeanShell from throwing exceptions
for missing interface methods, implement the
<code class="function">invoke()</code> method, which is called when an
undefined method is invoked on a scripted object. Typically, the
implementation of this method will do nothing, as in the following
example:</p><div class="informalexample"><pre class="programlisting">invoke(method, args) {}</pre></div><p>In addition to the implicit interface definitions described
above, BeanShell permits full-blown classes to be defined. Indeed,
almost any Java class definition should work in BeanShell:</p><pre class="programlisting">class Cons {
// Long-live LISP!
Object car;
Object cdr;
rplaca(Object car) {
this.car = car;
}
rplacd(Object cdr) {
this.cdr = cdr;
}
}</pre></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="scripts-command-line.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="macro-tips.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="macro-tips-debugging.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Running Scripts from the Command Line </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Debugging Macros</td></tr></table></div></body></html>
[ Dauer der Verarbeitung: 0.18 Sekunden
(vorverarbeitet)
]
|