/www/tags/NOV_07_2009/htdocs/42docs/users-guide/macro-tips-BeanShell.html
HTML | 88 lines | 83 code | 5 blank | 0 comment | 0 complexity | f869234bd5ef5fca7985cc08a1c758bc 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
- <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
- 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="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>
- BeanShell has a few advanced features that we haven't mentioned yet.
- They will be discussed in this section.
- </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>
- 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">loose</span>” syntax, BeanShell
- allows a “<span class="quote">convenience</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 <tt class="varname">btn</tt> is
- a <tt class="classname">JButton</tt> instance:
- </p><div class="informalexample"><table border="0" bgcolor="#E0E0E0"><tr><td><pre class="programlisting">b.setText("Choose");
- b.text = "Choose";
- b{"text"} = "Choose";
- </pre></td></tr></table></div><p>
- The last form can also be used to access a key-value pair of a
- <tt class="classname">Hashtable</tt> object.
- </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>
- BeanShell uses special keywords to refer to variables or methods defined in
- the current or an enclosing block's scope:
- </p><div class="itemizedlist"><ul type="disc"><li><p>
- The keyword <tt class="function">this</tt> refers to the current scope.
- </p></li><li><p>
- The keyword <tt class="function">super</tt> refers to the immediately
- enclosing scope.
- </p></li><li><p>
- The keyword <tt class="function">global</tt> 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"><table border="0" bgcolor="#E0E0E0"><tr><td><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></td></tr></table></div><p>
- When the script is run, the following text is inserted in the current
- buffer:
- </p><table border="0" bgcolor="#E0E0E0"><tr><td><pre class="screen">top
- middle
- bottom</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>
- 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
- interfaces such as <tt class="classname">ActionListener</tt>. For example:
- </p><table border="0" bgcolor="#E0E0E0"><tr><td><pre class="programlisting">myRunnable() {
- run() {
- System.out.println("Hello world!");
- }
-
- return this;
- }
- Runnable r = myRunnable();
- new Thread(r).start();</pre></td></tr></table><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 <tt class="function">invoke()</tt> 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"><table border="0" bgcolor="#E0E0E0"><tr><td><pre class="programlisting">invoke(method, args) {}</pre></td></tr></table></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><table border="0" bgcolor="#E0E0E0"><tr><td><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></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
- 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>