Wednesday, January 21, 2009

Eclipse RCP : Sorting JFace Viewer contents

The JFace viewer contents can be sorted by assigning them a ViewerSorter.


viewer.setSorter(new MyViewerSorter());

public class MyViewerSorter extends ViewerSorter
{
@Override
public int compare(Viewer viewer, Object o1, Object o2)
{
return o1.toString().compareToIgnoreCase(o2.toString());
}
}


This will sort the elements in alphabetical order.

Tuesday, January 20, 2009

Eclipse RCP: Command, Handler enabledWhen with a particular selection object

Command Framework
Command framework allows to add menubar and toolbar actions via handlers. To create a command in the application.

Create an extension of the commands


<extension point="org.eclipse.ui.commands">
<command defaulthandler="com.mycomp.app1.commands.SampleHandler" id="com.mycomp.app1.commands..SampleCommand" name="Add User">
</command>
</extension>

Implement the SampleHandler which is the default handler for the command. The handler should implement the interface IHandler or can simply extend AbstractHandler. Now we can add the Command to application main menubar, toolbar or to a menubar and toolbar of a view.

To add it a to a view's toolbar


<extension point="org.eclipse.ui.menus">
<menucontribution locationuri="toolbar:com.mycomp.app1.SampleView">
<command commandid="com.mycomp.app1.commands.SampleCommand" icon="icons/sample.png" label="Sample" style="">
</command>
</menucontribution>
</extension>

This will add the handler to the SampleView's toolbar. But to activate this, we need to have activeWhen command expression.


<activeWhen>
<equals variable="activePartId">
<value="com.mycomp.app1.SampleView"/>
</with>
</activeWhen>


Now the SampleHandler will be on the toolbar of the view. The handler execute method can get the activepart,selection, and different workbench values from HandlerUtil class.

HandlerUtil.getActiveWorkbenchWindow(event) or HandlerUtil.getCurrentSelection(event).

To enable and disable the action based on selection, enabledWhen needs to be extended, Now lets enable the action only for one selection.


<enabledWhen>
<with variable="selection">
<value="1">
</count>
</with>
</enabledWhen>

Sunday, January 18, 2009

Eclipse RAP: Creatiing a multiuser RAP Application

What is RAP ?

Rich Ajax Platform, a web development platform for RCP Developers. RAP uses the whole plugin architecture of the eclipse platform and provides a SWT port called RWT for the web browsers. RWT uses the qooxdoo java script library to create the User interfaces on the browser side.

Singleton

Singleton pattern is something which is widely used on the eclipse platform. But for a web based application where multiple users will be accessing the application we need session based singleton implementation.

SessionSingletonBase

Eclipse RAP provides a base class called SessionSingletonBase which provides a singleton implementation for each session and avoids the sharing the application state between the users.

so just extends the SessionSingletonBase and create a singleton instance as done below.


SampleAppPlatform extends SessionSingletonBase {

public static final SampleAppPlatform getInstance() {
return (SampleAppPlatform) getInstance(SampleAppPlatform.class);
}
}


and try accessing the RAP web application from multiple browser instances and check the SampleAppPlatform state. It will be different.



Friday, January 16, 2009

Eclipse: Build Product, Coverage and lot of Reports.

Continous Integration

Continuous integration is a practive where developers integrate their work frequently. This help the team increase their productivity by detecting the integrating errors as early as possible during the development. There are a lot of build tools which helps in automating the build process with different reports which helps the developers to increase their work productivity.

Ant, Maven

Ant and Maven are two build tools which are very widely used in the java build process. Though ANT is a old and little complicated tool than the Maven, it gives the flexibility to do the build for any kind of software application.
Maven works great for almost all kinds of java application like web application, desktop applications. Maven also generates a whole lot of reports which helps the team at various levels to see the progress of the development.

Building Eclipse Products

Building eclipse product is always a difficult job if you want to do it outside the Eclipse Environment. And especially building eclipse product with maven not so easy work. Though there are existing plugins which does the product build little easier, they are not as efficient as the ANT counter part.

Since eclipse requires its own run time environment for building and running, the Maven structure does not help a lot. The following maven plugins does help in building eclipse plugins

But running the test cases and generating different reports based on this is a difficult process using the above tools.

Combining ANT and Maven

PluginBuilder is a eclipse plugin which provides an easy mechanism to create the ANT build scripts for building eclipse plugins, running test cases, and building the product. And it works out of the box.

Download it from http://www.pluginbuilder.org/

Create your plugin project with the test cases in a separate fragment project.

Create the build.xml and other supporting files for the ANT Build which creates Product, Test Report and Test Coverage Report.

Configure a ANT build step on the Automated build tool such as Luntbuild.

Now for the usual Maven reports and site generation we can use the Maven PDE plugin without the test option. This should not include the test fragments in build path.

This is it. You get a Product file, Unit Test Reports, Unit Test Coverage and all maven reports deployed as a site.

This tutorial doesnt give you step by step procedure but if you need help doing anything above. Just drop in a comment.

Eclipse: Making Heap Status visible

On eclipse based applications, there is a way to display the heap status on the status bar. The API which enables this feature is internal and the way to make it to visible is to create the plugin customization file.

Add the following property in the plugin.xml under the products extension

And on the plugin_customization.ini

org.eclipse.ui/SHOW_MEMORY_MONITOR=true

The SHOW_MEMORY_MONITOR parameter will be looked when the workbench is created and if it is true it will create the heap status bar.

Thursday, January 15, 2009

Eclipse: Create a file in the runtime workspace

Sometimes you may need to create a file in the runtime workspace temporarily or to store application wide data. This is better than storing the file inside the .metadata/.plugins/ folder since this will require having to have a dependency on plugin which is creating the file.


IPath path = Platform.getLocation();


This gives the path of the runtime workspace.