RAP Client Scripting Phase II (1/3)

Part 1: RAP 2.1 and Client Scripting

The RAP ClientScripting add-on was introduced with RAP 1.5/Juno to enable more responsive input validation on text widgets. The basic idea is that while most components of a RAP application can run exclusively on the server, some surface-level behavior is best implemented to run on the client. ClientScripting allows this by adding support for SWT-style event listeners written in JavaScript which can be executed within the user’s browser without any latency.


clientscripting1 RAP Client Scripting Phase II (1/3)

The ClientScripting project also received a bigger update in time for the upcoming Kepler release. Though optimized for RAP 2.1, it’s not an official part of the release, hence “Phase II”. Support for the RAP 1.5 and 2.0 releases continues in their respective streamsbut will not include any of the new features.

Changed API

Previously, ClientListeners were registered with their slightly unfamiliar addTo method. Now they implement the SWT Listener interface and can be added to a widget like any other untyped listener:

text.addListener( SWT.Verify, new ClientListener( script ) );

Hint: Creating multiple equal ClientListener instances is currently not optimized, so “recycle” if you can. For example…

textOne.addListener( SWT.Verify, new ClientListener( script ) );
textTwo.addListener( SWT.Verify, new ClientListener( script ) );

…transfers the script code to the client twice, while…

ClientListener listener = new ClientListener( script );
textOne.addListener( SWT.Verify, listener );
textTwo.addListener( SWT.Verify, listener );

…does it only once.

More Supported Event Types

ClientListener can now also be used to listen to Show and Hide events on any Control, while support for the Selection and Paint events has been added to the List and Canvas widgets respectively. Paint events can be triggered by calling canvas.redraw() on the client or on the server, and the listener can then use a subset of the HTML5-Canvas API to draw. (Here is a code example.)

New Getter

While the widget-objects in ClientScripting have most of the setters of their Java-counterparts, only a few getters are available. New for Kepler are Text#getSelection() and List#getSelection(). This makes the list widget pretty useable with ClientScripting.

Tomorrow…

… I will tell you how to pass data to a ClientListener.

3 Responses to “RAP Client Scripting Phase II (1/3)”

  1. Thomas Kratz says:

    Hey Tim,

    I think I could use that for adding google analytics to my Buttons, yes?
    As my App is based on the workbench, would you hav a generic idea to add something like this on Tool/MenuItems created by the Workbench?

  2. You can execute any arbitrary piece of JavaScript in an ClientListener, so Google Analytics should work too. I can’t really help with the second question since I’m not at all an expert on the workbench. If you can’t access the widget itself, but execute Java code when it is selected, you could could perhaps use the JavaScriptExecuter service instead of ClientListeners to access the analytics API.

  3. Thomas Kratz says:

    Thx Tim for clearing this up. I’ll give this a try together with the internal widget id api as soon as I find some time …

3 responses so far

Written by . Published in Categories: EclipseSource News, Editors choice, Planet Eclipse

Author:
Published:
Jun 5th, 2013