PageRenderTime 26ms CodeModel.GetById 20ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms


HTML | 88 lines | 83 code | 5 blank | 0 comment | 0 complexity | f869234bd5ef5fca7985cc08a1c758bc MD5 | raw file
 1<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.65.1"><link rel="home" href="index.html" title="jEdit 4.2 User's Guide"><link rel="up" href="macro-tips.html" title="Chapter 15. Macro Tips and Techniques"><link rel="previous" href="scripts-command-line.html" title="Running Scripts from the Command
 2Line"><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="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="macro-tips-BeanShell"></a>Advanced BeanShell Techniques</h2></div></div><div></div></div><p>
 3  BeanShell has a few advanced features that we haven't mentioned yet.
 4  They will be discussed in this section.
 5</p><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="macro-tips-BeanShell-convenience"></a>BeanShell's Convenience Syntax</h3></div></div><div></div></div><p>
 6  We noted earlier that BeanShell syntax does not require that variables
 7  be declared or defined with their type, and that variables that are
 8  not typed when first used can have values of differing types assigned
 9  to them. In addition to this &#8220;<span class="quote">loose</span>&#8221; syntax, BeanShell
10  allows a &#8220;<span class="quote">convenience</span>&#8221; syntax for dealing with the
11  properties of JavaBeans.  They may be accessed or set as if they were
12  data members. They may also be accessed using the name of the property
13  enclosed in quotation marks and curly brackets.  For example, the following
14  statement are all equivalent, assuming <tt class="varname">btn</tt> is
15  a <tt class="classname">JButton</tt> instance:
16</p><div class="informalexample"><table border="0" bgcolor="#E0E0E0"><tr><td><pre class="programlisting">b.setText("Choose");
17b.text = "Choose";
18b{"text"} = "Choose";
20  The last form can also be used to access a key-value pair of a
21  <tt class="classname">Hashtable</tt> object.
22</p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="macro-tips-BeanShell-keywords"></a>Special BeanShell Keywords</h3></div></div><div></div></div><p>
23  BeanShell uses special keywords to refer to variables or methods defined in
24  the current or an enclosing block's scope:
25</p><div class="itemizedlist"><ul type="disc"><li><p>
26      The keyword <tt class="function">this</tt> refers to the current scope.
27    </p></li><li><p>
28      The keyword <tt class="function">super</tt> refers to the immediately
29      enclosing scope.
30    </p></li><li><p>
31      The keyword <tt class="function">global</tt> refers to the top-level
32      scope of the macro script.
33    </p></li></ul></div><p>
34  The following script illustrates the use of these keywords:
35</p><div class="informalexample"><table border="0" bgcolor="#E0E0E0"><tr><td><pre class="programlisting">a = "top\n";
36foo() {
37    a = "middle\n";
38    bar() {
39        a = "bottom\n";
40        textArea.setSelectedText(global.a);
41        textArea.setSelectedText(super.a);
42        // equivalent to textArea.setSelectedText(this.a):
43        textArea.setSelectedText(a);
44    }
46    bar();
49  When the script is run, the following text is inserted in the current
50  buffer:
51</p><table border="0" bgcolor="#E0E0E0"><tr><td><pre class="screen">top
53bottom</pre></td></tr></table></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="macro-tips-BeanShell-class"></a>Implementing Classes and Interfaces</h3></div></div><div></div></div><p>
54  As discussed in the macro example in <a 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
55  interfaces such as <tt class="classname">ActionListener</tt>. For example:
56</p><table border="0" bgcolor="#E0E0E0"><tr><td><pre class="programlisting">myRunnable() {
57    run() {
58        System.out.println("Hello world!");
59    }
61    return this;
64Runnable r = myRunnable();
65new Thread(r).start();</pre></td></tr></table><p>
66  Frequently it will not be necessary to implement all of the methods of
67  a particular interface in order to specify the behavior of a scripted
68  object. To prevent BeanShell from throwing exceptions for missing interface methods, implement the <tt class="function">invoke()</tt> method,
69  which is called when an undefined method is invoked on a scripted
70  object. Typically, the implementation of this method will do nothing,
71  as in the following example:
72</p><div class="informalexample"><table border="0" bgcolor="#E0E0E0"><tr><td><pre class="programlisting">invoke(method, args) {}</pre></td></tr></table></div><p>
73  In addition to the implicit interface definitions described above,
74  BeanShell permits full-blown classes to be defined. Indeed, almost any Java class definition should work in BeanShell:
75</p><table border="0" bgcolor="#E0E0E0"><tr><td><pre class="programlisting">class Cons {
76    // Long-live LISP!
77    Object car;
78    Object cdr;
80    rplaca(Object car) {
81 = car;
82    }
84    rplacd(Object cdr) {
85        this.cdr = cdr;
86    }
87}</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="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
88Line </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>