Greg has posted 18 posts at DZone. You can read more from them at their website. View Full User Profile

Building Rich Internet Applications Using Pivot and JavaScript

11.17.2008
| 6580 views |
  • submit to reddit

Pivot is a platform for building rich internet applications that run in the Java Plugin. While Pivot applications are often built using a combination of XML and Java, it is sometimes more convenient to build simple applications or applications whose code is likely to change often, such as prototypes, using a scripting language such as JavaScript. Pivot now allows developers to write their application logic using their scripting language of choice, using the features provided by the javax.script package available in Java 6 and above.

Pivot script code is stored in external files included by a WTKX source file, as shown below (WTKX is an XML markup language used by Pivot to simplify user interface construction):

<Border xmlns:wtkx="http://pivot-toolkit.org/wtkx/2008" xmlns="pivot.wtk">
    <content>
        <FlowPane orientation="vertical" styles="{padding:6}">
            <wtkx:script src="demo.js"/>

            <PushButton wtkx:id="pushButton" buttonData="Click Me!"
                buttonPressListener="$buttonPressListener"/>

            <Border styles="{padding:0, color:10}">
                <content>
                    <ListView listData="$listData" selectedIndex="0"/>
                </content>
            </Border>
        </FlowPane>
    </content>
</Border>

The <wtkx:script> tag is used to include the JavaScript code; all instance IDs declared in the WTKX file are available as variables to the script, and all variables declared in the script can be accessed using the WTKX dereference operator ("$"). The script in this example attaches a press listener to a push button and populates a list view component:

importPackage(Packages.pivot.collections);
importPackage(Packages.pivot.wtk);

var buttonPressListener = new ButtonPressListener() {
    buttonPressed: function(button) {
        Alert.alert("You clicked me!", button.getWindow());
    }
};

var listData = new ArrayList();
listData.add("One");
listData.add("Two");
listData.add("Three");

A screen shot of the result is as follows; a live example can be seen in the Demos section of the Pivot web site:

Pivot scripting support is not limited to JavaScript, however. Here is a similar example written using Groovy (I'm not a Groovy expert - thanks to Adam Stachelek at Cantina Consulting for contributing this example):

import pivot.collections.adapter.*
import pivot.wtk.*

public class MyButtonPressListener implements ButtonPressListener {
    public void buttonPressed(Button button) {
        Alert.alert("You clicked me!", button.getWindow())
    }
}

buttonPressListener = new MyButtonPressListener()

listData = []
listData << "One"
listData << "Two"
listData << "Three"

listData = new ListAdapter(listData)

The resulting application behavior is identical to the JavaScript version.

Pivot is a powerful, modern platform for creating rich internet applications, and now, thanks to the scripting support provided by Java 6, you don't have to be a Java developer to take advantage of it.

Published at DZone with permission of its author, Greg Brown.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)