• Share this article:

Scripting Eclipse RCP with JavaScript?!?

Thursday, March 2, 2006 - 00:50 by Wayne Beaton

Ward and I have been talking about mashups a lot lately. We both think that Eclipse RCP is a great vehicle for building mashups on the desktop (we don’t think that mashups should be exclusive to the web). I’ve managed to invoke a few web services and do some interesting things from within Eclipse, but thus far, everything has required a lot of Java programming.

Then I thought of Eclipse Monkey (Dash Project). Eclipse Monkey is the result of some work done by Bjorn and Ward to bring scripting to Eclipse. You can use Eclipse Monkey to do all sorts of things provided you have a rich enough set of DOMs. DOMs basically contribute functionality that manifest as variables you can access in your scripts. The DOMs themselves are pretty easy to create; making them do interesting things is the challenge. But if you build reasonably good DOMs, you can reuse the heck out of them.

Anyway, it occurred to me that Eclipse Monkey can be used to invoke web services and create views and just about anything else you can imagine if you provide the right set of DOMs. Why not use Eclipse Monkey to drive a mashup?

I spent the afternoon coding up a couple of DOMs:

The first DOM contributes a variable named “Flickr”. You can use this variable to search for photos on flickr.com by providing a search phrase. The method returns a bunch of objects containing data about the photos, including a title and enough information to find the actual image itself.

The second DOM contributes a “TableView”. This is an Eclipse view that contains a table. You can tell the table to add and remove columns (columns are moveable and resizeable). When you add a column, you provide a label for the column, what to display, and an initial width. The “what to display” is either the name of a JavaScript function or the name of a property. The function or property is used on each object in the table to compute what will be displayed for that object in the column.

You can create any number of these “TableViews”. When you “get” the TableView, you provide an id. If you ask for a TableView with the same id twice, you get the same instance back. Use a different id, and you get a different TableView.

The third DOM contributes an “ImageViewer”. It listens to the workbench’s selection service and when the selection changes, it asks the selected object for an image to display. Currently, if the selected object has a “getImageUrl” method, it is invoked, and the resulting URL is used to download the image which is displayed. At some point, I’ll probably add the ability to provide the view with a JavaScript function to invoke on the object to get the image. I leveraged the Image View plugin I wrote about a few days ago for this so it displays a scaled version that gets enlarged when you hover over it.

Each of these DOMs is completely decoupled from the others. Everything is completely reusable.

Here’s a script that I wrote to test this out:


/*
* Menu: Fun
* DOM: http://needanupdatesite/org.eclipse.monkey.flickr
* DOM: http://needanupdatesite/org.eclipse.monkey.console
* DOM: http://needanupdatesite/org.eclipse.monkey.table
* DOM: http://needanupdatesite/org.eclipse.monkey.image
*/

function main() {
ImageView.show();
table = Table.getTable("photos");
table.clearColumns();
table.setName("Photos of Wayne");
table.addColumn("Id", "id", 100);
table.addColumn("Title", getTitle, 200);
table.addColumn("URL", "imageUrl", 200);
images = Flickr.search("Wayne");
table.setContents(images);
}

function getTitle(object) {
return object.title;
}

and here’s what it produces:

This is running in Eclipse 3.2M4. You can see the TableView open in the bottom right corner, displaying the results of searching Flickr. The image view (bottom left) displays the image corresponding to the object selected in the table. When you change your selection, the image changes.

There’s still a lot of work to do to make this really useful. Each of the DOMs is pretty simple and each basically does what I need it to do and little more. I’m going to spend some time making a little more useful (like make the table sortable on all columns). Also, this isn’t really much of a mashup yet since I’m only using one service. Now that I’ve proven that this works, I need to make DOMs for more services and work out how to actually mash things together…

And then, of course, I have to sort out how to package this all up in an Eclipse RCP application. I have some thoughts about this that I’m going to explore over the next couple of days. When I get this sorted out, it will be possible to create an Eclipse RCP using JavaScript. Maybe that will open the doors for other scripting languages…