/www/tags/NOV_07_2009/htdocs/42docs/users-guide/first-example.html
HTML | 138 lines | 138 code | 0 blank | 0 comment | 0 complexity | 1c6d4c2b2d08f0cf0cf776b8c0dc2e8f 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.65.1"><link rel="home" href="index.html" title="jEdit 4.2 User's Guide"><link rel="up" href="macro-basics.html" title="Chapter 13. Macro Basics"><link rel="previous" 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></div><div class="informalexample"><table border="0" bgcolor="#E0E0E0"><tr><td><pre class="programlisting">Macros.message(view, "Hello world!");</pre></td></tr></table></div><p>
- Running this one line script causes jEdit to display a message
- box (more precisely, a <tt class="classname">JOptionPane</tt> object) with
- the traditional beginner's message and an <span><b class="guilabel">OK</b></span> button.
- Let's see what is happening here.
- </p><p>
- This statement calls a static method (or function) named
- <tt class="function">message</tt> in jEdit's <a 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 <i class="glossterm">object</i> is a collection of data that can be
- initialized, accessed and manipulated in certain defined ways.
- </p></li><li><p>
- A <i class="glossterm">class</i> 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 <i class="glossterm">subclass</i> (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 <i class="glossterm">method</i> (or function) is a procedure that works
- with data in a particular object, other data (including other objects)
- supplied as <i class="glossterm">parameters</i>, or both. Methods
- typically are applied to a particular object which is an
- <i class="glossterm">instance</i> of the class to which the method
- belongs.
- </p></li><li><p>
- A <i class="glossterm">static method</i> 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 <i class="firstterm">Java classes</i> and <i class="firstterm">jEdit
- classes</i> 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
- <tt class="function">Macros.message()</tt> has two parameters because that is
- the way the method is defined in the <a 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, <i class="parameter"><tt>view</tt></i>, is a variable naming
- the current, active <a href="../api/org/gjt/sp/jedit/View.html" target="_top">View</a> object. Information
- about pre-defined variables can be found in <a 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
- <i class="glossterm">string literal</i> - 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 <tt class="classname">String</tt>
- object. Normally, if you want to create an object in Java or BeanShell,
- you must construct the object using the <tt class="function">new</tt> keyword
- and a <i class="firstterm">constructor</i> 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 <tt class="classname">String</tt>.
- </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 <tt class="function">main()</tt> method in an anonymous class. That is
- in fact how BeanShell is implemented; the class is derived from
- a BeanShell class called <a 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
- <tt class="function">package</tt> 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 <tt class="function">import</tt> statements in this
- script. In Java, an <tt class="function">import</tt> 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 <tt class="function">import</tt>
- statements. For the same reason, most BeanShell scripts you write will
- not require <tt class="function">import</tt> statements.
- </p><p>
- Java requires <tt class="literal">import</tt> statement to be located
- at the beginning of a source file. BeanShell allows you to place
- <tt class="literal">import</tt>
- statements anywhere in a script, including inside a block of
- statements. The <tt class="literal">import</tt> 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>
- Here is the full list of packages automatically imported by jEdit:
- </p><table border="0" bgcolor="#E0E0E0"><tr><td><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></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="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>