Are you ready for WABA?
By Piroz Mohseni

The so-called experts predict that the next big wave will involve small devices running network-aware applications. Coming from a Java background, I was looking for an environment that allowed me to develop device applications using Java. Furthermore, since I already had a PalmPilot, I wanted to develop applications for the Palm OS. I found a perfect match called Waba from wabasoft.com. Although still a beta release, Waba provides a nice entry point to satisfy the curiosity of those who are interested in learning about developing applications for small devices. In this article, I'll summarize my experience.

#### What is Waba?

Waba has several components. First there is the Waba Virtual Machine which you must install on your PalmPilot. The VM is small (around 36K). The distribution is a zip file. Two files in the distribution make up the VM. They are waba.pdb and waba.prc. You should install these two files in your PalmPilot as you do any other installations. I used the Palm Desktop program to upload these files into my PalmPilot.

The other component is the Waba SDK, which is what you would use to develop Waba applications. The SDK contains a number of Java classes, which make up the API for Waba. You write your applications in Java and compile them using your favorite Java compiler/IDE. The only catch is that you are limited to using only the classes that are part of the SDK. While your full-scale Java compiler will compile any valid Java program, the Waba VM does not support the entire Java environment. So if you use a class that is not part of Waba, you won't see an error until you run it inside your Waba VM (on the PalmPilot). This took me a while to get used to, since I constantly had to check to see what I was about to write was indeed supported by Waba VM.

#### Writing a Waba application

In this section, we'll go through the typical steps for writing a Waba application. There is a good tutorial on the Wabasoft Web site, and there are some example applications in the Waba SDK as well. For our purposes, I decided to write a simple data-entry application. Once the data is entered, the application will provide an XML representation of the data. That XML output can then be sent to another device for further processing (but that's beyond the scope of this article).

Our task is then to display a few fields where the user can enter some data. The program will then translate that data into XML and display it back. I developed this application on Windows 98, with JDK 1.2 from Sun Microsystems and Beta 8 of Waba SDK. I tested the program on Palm V and Palm IIIx.

Writing a Waba application is similar to writing an applet. Waba classes inherit from the MainWindow class. You have a graphics context where you can draw text and geometric objects. You also have access to an array of user interface controls such as labels, buttons, check boxes, radio buttons, etc. If you have been doing GUI development with Swing, you will be disappointed with what Waba has to offer, but always remember the environment where Waba programs were intended to run. Small devices most likely will not support a sophisticated GUI any time soon (see your VCR for an example).

The entire listing for our program is shown in Listing 1. The program uses a number of controls like buttons, labels, and check boxes. Displaying a control is merely a matter of instantiating it and then placing it on the screen. For example, to place a label you have to do the following:

     label = new Label("Enter Information:");

     label.setRect(30, 0, 60, 30);

     add(label);

The

    setRect()

method specifies the x,y location on where the label should go (x=30, y=0 in our example) and the width and height of the label control (width=60, height =30 in our example).

The constructor for our class calls the

    FirstPage()

method which basically paints the data-entry screen for our application. Creating user interfaces by hand can become very tedious. There is an application called "UIGen" which helps in creating complex Waba user interfaces. The application is available from the WabaSoft site.

The next part of the program had to capture an event and then display the XML data. The event I decided to capture was the pressing of a button. Waba is based on a simple event model. The

    onEvent()

method can be overridden to provide custom event-handling. This method received an Event object as its input. We check to make sure the event is a "

    PRESSED

" event and its source was the button on our data-entry screen using the following code:

    if (event.type == ControlEvent.PRESSED && event.target == button) {

                    SecondPage();

    }

Again, the event model is not like Java's event model with event sources and listeners, but it is good enough for simple applications. Other methods that can be overridden include

    onStart()

,

    onExit()

, and

    onPaint()

.

When the mouse press occurs, we remove all the controls from the page and then display the XML output using a series of "Edit" controls (one on each line). That code is in the

    SecondPage()

method, which is called by the event handler.

Listing 1. This program displays a few "edit" boxes and then converts the input data into XML.

| Listing 1 (Code Sample) |
    // displays a few "edit" boxes and then converts the input data into XML
    import waba.ui.*;
    import waba.fx.*;
    public class ShowXML extends MainWindow
    {
    Label label, l1, l2, l3;
    Button button, button2;
    Check employee;
    Edit fname, lname, email;
    public ShowXML() {
       FirstPage();
    }
    public void FirstPage()
                   {
                   label = new Label("Enter Information:");
                   label.setRect(30, 0, 60, 30);
                   add(label);
          l1 = new Label("First Name:");
                   l1.setRect(10, 20, 50, 20);
                   add(l1);
          fname = new Edit();
                   fname.setText("");
                   fname.setRect(70, 20, 60, 20);
                   add(fname);
          l2 = new Label("Last Name:");
                   l2.setRect(10, 40, 50, 20);
                   add(l2);
          lname = new Edit();
                   lname.setText("");
                   lname.setRect(70, 40, 60, 20);
                   add(lname);
          l3 = new Label("Email:");
                   l3.setRect(10, 60, 50, 20);
                   add(l3);
          email = new Edit();
                   email.setText("");
                   email.setRect(70, 60, 60, 20);
                   add(email);
                   employee = new Check("Employee");
                   employee.setRect(10, 80, 60, 20);
                   add(employee);
          button = new Button("Generate XML");
                   button.setRect(50, 100, 60, 20);
                   add(button);
    }
    public void SecondPage() {
          Edit ln1, ln2, ln3, ln4, ln5, ln6, ln7, ln8;
          String mfname = fname.getText();
          String mlname = lname.getText();
          String memail = email.getText();
          remove(button);
          remove(email);
          remove(fname);
          remove(lname);
          remove(label);
          remove(l1);
          remove(l2);
          remove(l3);
          remove(employee);
          ln1 = new Edit();
                   ln1.setText( "<?xml version=\"1.0\"?>" );
                   ln1.setRect(0, 0, 160, 10);
                   add(ln1);
          ln2 = new Edit();
                   ln2.setText("<info>");
                   ln2.setRect(0, 10, 160, 10);
                   add(ln2);
          ln3 = new Edit();
          if (employee.getChecked())
                   ln3.setText("<name employee=\"yes\">");
          else
                   ln3.setText("<name employee=\"no\">");
                   ln3.setRect(10, 20, 160, 10);
                   add(ln3);
          ln4 = new Edit();
                   ln4.setText("<fname>" + mfname + "</fname>");
                   ln4.setRect(20, 30, 160, 10);
                   add(ln4);
          ln5 = new Edit();
                   ln5.setText("<lname>" + mlname + "</lname>");
                   ln5.setRect(20, 40, 160, 10);
                   add(ln5);
          ln6 = new Edit();
                   ln6.setText("</name>");
                   ln6.setRect(10, 50, 160, 10);
                   add(ln6);
          ln7 = new Edit();
                   ln7.setText("<email>" + memail + "</email>");
                   ln7.setRect(10, 60, 160, 10);
                   add(ln7);
          ln8 = new Edit();
                   ln8.setText("</info>");
                   ln8.setRect(0, 70, 160, 10);
                   add(ln8);
    }
    public void onPaint(Graphics g) {
                   g.setColor(0, 0, 0);
    }
    public void onEvent(Event event) {
                      if (event.type == ControlEvent.PRESSED && event.target == button) {
                    SecondPage();
             }
    }
    }

With the application in hand, we had to compile it and upload it to the Palm device. The compilation is just like any Java program. Make sure the Waba classes are included in your CLASSPATH. The development process would be too cumbersome if you had to upload the program to the PalmPilot every time while you debug and fine-tune it. To get around that, you can actually see your Waba program on your PC using a Java applet environment that simulates the PalmPilot screen. If your application code is in a file called

    foo.class

, you can run it using the following command:

    java waba.applet.Applet foo

The files that get uploaded to the PalmPilot are not the Java class files. You have to go through an extra step. In the bin directory of the Waba SDK, there are two programs called

    exegen.exe

and

    warp.exe

. These are binary programs and only run under Windows, but you can download versions for other platforms as well.

The

    exegen

program generates a PalmPilot OS executable that actually launches your program under the Waba VM. With

    exegen

, you can specify the application name, its memory allocation requirement, and an icon (in the form of a .bmp file). Palm OS executables have a ".prc" extension. If you don't specify any parameters, the default is taken. For our example, we used the following command line:

    exegen MyApp ShowXML MyApp

The first parameter is the name of the output file without the .prc extension. The second parameter is the name of the main class of the application and the last parameter is the "warp" file that contains the Java classes (without the .extension). The warp program is a packaging application that merely puts the various files needed for your application (mainly class files) into one single file with the .pdb extension. We used the following command line for our program:

    warp c MyApp ShowXML.class

The first parameter (i.e.,

    MyApp

) must match the last parameter of the

    exegen

program.

You should now have a .pdb and .prc file. The .pdb file is really the container of your application code. The .prc file merely launches your program and does not change when your program changes. You can now install these two files on your PalmPilot, find the icon representing the ShowXML application (called MyApp), and run it. Congratulations! You have just written an application for a device without doing chip design!

#### Conclusions

Waba is a nice environment for developing simple to moderate applications using the Java programming language. Small devices have their own unique requirements, so simply knowing Java is not going to be enough for developing effective applications for devices. One must take into account the network capabilities, memory limitations, and performance issues often associated with such applications. Your PalmPilot is not running a 500-MHz Pentium processor. There are two good articles on the WabaSoft Web site regarding performance and memory issues of Waba programs.

Another factor to consider is that Sun wants Java to be a big player in the device market and will have offerings in that market soon. It would be interesting to see how an environment like Waba or REBOL would compete under that scenario. For now, if you are curious about device applications or simply want to create a program to impress your PalmPilot-loving friends, Waba is definitely a nice place to start.

#### Related articles

Java programming for the Palm OS using Jump

#### About the author

Piroz Mohseni is president of Bita Technologies focusing on business improvement through effective usage of technology. His areas of interest include enterprise Java, XML, and e-commerce applications. Contact him at mohseni@bita-tech.com.