[Previous] [Next] [TOC] [Index]

Chapter 3
Using Java Objects as Components

Introduction to Java Objects

This document describes how to use Java objects as component in Visual JavaScript. Certain Java objects can be used in Visual JavaScript applications. These include:

The advantage to using an existing Java applet is that many such applets can be incorporated into Visual JavaScript applications without modification.

The advantage to using an applet implemented as a Java Bean is that it includes a BeanInfo that describes the applet's properties, methods, and events. Visual JavaScript makes use of this information to display information about the object to developers at design time. Using this information enables developers to integrate the Java object into applications more easily.

Finally, for components without user interfaces, the JavaScript new operator provides a way to use components implemented in Java, including Java Beans, that are described using JavaScript Bean (JSB) files. Creating a JavaScript component using JSB files is described in "Creating JavaScript Components". The JSB file structure and the syntax of its tags is described in "JSB File Structure Reference".

Using Existing Applets

Applets written with JDK 1.02 can be used without modification in Visual Javascript. An applet can be embedded in a Visual JavaScript page either by:

Importing Pages

Choose File |Import to import an applet on an existing web site page into Visual JavaScript. The page can be local or remote. Visual JavaScript reads the HTML page and adds any directly referenced files to the current Visual JavaScript project file. Files added to the project include the .CLASS file specified in the CODE attribute of the <APPLET> tags that appear on the page.

NOTE: If an applet depends on other .CLASS or support files that are not directly referenced on the HTML page, these files are not automatically imported. You must repeat the import command for each of these extra files, and you must set the <APPLET> tag's FILEREFS attribute to list these files. FILEREFS is a quoted string consisting of a comma-delimited list of files. The list of files is used by Visual JavaScript to ensure that as the applet component is used in applications and deployed, its support files are also used and deployed.

Creating an Applet Tag by Hand

Instead of importing a page containing an applet into a Visual JavaScript project, you can create an applet tag by hand. In this case, you must import the applet's .CLASS file into the project. If the applet also depends on other .CLASS or support files, you must repeat the import command for each of these extra files, and you must set the <APPLET> tag's FILEREFS attribute to list these files. FILEREFS is a quoted string consisting of a comma-delimited list of files. The list of files is used by Visual JavaScript to ensure that as the applet component is used in applications and deployed, its support files are also used and deployed.

Creating and Using Java Beans

Java Beans are Java classes written in accordance with the Sun Microsystems Java Beans specification. This document explains how to incorporate a Java Bean component into a Visual JavaScript project, and describes some Visual JavaScript PR2 restrictions about using JavaBeans, but it does not describe the actual implementation of a Java Bean component. The Java Beans specification can be found at http://splash.javasoft.com/beans.

Adding Beans to Visual JavaScript

To use a Java Beans component to build Visual JavaScript applications, follow these steps:

  1. Package the Java Bean component and its supporting files into a JAR file. To create a JAR file, see "Creating a JAR File". Visual JavaScript supports both compressed and uncompressed JAR files.

  2. Import the Java Bean component to a Visual JavaScript palette. Choose Tools | Install to Palette | New, and select the JAR file containing the Java Bean component. Importing the Java Bean to the palette creates an <APPLET> tag with a FILEREFS attribute referring to all the files in the JAR. This ensures that supporting files get deployed with the component.

Creating a JAR File

To create a JAR file, you need the Java Developer's Kit (JDK) version 1.1, which you can download from the JavaSoft web site. Ordinarily, at least one of the files you put into a JAR file must be a JavaBean as defined in the JDK 1.0.2 transitional bean specification.

To create the JAR file using the JAR packager in the JDK, follow these steps:

  1. Create a temporary working directory. Copy the beans and any related .CLASS files to the working directory.

  2. Create subdirectories for your CLASS files, if necessary. If the classes are in packages, you must make subdirectories. Make the subdirectories relative to the working directory, and move the beans into them. For example, if a Java Bean "Molecule.class" is in the package "sunw.demo.molecule", you should move Molecule.class to the subdirectory sunw/demo/molecule under your working directory.

  3. In the working directory, create a manifest file. This is an ASCII file listing all the files to be included in the archive. If you have one manifest for a directory, name the file "MANIFEST.MF" (all uppercase). If you have more than one, you can give them different names, and internally, they will be combined into a single manifest file named MANIFEST.MF. The first line in this file should contain this:
       Manifest-Version: 1.0

    If all the .CLASS and/or .JSB files in the jar are beans, you don't need to make a manifest file.

  4. Create one entry in the manifest file for each JavaScript component in the archive. Each entry must have two lines. The first line gives the name of the class and the second indicates if the class is a Java Bean. Case is significant in these names. For example, here is the entry for the Molecule applet bean:
       Name: sunw/demo/molecule/Molecule.class
       Java-Bean: True

  5. Make sure the JDK1.1 bin directory is in your PATH so you can execute the "jar" command.

  6. To create a JAR file, use the command:
       jar cfm jar-name manifest-name bean-name1 bean-name2...

    For example to create a JAR file called Avogadro.jar to hold the Molecule class, use the following command:

       jar cfm Avogadro.jar MANIFEST.MF sunw/demo/molecule/Molecule.class

    If you did not need to create a manifest file in step 3, then the command to create a JAR file takes the form:

       jar cfm jar-name bean-name..
If this command executes successfully, you should now have a JAR file containing your JavaScript components. You can use the JAR file to import the components into a Visual JavaScript palette for testing and use.

To examine the contents of a JAR file, you can execute the following command:

   jar tf jar-name

Restrictions

In Visual JavaScript PR2 and Communicator 4.01, there are some restrictions on developing and using Java Beans. The following list describes these restrictions.

Visual JavaScript Standard Bean Interfaces

Visual JavaScript provides a set of simple Java Bean components that deliver database functionality. Two standard interfaces are used by these components, and any third party components that interact with them. The Java Bean components provided with Visual JavaScript are the Row interface, and the Table interface. These interfaces use standard Java Bean conventions, and are designed to work in conjunction with the Java Bean PropertyChange mechanisms. These interfaces define a protocol that is bidirectional and that allows Java and JavaScript components which implement them to be easily connected on an HTML page using Visual JavaScript.

The Row interface is used to define and transfer a set of name/value pairs, such as one row of a database table. This interface could be used, for example to connect a cursor object with a form object that displays one row of data at a time. The interface has two parts: one to specify the number of columns, their names and types; and another to manage the changing and transferring of data.

The Table interface is used by two components to exchange an entire collection of data. For example, a cursor and a spreadsheet component could use the Table interface to exchange data.

These interfaces, and various components that implement them (JavaDummyCursor, JavaForm, DummyCursor, TableApplet and ClientCursor) are provided as examples in the CDK's netscape.peas directory.

NOTE: There are not different interfaces for data providers and data consumers.The same interface is used to send and receive data. This reduces the number of interfaces, adapters and support classes in the system.

Data Contexts

Even though the same interface is used for receiving and sending data, the Row and Table interfaces behave differently depending on their roles. re are different roles which components using the Row and Table interfaces assume. In addition to the sender and receiver roles, each of these interfaces acts in a data context, either as a consumer of data, or a provider of data. Data context refers to the number and names of available columns, the number of rows, the current row, and the numbering of those rows. If two components are connected using the Row or Table event, one must provide the data context, and the other consumes it.

For example, assume that a cursor is connected to a spreadsheet component, and that the spreadsheet component is also connected to a form component. The cursor is the data context provider, and the spreadsheet is the data context consumer. By connecting to the cursor, the spreadsheet inherits its data context, which it in turn provides to the form. Note that the spreadsheet is both a consumer and provider, and that a component's role in terms of data context has nothing to do with the direction of the data transfer.

Support Classes

Two Java classes, RowSupport and TableSupport are provided as support for the Row and Table interface components. These classes make it easy to implement the Row and Table interfaces in a Java Bean component. The classes are similar in nature to Java Bean adapters, but have slightly different uses.

The classes are generic support classes that are used by the JavaDummyCursor and JavaForm examples. Like the PropertyChangeSupport class in the BDK, they can be used as members to which Row and Table methods may be delegated. It is also possible to extend these classes, provided of course that the Java Bean does not already extend some other class.

An additional use of these classes is to server as helper objects for JavaScript components. In order to connect to Java Objects, the DummyCursor JavaScript object employs an instance of RowSupport (created by using the JavaScript new operator) to hold its current row's data, and to manage the event interactions with Java objects. Without such an object, such a connection would not be possible.


[Previous] [Next] [TOC] [Index]

Last Updated: 07/08/97 10:46:17


Copyright © 1997 Netscape Communications Corporation