RAP Client Scripting Phase II (3/3)

June 7, 2013 | 2 min Read

Part 3: Cross-Widget Scripting

The most glaring limitation of ClientScripting since it’s inception was that a listener can only manipulate the widget that fired the current event. Consider, for example, an onscreen number keypad like this:

The obvious (non-ClientScripting) implementation would be to attach selection listeners to the buttons that insert the given character into the text field. In RAP this would normally cause a slight delay between pressing the button and the new number appearing. We can now use ClientScripting to make the keypad more responsive, which is especially noticeable if you use a touchscreen device.

As demonstrated the last time, we can add simple objects like strings to widgets and use them in a ClientListener. While it is not (at least not yet) possible to directly pass other widgets to the listener with this method, you can add their RAP protocol id in the following way:

widget.setData( "myOtherWidget", WidgetUtil.getId( otherWidget ) );

In the ClientListener, you can convert the id to a ClientScripting widget object:

var handleEvent = function( event ) {
  var id = event.widget.getData( "myOtherWidget" );
  var otherWidget = rap.getObject( id );
  //...
}

Therefore the keypad can be implemented like this:

Java:

ClientListener listener = new ClientListener( scriptCode );
int[] numbers = new int[]{ 7, 8, 9, 4, 5, 6, 1, 2, 3, 0 };
for( int i = 0; i < numbers.length; i++ ) {
  Button button = new Button( parent, SWT.PUSH );
  button.setText( String.valueOf( numbers[ i ] ) );
  button.setData( "textWidget", WidgetUtil.getId( text ) );
  button.setData( "numValue", Integer.valueOf( numbers[ i ] ) );
  button.addListener( SWT.MouseDown, listener );
}

JavaScript:

var handleEvent = function( event ) {
  var button = event.widget;
  var text = rap.getObject( button.getData( "textWidget" ) );
  var value = button.getData( "numValue" );
  var str = text.getText();
  text.setText( str + value );
};

This and other examples are part of the bundle org.eclipse.rap.clientscripting.demo that is included in the git repository. (It is not part of the p2 repository.)

The Future

ClientScripting has a few more issues to overcome before it will become an official part of RAP (possibly in 2.2/Luna): We will need to nail down and better document the API, especially for the widget objects, consider single-sourcing strategies, and enable client-to-server communication/synchronization. Ideally, ClientScripting will become a cool little feature of RAP that you only need occasionally, but can give your application that final touch to make it feel just right.