Monday, July 6, 2009

Eclipse RAP : Custom Widget development

It took a while to understand and create a custom widget but finally i could do it. I wanted to create a custom widget to draw sequence of svg graphics. I followed the Custom widget development at the RAP help site. It helped me to get a good start but since i was trying to do SVG it didnt really work at my first attempt..(i know... nothing works at the first attempt).

The SVG support i wanted to create was something like below.

Here is what i did to create a custom widget.

1. Create a new plugin project and name it like com.xyz.project.mywidget This is not mandatory but its good to call the plugin as the widget name.

2. Create a package com.xyz.project.mywidget This package will contain the server side, client side and API resource classes. Just follow as explained in the RAP help.

3. Create the server side Widget class , i wanted a widget to send events to the server and also behave as a selection provider when a image is clicked. So i extended SWT Composite and implemented ISelectionProvider.

Now that the server side, widget is ready, we need the client side widget code, the qooxdoo javascript code. Read the qooxdoo explanation in RAP custom widget tutorial and follow it, it gives you a basic idea of what to do.

Now that, whenever the setImages method is called on the server side widget, the client has to update the browser. But how does it interact, the server side widget and the client side widget get connected through two main classes, the API resource class and the LCA class. There are different ways of defining the LCA class but here i would just follow the simple approach as explained in the RAP help.

First to create the API resource, we need to create a IResource implementation , In our case, MyWidgetResource as follows,

you can just copy and paste the above code and replace the getLocation method to your .js file. Now the LCA, this can either be written in a pre defined package or can be done using getAdapter method in the widget class. I will follow the pre defined package which com.xyz.project.mywidget.internal.mywidgetkit and create MyWidgetLCA.java

Here, the LCA class is the important class which communicates between the server code and client script. The selection on the client sends an event to the server through the LCA in the readData method. The client code will use org.eclipse.swt.Request.getInstance() to send the request to the server when an image is clicked on the Widget.

Now we got the both the resource and LCA ready. we need to create an resources extension point and add the MyWidgetResource classes.

ok..we have a basic setup ready, what is the Raphael stuff on the Qooxdoo javascript. Raphael is a Javascript based SVG library which helps creating different SVG based graphics. For more details on that visit http://raphaeljs.com/. The demos explain a lot about the library. How do we add an external javascript resource into the myWidget implementation.

1. Create a RaphaelAPIResource similar to the one we created for MyWidget as MyWidgetResource.

Since we are displaying images on the client side , we need the images also as resources sent to the client. we have to create http.registryresources to use aliases for the images. In this case, we are referring to Part_icon_image1.png and Part_icon_image2.png as alias in the javascript.