Java 1.2 Unleashed

Contents


- 55 -

Java Command Language


Java is a remarkable programming language, but it isn't the only programming language to be developed at Sun. The Tool Command Language, or Tcl, is another extraordinary programming language that was developed (by John Ousterhout) at Sun. Tcl is an embedded scripting language that was originally created to support the rapid development of applications that run under the X Window System. However, Tcl's power, flexibility, and ease of use have caused it to be ported to almost all popular operating systems. Tcl is associated with the GUI toolkit named Tk, which provides a rich set of components for developing GUIs.


NOTE: Tcl is pronounced "tickle."

Because Java is a general-purpose, platform-independent programming language and Tcl is a cross-platform scripting language, the software engineers at Sun launched a research project to determine how the languages could be used together. The result of this effort is the Java Command Language (Jacl). Jacl is a 100% Java implementation of Tcl that allows Tcl scripts to take full advantage of the Java API. Jacl allows Tcl to be used as the scripting language for Java.

In this chapter, you'll be introduced to Jacl and learn how it provides programmers with the best features of both Java and Tcl. You'll use Tcl to quickly script an AWT-based Java application. You'll also learn how another product of the Java-Tcl research team, Tcl Blend, can be used to convert legacy C applications to Java. When you finish this chapter, you'll be able to use Tcl to script Java applications.

What's Jacl?

Jacl is the result of a very successful research effort to combine the best features of Java and Tcl. In a nutshell, Jacl is a 100% pure Java implementation of Tcl that give Tcl scripts full access to the Java API. Jacl enables Tcl to be used as the scripting language for Java applications. It lets you very quickly and easily generate GUI-based Java programs with just a few lines of Tcl code.

The power of Jacl lies in its capability to expose the Java API to Tcl. It does this through a software component referred to as the Java Package, which provides an interface between Java and Tcl. This interface provides Tcl scripts with the following capabilities:

Jacl also exploits the Reflection API to provide Tcl scripts with the capability to access the methods, properties, and events of JavaBeans. It allows Tcl scripts to create JavaBeans, invoke their methods, and get and set their properties. It also provides exceptional support of bean event handling. Bean events are exposed to Tcl, and these events may be handled in Tcl by binding Tcl code to the events.


NOTE: Don't worry if you are unfamiliar with Tcl. The next section provides an introductory Tcl primer.

One benefit of Jacl-to-Tcl programmers is that Jacl provides a platform-independent implementation of Tcl. This means that anywhere that Java will run, Tcl will also run. Another advantage is the feature-rich Java API. The capabilities of the Java API are reason alone for a Tcl programmer to switch from a C-based Tcl environment to Jacl.

Jacl is available for download from Sun's Web site at http://sunscript.sun.com/java/. It is packaged as a self-extracting installation file. You should download and install Jacl before going to the next section. Also, make sure that you put the \Jacl1.0 directory in your PATH.


NOTE: Jacl supports Tcl version 8.0. You do not need to install Tcl to run Jacl.

A Tcl Primer

This section teaches you how Tcl works and shows you how to write simple Tcl scripts. Having made it through 54 chapters of this book, you are on your way to becoming an accomplished programmer and should be able to pick up Tcl's syntax rather easily. The next section shows how to use Jacl to access the Java API from within Tcl scripts.

The syntax of Tcl, like Java, is based on C. It is organized into a set of commands that are executed by a Tcl interpreter. Tcl supports a single datatype--the string datatype. Everything in Tcl, including commands, is implemented using strings. The Tcl interpreter can be viewed as a very flexible and efficient string processor.

Command Syntax

Tcl commands are line-oriented, separated by the new line character or by semicolons. Commands consist of one or more fields, separated by spaces or tabs, where the first field is the name of the command. All fields after the first field are the arguments to the command. For example, the following command sets the value of the variable x to 5:

set x 5

Including Spaces as Part of a Field

Double quotes are used to include a space as part of a field. The following command sets the value of x to This is a test:

set x "This is a test"

Curly braces ({ and }) may also be used to delimit a field.

Command Substitution

Brackets ([ and ]) are used to delimit commands that are embedded in a field. Embedded commands are executed and their values are returned as part of the field in which they occur. Bracketed commands may span multiple lines. For example, the following commands set the value of x to 12:

set y 12

set x [set y]

In this command, the value returned by the command set y is 12.

Variable Substitution

The dollar sign ($) is used to substitute the value of a variable for the variable itself. For example, if the value of the variable y is 12, the following statement sets the value of x to 12:

set x $y

Arrays

Tcl arrays associate values with array elements. Array indices are not ordered as Java arrays and may consist of non-integer string values. Paired opening and closing parentheses are used to identify array indices. If a variable name is followed by an opening parenthesis, all characters up to the closing parenthesis are used to determine the array index. For example, if y(5) is 10, the following command assigns 10 to x:

set x y(5)

Escape Characters

Backslashes are used as escape characters in a similar manner as Java. For example, \n represents a new line character.

Comments

Tcl comments begin with the pound character (#) and extend to the end of the line on which they occur.

Expressions

Some Tcl commands treat their arguments as expressions. The operators used in Tcl expressions are based on C and are very similar to Java operators.

Lists

Tcl also supports list processing. Tcl lists are strings in which the individual list elements are separated by spaces, tabs, or new line characters. List elements are also delimited by curly braces. For example, the following list consists of the three elements a, {b c}, and {c {d e}}:

a {b c} {c {d e}}

The last element of the list is itself a list consisting of c and {d e}.

Variable Declaration

Tcl variables do not need to be declared.

Built-In Commands

Tcl supports a rich set of built-in commands. The following subsections cover some of the most important commands.

set

The set command is used to assign a value to a variable. It has the following syntax:

set variableName expression

The value of variableName is set to the result of expression. The set command can also be used to return a value when enclosed within brackets. The expression [set variableName] returns the value of variableName.

if

The Tcl if command is similar to that of other languages. It has the following form:

if { expression1 } {

 commands1

} elseif { expression2 }

 commands2

} else {

 commands3

}

Multiple elseif clauses may be used. The elseif and else clauses are optional and may be omitted. Note that the expressions are enclosed in curly braces instead of the parentheses used with Java.

for

The for command is similar to the Java statement. Its syntax follows:

for {start} {test} {update} {body}

The start is typically a set command that assigns a value to a loop iterator variable. The test is a condition that is evaluated to determine whether the loop should continue. The update updates the iterator between loop iterations. The body consists of commands that are the body of the for loop.

The following Tcl for command iterates i from 0 to 10 and executes commands:

for {set i 0} {$i < 10} {incr i} { commands }

break

The break command may be used to break out of a for command. It is used in the same way it is in Java. For example, the break command causes the following for loop to be exited when i is 5:

for {set i 0} {$i < 10} {incr i} {

 if {i == 5} break

}

Other Commands

Tcl supports many other commands, such as while, case, continue, and others. Consult an introductory Tcl book for more information on these commands. Tcl and the Tk Toolkit by John Ousterhout is the definitive reference on Tcl.

Built-In Variables

Tcl also supports several built-in variables that provide access to the environment and error codes.

Accessing Java from Tcl Scripts

Jacl makes it easy to access Java objects from Tcl scripts. The best way to learn how to do this is through an example. Listing 55.1 shows an example program named Demo.tcl. We'll run this program and then examine how it works. Make sure that you have Jacl installed and the \Jacl1.0 directory in your PATH.


TIP: The C:\Jacl\jacl.bat file assumes you have JDK 1.1. You must modify this file so that it contains the correct CLASSPATH for your JDK installation.

Open a console window and switch to the \ju\ch55 directory. Launch Jacl and enter the following command at the Jacl prompt:

%source Demo.tcl

The Java window shown in Figure 55.1 is displayed. This window contains a text field, a label, and a button. Click on the button and the text field is updated, as shown in Figure 55.2. When you are finished, close the window and exit Jacl by entering exit at the Jacl prompt.

FIGURE 55.1. The Demo.tcl opening window.

FIGURE 55.2. The text field is updated when the button is clicked.

LISTING 55.1. THE Demo.tcl SCRIPT.

set frame [java::new java.awt.Frame]

set panel [java::new java.awt.Panel]

$frame setLayout [java::new java.awt.BorderLayout]

$frame setTitle "Jacl Demo"

set label [java::new java.awt.Label]

$label setText "Click to update the text field:"

set button [java::new java.awt.Button]

$button setLabel "Update text"

$panel {add java.awt.Component} $label

$panel {add java.awt.Component} $button

$frame {add java.lang.String java.awt.Component} "Center" $panel

set text [java::new java.awt.TextField]

$text setText "An AWT TextField"

$frame {add java.lang.String java.awt.Component} "North" $text

$frame setSize 400 400

$frame show

$frame toFront

java::bind $frame windowClosing "set done yes"

set newText {"THE TEXT HAS BEEN UPDATED!!!"}

java::bind $button actionPerformed "$text setText $newText"

vwait done

$frame dispose

The first line of the script sets the value of [java::new java.awt.Frame] to the frame variable. The java::new command creates a new Java object. In this case, it is a Frame object. The end result is that a Frame object is created and assigned to the frame variable.

The next line creates a Panel object and assigns it to the panel variable.

The third line invokes the setLayout() method of the Frame object, passing it a new object of the BorderLayout class. Whenever a Java object is the first field in a command, the second field identifies the method being invoked and the remaining fields identify the arguments for the method invocation.

The next line invokes the setTitle() method of the Frame object with the "Jacl Demo" argument.

The fifth and sixth lines of the script create a Label object and assign it to the label variable. The label's text is set to "Click to update the text field:".

The next two lines create a Button object that is labeled Update text.

The ninth line of the script adds the Label object to the Panel object. The add() method of the container class has more than one version, so the specific version must be identified by placing the method name and its argument types in a list (surrounded by braces).

The tenth line adds the Button object to the Panel object.

The eleventh line adds the Panel object to the center of the Frame object. Note that the add() method with two arguments (String and Component) is used.

The next two lines create a TextField object and initialize its text to An AWT TextField.

In lines 14-17, the text field is added to the frame, the frame's size is set to 400 by 400, the frame is shown, and it is set to the front window.

The next line shows how Java events are handled by Tcl. The java::bind command is used to bind a Java event with a Tcl command. The first argument to java::bind is the Java object whose events are being handled. The second argument is the event name, and the third argument is the Tcl command that is executed to handle the event. This command sets the done variable to yes.

The following line sets the value of newText to the list containing the string "THE TEXT HAS BEEN UPDATED!!!". The text is put in a list so that it appears as a single argument to a method.

The actionPerformed event of the button is handled by this command:

$text setText $newText

This command updates the text of the TextField object referenced by the text variable.

The vwait command causes the script to wait while events are handled. The vwait command waits until the done variable is set by an event handler and the event handler has completed its processing. This variable is set by the handling of the windowClosing event. The Frame object is disposed after the vwait command finishes its processing.

Jacl Versus Tcl

Because Jacl supports the integration of both Java and Tcl, it is natural to ask which part of an application should be written in Java and which part should be written in Tcl. The developers of Jacl envisioned a development approach in which Java would be used to develop reusable components, such as JavaBeans. As such, Java is referred to as a component developer. Tcl, on the other hand, is referred to as an application assembler. Tcl scripts are the "glue" used to integrate Java components into applications.

This symbiosis between Java and Tcl can be compared to the complimentary relationship between Microsoft's Visual C++ and Visual Basic. Visual C++ is used to develop Component Object Model (COM) components, and Visual Basic is used to assemble COM components into final applications. In the same way, Java is used to create JavaBeans, and Tcl is used to integrate the beans into Jacl applications.

Jacl can also be used in another context that has a Microsoft analogy. The individual programs of Microsoft Office, such as Word and Excel, support Visual Basic as an application scripting language. Visual Basic can be used to extend the capabilities of the Office application programs. It is envisioned that Tcl will be used in the same manner to extend Jacl-enabled Java applications. Imagine a Java-based office suite that uses Tcl to create extensions to the individual office programs.

Tcl Blend

Tcl Blend is another product of the Java-Tcl research team. It's an extension to the C-based Tcl environment that allows the C-based Tcl interpreter to interact with the JVM, and vice versa. Tcl Blend is the middle ground between the standard Tcl environment and Jacl. It supports Tcl, C, and Java, and provides a bridge between the C and Java-based Tcl environments so that legacy code, written in C, can be incrementally ported to Java.

Tcl Blend uses the same Java Package software component as Jacl. This means that Tcl scripts have the same visibility into the Java API as they do in Jacl. For this reason, Tcl Blend is a great way for Tcl programmers to move over to Java without sacrificing their investment in C code. Tcl programmers can write Tcl scripts that use both the Java API and C-based Tcl extensions. As the C-based Tcl extensions are replaced by the Java API, the Tcl applications can be ported to the 100-percent Java environment of Jacl.

Tcl Blend is available from the Sun Web site at http://sunscript.sun.com/java/.


NOTE: Tcl Blend requires Tcl version 8.0.

Summary

In this chapter, you were introduced to Jacl and learned how it provides programmers with the best features of both Java and Tcl. You used Tcl to script an AWT-based Java application. You were then introduced to Tcl Blend and learned how it can be used to convert legacy C applications to Java. In the next chapter, you'll learn about the visual design tools that are available to support Java software development.


Contents

© Copyright 1998, Macmillan Publishing. All rights reserved.