PageRenderTime 107ms CodeModel.GetById 21ms RepoModel.GetById 3ms app.codeStats 0ms

/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
  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
  2. 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>
  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");
  17. b.text = "Choose";
  18. b{"text"} = "Choose";
  19. </pre></td></tr></table></div><p>
  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";
  36. foo() {
  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. }
  45. bar();
  46. }
  47. foo();</pre></td></tr></table></div><p>
  48. When the script is run, the following text is inserted in the current
  49. buffer:
  50. </p><table border="0" bgcolor="#E0E0E0"><tr><td><pre class="screen">top
  51. middle
  52. 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>
  53. 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
  54. interfaces such as <tt class="classname">ActionListener</tt>. For example:
  55. </p><table border="0" bgcolor="#E0E0E0"><tr><td><pre class="programlisting">myRunnable() {
  56. run() {
  57. System.out.println("Hello world!");
  58. }
  59. return this;
  60. }
  61. Runnable r = myRunnable();
  62. new Thread(r).start();</pre></td></tr></table><p>
  63. Frequently it will not be necessary to implement all of the methods of
  64. a particular interface in order to specify the behavior of a scripted
  65. object. To prevent BeanShell from throwing exceptions for missing interface methods, implement the <tt class="function">invoke()</tt> method,
  66. which is called when an undefined method is invoked on a scripted
  67. object. Typically, the implementation of this method will do nothing,
  68. as in the following example:
  69. </p><div class="informalexample"><table border="0" bgcolor="#E0E0E0"><tr><td><pre class="programlisting">invoke(method, args) {}</pre></td></tr></table></div><p>
  70. In addition to the implicit interface definitions described above,
  71. BeanShell permits full-blown classes to be defined. Indeed, almost any Java class definition should work in BeanShell:
  72. </p><table border="0" bgcolor="#E0E0E0"><tr><td><pre class="programlisting">class Cons {
  73. // Long-live LISP!
  74. Object car;
  75. Object cdr;
  76. rplaca(Object car) {
  77. this.car = car;
  78. }
  79. rplacd(Object cdr) {
  80. this.cdr = cdr;
  81. }
  82. }</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
  83. 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>