|
101, Part III: Introduction | Page 1 | Page 2 | Page 3 | Page 4 Converting Existing Code to Beans Got some old code you'd like to update? To see how it's done, look at the NervousText example program distributed with the JDK software. All AWT components in JDK version 1.1 software, are JavaBeansTM technology. This means they are already set up to be used as reusable components in builder tools. However, older JDK software, version 1.0, programs and some JDK software, version 1.1 demo programs are not built as Beans. This lesson describes how to convert existing programs and applets into JavaBeans. Converting existing programs has advantages when learning about JavaBeans tecnology. First, you can concentrate on the parts of the program or applet that are unique to Beans. Second, working with a familiar program helps you to focus on the JavaBean features that you need to learn first. The concepts introduced in this lesson include:
Start by taking a look at the JDK 1.1 code in the The next section contains the steps to convert the NervousText applet into a JavaBean. Making an Applet into a Bean The following steps outline the process of converting a Java program or applet into a JavaBean. Step 1. Select program to convert First, select a demo applet or class to convert to a Bean. This particular example uses NervousText. Step 2. Change applets to standalone Window objects While it is possible to make applets into Beans, it's a little easier to convert standalone Window objects or components to Beans. To see an example of an applet that is a Bean, look at the source code for the JugglerBean distributed with the Beans Development Toolkit (BDK). To change NervousText into a standalone Window object, edit the line declaring the NervousText class. In the JDK 1.1 distribution, it reads:
public class NervousText extends java.applet.Applet
implements Runnable {
Instead of making NervousText a subclass of Applet, make it a subclass of Panel, as follows:
public class NervousText extends Panel implements Runnable {
Step 3. Add AWT and Beans import and package statements
Add the following import statement after import java.awt.*; The most effective way to manage the different Beans you create is to define them to be part of a package, and then build a JAR file based on the directory structure implicitly defined by the package name.
To test the various versions of the NervousText Bean in the BeanBox, you need to add a package statement indicating the location of the Java class files that make up the Bean. The package reflects the subsystem where you plan to store your class files locally, and not necessarily where the BeanBox expects to find the actual class files. Once you have bundled the set of classes making up a Bean in a JAR file, you can place the JAR file in the default location where the BeanBox, on its initialization, looks for JAR files. For example, the demo Beans for the NervousText example in this lesson are part of the You can define your own package and corresponding subdirectories. However, it is convenient to use the supplied demo package.
Add the following package sun.beanbox.beans; The beginning of the NervousText file now looks like this:
Step 4. Add a constructor
When converting applets to Window objects, you no longer need the Change:
public void init() {
to:
public NervousText() {
The constructor now looks like this. Step 5. Adapt applet parameters
Since NervousText is no longer an applet, it can't call if (s == null) {
s = "HotJava";
}
You can assure that the value of
Step 6. Start the component's thread
A browser or appletviewer automatically starts an applet such as NervousText by calling its
Add the following line at the end of the constructor NervousText, just after the start(); The constructor should now look like this: Step 7. Add properties and support for introspection
A nice feature is to allow users of the NervousText Bean to customize the text it displays when they are building new applications. You can enable builder tools to give users the option to change the text displayed by NervousText by adding a text property. Adding the methods Add these methods after the NervousText constructor:
Step 8. Define the preferred and minimum sizes for the Bean
Strictly speaking, you do not need to define
What is the difference between The BeanBox calls the Bean's Add the following public Dimension preferredSize() {
return (new Dimension(150,150));
}
Step 9. Remove extraneous event handlers
Some event-handler routines used for applets no longer apply once the applet is turned into a Bean.
Within NervousText, comment out the
Now the mouse down handler is defined, but it no longer performs any action. Specifically, a mouse down event no longer kills the thread and stops the nervous text from jittering. Step 10. Test the Bean in the BeanBox You can now add the NervousText component from the Toolbox into the BeanBox panel. You can also change the text parameter in the Property Sheet that is automatically associated with the Bean by the builder tool. When using the BeanBox for testing, it is easiest to package the Bean as a JAR (Java Archive File) and then load the JAR into the BeanBox. To do this, you use a manifest file to define the contents of the JAR so the BeanBox or builder tool knows how to use each of the files contained in the JAR. Your manifest file for NervousText looks like this: Manifest-Version: 1.0 Name: sun/beanbox/beans/NervousText01.class Java-bean: True
This manifest indicates the JAR contains a single Java class file,
Assuming you have installed the Beans Development Kit (BDK) in a directory defined as
Compile the source with the javac -d . NervousText01.java
The dot following the Once you've compiled the class file and created the manifest file, build the JAR as follows: jar cfm NervousText01.jar manifest.tmp sun/beanbox/beans/NervousText01.class You can now remove the temporary manifest file and copy the JAR file to the JAR directory for the BeanBox. rm manifest.tmp cp -p NervousText01.jar BDK_HOME/beans/jars
After copying the JAR file to the BeanBox Program Source Code The makefile for this lesson automates source code compilation, JAR file construction, and copying of JAR files to the appropriate BeanBox directory. You'll have to edit several of the variables in the makefile to indicate the location of your JDK 1.1 and BDK installation directories.
You may want to look at the final source file for NervousText Bean, Version 01, to verify the changes you have made to the
original JDK 1.1 Event Handlers for Beans In this lesson, you add JDK 1.1 event handler methods to NervousText. These methods let you program NervousText Beans to change their run time (as opposed to design time) behavior. Event handlers allow you to define methods which can be fired in a running application. This differs from properties, which allow you to change the way a Bean looks or behaves while building an application.
We'll use event handlers to change the direction in which NervousText writes text. Start by adding
an event receiver so that you can change the text direction from the Step 1. Add import Statement for AWT events
First add the JDK 1.1 event handler import java.awt.event.*;
You now have the following package and import statments at the top of package sun.beanbox.beans; import java.awt.event.*; import java.awt.Graphics; import java.awt.Font; import java.awt.*; Step 2. Add a Variable to Control Direction of Text
Add a boolean toggle variable called
boolean lefttoright=true; The data field declartions for NervousText02 now looks like this: Step 3. Create an ActionEvent Handler Method
Create an action event receiver target called
Step 4. Modify NervousText's paint Method
Finally, change the
Change the call to
g.drawChars(separated, i,1,
x_coord,y_coord);
The Step 5. Build the JAR and Install in the BeanBox javac -d . NervousText02.java Create the manifest in a text editor: Name: sun/beanbox/beans/NervousText02.class Java-Bean: True jar cfm NervousText02.jar manifest.tmp sun/beanbox/beans/NervousText02.class
Install the JAR in the BeanBox JAR file directory (substituting your BDK installation directory for
cp -p NervousText02.jar BDK_HOME/beans/jars Step 6. Test NervousText02 in the BeanBox This test adds two Beans to the BeanBox: OurButton and NervousText02.
Now you want a mouse press on the OurButton Bean to fire an actionPerformed button event. Use the
BeanBox Edit menu to direct this action event to be handled by the
You then see a popup dialog named EventTargetDialog. Pressing OurButton now causes the text displayed by the NervousText02 button to be reversed from right to left. Program Source Code The makefile for this lesson automates source code compilation, JAR file construction, and copying of JAR files to the appropriate BeanBox directory. You have to edit several of the variables in the makefile to indicate the location of your JDK 1.1 and BDK installation directories.
You may want to look at the final source file for NervousText Bean, Version 02, to verify the
changes you have made to the original Making Beans Serializable Adding serialization to a bean allows it to save itselfto persisteven after it has been customized in a builder tool.
This lesson adds methods to Step 1. Add an import statement for serialization
Add the following lines after the existing
import java.io.ObjectOutputStream; import java.io.ObjectInputStream; It's also a good idea to add support for those exceptions that can occur while reading or writing a serialized bean to a file.
To do this, add the following
import java.io.IOException; Now you can handle any trouble your Bean may have when reading the saved object's state from disk. The beginning of the file looks like this: Step 2. Make the thread fields transient You should mark as transient any thread fields declared inside a class whose instances are to be serialized. You must do this because when a serialized bean is reinstantiated, the original thread context no longer exists. There are other types of objects which can not be serialized, and they, too, must be marked as transient. Refer to the documentation on object serialization for details.
Thus, the
Thread killme = null; transient Thread killme = null; You can't save the context of a thread and reload it in the same state because the thread's state depends on the runtime environment. The instance fields declared in NervousText03 now look like this. Step 3. Add serialization methods for persistence
Now you're ready to add the
Add the following definitions for Step 4. Build the JAR and install it in the BeanBox javac -d . NervousText03.java Create the manifest, which looks like this: Name: sun/beanbox/beans/NervousText03.class Java-Bean: True jar cfm NervousText03.jar manifest.tmp sun/beanbox/beans/NervousText03.class
Remove the temporary manifest and install the JAR in the BeanBox JAR file directory (substituting
your BDK installation directory for rm manifest.tmp cp -p NervousText03.jar BDK_HOME/beans/jars Step 5. Test NervousText03 in the BeanBox
Pressing OurButton now causes the text displayed by the NervousText03 button to be reversed from right to left.
A file dialog box appears to let you specify the file name for the application. The BeanBox application has now been serialized and saved to disk. All components disappear. You're now ready to reload the application. The application appears exactly as it did when you saved it. What's happening behind the covers
Notice when the saved file is restored, the text is set to the same string you specified in the
property sheet editor, plus its direction is reversed. Also notice, that the application comes up in a running state. That's
because NervousText03 simulates the saving of the bean's thread state by appending the value of the boolean variable,
Because the thread was running when you saved the application, the code to reinstantiate the bean
in
If You will have to figure out how to hook up the action events generated by the start and stop buttons to the appropriate handlers to start and stop the thread in the NervousText03 Bean. If you need a hint, go back and review how this was done with the original OurButton to change the text direction. Now stop the application by pressing the stop button, serialize it, clear the BeanBox and reload it. Then press the start button and try the exercise again. Program Source Code The makefile for this lesson automates source code compilation, JAR file construction, and copying of JAR files to the appropriate BeanBox directory. You have to edit several of the variables in the makefile to indicate the location of your JDK 1.1 and BDK installation directories.
You may want to look at the final source file for NervousText Bean, Version 03, to verify the
changes you have made to the original Introduction | Page 1 | Page 2 | Page 3 | Page 4 | ||||||||||||
|
| ||||||||||||