/www/tags/NOV_07_2009/htdocs/users-guide/first-example.html
HTML | 113 lines | 113 code | 0 blank | 0 comment | 0 complexity | d57e458e0e8e56222fee0ca6379313ae 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>The Mandatory First Example</title><meta name="generator" content="DocBook XSL Stylesheets V1.73.2"><link rel="start" href="index.html" title="jEdit 4.3 User's Guide"><link rel="up" href="macro-basics.html" title="Chapter 13. Macro Basics"><link rel="prev" href="single-macros.html" title="Single Execution Macros"><link rel="next" href="predefined-variables.html" title="Predefined Variables in BeanShell"></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">The Mandatory First Example</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="single-macros.html">Prev</a> </td><th width="60%" align="center">Chapter 13. Macro Basics</th><td width="20%" align="right"> <a accesskey="n" href="predefined-variables.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="first-example"></a>The Mandatory First Example</h2></div></div></div><div class="informalexample"><pre class="programlisting">Macros.message(view, "Hello world!");</pre></div><p>Running this one line script causes jEdit to display a message box
- (more precisely, a <code class="classname">JOptionPane</code> object) with the
- traditional beginner's message and an <span class="guilabel"><strong>OK</strong></span> button.
- Let's see what is happening here.</p><p>This statement calls a static method (or function) named
- <code class="function">message</code> in jEdit's <a class="ulink" href="../api/org/gjt/sp/jedit/Macros.html" target="_top">Macros</a> class. If you
- don't know anything about classes or static methods or Java (or C++,
- which employs the same concept), you will need to gain some
- understanding of a few terms. Obviously this is not the place for
- academic precision, but if you are entirely new to object-oriented
- programming, here are a few skeleton ideas to help you with
- BeanShell.</p><div class="itemizedlist"><ul type="disc"><li><p>An <em class="glossterm">object</em> is a collection of data
- that can be initialized, accessed and manipulated in certain
- defined ways.</p></li><li><p>A <em class="glossterm">class</em> is a specification of what
- data an object contains and what methods can be used to work
- with the data. A Java application consists of one or more
- classes (in the case of jEdit ,over 600 classes) written by the
- programmer that defines the application's behavior. A BeanShell
- macro uses these classes, along with built-in classes that are
- supplied with the Java platform, to define its own
- behavior.</p></li><li><p>A <em class="glossterm">subclass</em> (or child class) is a
- class which uses (or “<span class="quote">inherits</span>”) the data and
- methods of its parent class along with additions or
- modifications that alter the subclass's behavior. Classes are
- typically organized in hierarchies of parent and child classes
- to organize program code, to define common behavior in shared
- parent class code, and to specify the types of similar behavior
- that child classes will perform in their own specific
- ways.</p></li><li><p>A <em class="glossterm">method</em> (or function) is a
- procedure that works with data in a particular object, other
- data (including other objects) supplied as
- <em class="glossterm">parameters</em>, or both. Methods typically
- are applied to a particular object which is an
- <em class="glossterm">instance</em> of the class to which the method
- belongs.</p></li><li><p>A <em class="glossterm">static method</em> differs from other
- methods in that it does not deal with the data in a particular
- object but is included within a class for the sake of
- convenience.</p></li></ul></div><p>Java has a rich set of classes defined as part of the Java
- platform. Like all Java applications, jEdit is organized as a set of
- classes that are themselves derived from the Java platform's classes. We
- will refer to <em class="firstterm">Java classes</em> and <em class="firstterm">jEdit
- classes</em> to make this distinction. Some of jEdit's classes
- (such as those dealing with regular expressions and XML) are derived
- from or make use of classes in other open-source Java packages. Except
- for BeanShell itself, we won't be discussing them in this guide.</p><p>In our one line script, the static method
- <code class="function">Macros.message()</code> has two parameters because that is
- the way the method is defined in the <a class="ulink" href="../api/org/gjt/sp/jedit/Macros.html" target="_top">Macros</a> class. You must
- specify both parameters when you call the function. The first parameter,
- <em class="parameter"><code>view</code></em>, is a variable naming the current, active
- <a class="ulink" href="../api/org/gjt/sp/jedit/View.html" target="_top">View</a> object.
- Information about pre-defined variables can be found in <a class="xref" href="predefined-variables.html" title="Predefined Variables in BeanShell">the section called “Predefined Variables in BeanShell”</a>.</p><p>The second parameter, which appears to be quoted text, is a
- <em class="glossterm">string literal</em> - a sequence of characters of
- fixed length and content. Behind the scenes, BeanShell and Java take
- this string literal and use it to create a <code class="classname">String</code>
- object. Normally, if you want to create an object in Java or BeanShell,
- you must construct the object using the <code class="function">new</code> keyword
- and a <em class="firstterm">constructor</em> method that is part of the
- object's class. We'll show an example of that later. However, both Java
- and BeanShell let you use a string literal anytime a method's parameter
- calls for a <code class="classname">String</code>.</p><p>If you are a Java programmer, you might wonder about a few things
- missing from this one line program. There is no class definition, for
- example. You can think of a BeanShell script as an implicit definition
- of a <code class="function">main()</code> method in an anonymous class. That is
- in fact how BeanShell is implemented; the class is derived from a
- BeanShell class called <a class="ulink" href="../api/bsh/XThis.html" target="_top">XThis</a>.
- If you don't find that helpful, just think of a script as one or more
- blocks of procedural statements conforming to Java syntax rules. You
- will also get along fine (for the most part) with C or C++ syntax if you
- leave out anything to do with pointers or memory management - Java and
- BeanShell do not have pointers and deal with memory management
- automatically.</p><p>Another missing item from a Java perspective is a
- <code class="function">package</code> statement. In Java, such a statement is
- used to bundle together a number of files so that their classes become
- visible to one another. Packages are not part of BeanShell, and you
- don't need to know anything about them to write BeanShell macros.</p><p>Finally, there are no <code class="function">import</code> statements in
- this script. In Java, an <code class="function">import</code> statement makes
- public classes from other packages visible within the file in which the
- statement occurs without having to specify a fully qualified class name.
- Without an import statement or a fully qualified name, Java cannot
- identify most classes using a single name as an identifier.</p><p>jEdit automatically imports a number of commonly-used packages
- into the namespace of every BeanShell script. Because of this, the
- script output of a recorded macro does not contain
- <code class="function">import</code> statements. For the same reason, most
- BeanShell scripts you write will not require <code class="function">import</code>
- statements.</p><p>Java requires <code class="literal">import</code> statement to be located at
- the beginning of a source file. BeanShell allows you to place
- <code class="literal">import</code> statements anywhere in a script, including
- inside a block of statements. The <code class="literal">import</code> statement
- will cover all names used following the statement in the enclosing
- block.</p><p>If you try to use a class that is not imported without its
- fully-qualified name, the BeanShell interpreter will complain with an
- error message relating to the offending line of code.</p><div class="sidebar"><p class="title"><b></b></p><p>Here is the full list of packages automatically imported by
- jEdit:</p><pre class="programlisting">java.awt
- java.awt.event
- java.net
- java.util
- java.io
- java.lang
- javax.swing
- javax.swing.event
- org.gjt.sp.jedit
- org.gjt.sp.jedit.browser
- org.gjt.sp.jedit.buffer
- org.gjt.sp.jedit.gui
- org.gjt.sp.jedit.help
- org.gjt.sp.jedit.io
- org.gjt.sp.jedit.msg
- org.gjt.sp.jedit.options
- org.gjt.sp.jedit.pluginmgr
- org.gjt.sp.jedit.print
- org.gjt.sp.jedit.search
- org.gjt.sp.jedit.syntax
- org.gjt.sp.jedit.textarea
- org.gjt.sp.util</pre></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="single-macros.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="predefined-variables.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Single Execution Macros </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Predefined Variables in BeanShell</td></tr></table></div></body></html>