Palm Programming with Waba
by Matthew E. Ferris04/19/2001
Last month we looked at the various options that are available to developers who want to write Palm OS applications using Java. This month we begin looking deeper into the solutions we profiled, and we begin with Waba, an open source, Java-like language. If you know Java, you know the syntax of Waba.
User Interface Controls
Let's begin with the user interface classes, since very few programs on a PDA are usable without a user interface. I'll look at data persistence next month.
Most of the classes for GUI building are contained in the waba.ui
package. The inheritance hierarchy of these classes is much like
Java's AWT. All of them of course extend java.lang.Object
and waba.ui.Control. I would encourage you to consult the
JavaDoc for the waba.ui package for particulars about the
static data members and the methods for performing common tasks such
like placing a control, making it visible, etc. The following classes
extend the waba.ui.Control:
Button-- a CommandButtonCheck-- a Checkbox controlContainer-- ancestor for other controls (similar to an AWT Panel, or a Swing Jpanel).Edit-- a text entry and edit control (similar to a TextField).Label-- a label control.MainWindow-- the main entry point to a user interface based application. All GUI apps must have one.Radio-- a radio buttonTabBar/Tab/TabPanel-- a TabBar is a container for tabs.
As the ancestor of these classes, Control contains
methods for event handling and painting of controls. Let's look at an
example of a simple Waba application that would prompt the user for
some information; in this case, a name to lookup in a contact
manager.

The code for this class is
import waba.ui.*;
public class ContactManager extends MainWindow
{
Edit edit;
Label label;
Button button;
public ContactManager()
{
setTitle("Contact Manager");
setBorderStyle(Window.RECT_BORDER);
label = new Label("Enter name to search for:");
label.setRect(5, 65, 110, 15);
add(label);
Edit edit = new Edit();
edit.setRect(55, 80, 60, 15);
add(edit);
button = new Button("Find");
button.setRect(65, 110, 30, 15);
add(button);
}
public void onEvent(Event event)
{
if (event.type == ControlEvent.PRESSED)
{
if (event.target == button)
{
// lookup contact in Catalog...
}
}
}
}
First we import the waba.ui package, since all of the
user interface classes are located there. This class extends
MainWindow. Every Waba application that has a user interface must
extend MainWindow. Next we declare the variables we will need: an
Edit, a Label, and a Button. In the constructor we call setTitle and
setBorder, then initiate each of the GUI components. Notice that we
also use absolute placement of the controls. This is a difference
between Waba and Java. There is no concept of a layout manager. We
pass in the x and y coordinates, plus the width and height of the
control. The control is then dropped at that screen location.
Another nice feature of Waba is a mimic of the Palm keyboard. When you place the cursor in the Edit and tap the section of the Palm silkscreen where the ABC/123 are placed, the keyboard displays:

Finally, the event handling occurs in the onEvent method, in a two-fold fashion. First, the event object itself is queried to determine the type of event. In this case we want to know if it's a PRESSED event. (Other types include FOCUS_IN, FOCUS_OUT, TIMER, and WINDOW_CLOSED). These are all defined in the ControlEvent class, which extends event. If the event matches the type we are looking for, then we want to determine the object that fired the event, referred to as the target. In our ContactManager application, we only want to respond to button presses. So if the user tapping the button fired the event, then we would execute the code to look up the name in the Catalog.
Other methods that are defined in the MainWindow class which could be useful are onStart and onExit. The onStart method might contain initialization code. It is called after the constructor finishes. The onExit method is called just prior to the application exiting, so any necessary cleanup such as writing unsaved data may be performed there.


