Tag Archive for 'rcp'

Two great tools to help your SWT application development

SWT teams has provided two great tools to help you develop your SWT applications. Just follow the installation procedure and you should be OK.

The standard Java Profiler like one from YourKit or JProfiler are not really usable in SWT environment, especially for part where GUI is involved. For just one example, the memory detection of those profilers show you a different number compared to the one you see from Windows Task Manager.

Sleak is a tool you can use to detect memory leak in SWT. For example, if you forgot to dispose some of your system resources, you can use this tool to help you find the culprit.

sleak

From the picture above, you can ‘Snap’ all the used system resources and get all new resources from the last time you clicked ‘Snap’ by clicking ‘Diff’. If the total number of resources is constantly increasing, you can suspect that there is a leak on your SWT application. By clicking each resource on the combo box, you will see the representation of this resource on the right panel. Once you found resources that should have already been disposed, go back to your code and do the necessary fix. Isn’t that great?

The second tool available is SWT Spy. This is a view plugin that allow you to get the information about a widget that is below your mouse cursor. If you ever wondered what widget is used in a view and how is the configuration to make it look like what you see, this plugin will help you a lot about it.

swt_spy

More about these tools:

  1. Use the SWT Development Tools

Eclipse RCP: ISharedImages

One good thing of using a RCP platform is you’ve got access to standard images that are often used. That is not valid, of course, if you are so obsessed about the icons and want to customize the icons.

In Eclipse you can access the standard images by using:

PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_DEF_VIEW);

Change the parameter with one from the constant in ISharedImages. You can check the list (unfortunately not complete) from this link.

If you want the complete list (and more exciting, will be valid all the times, even if the list get updated) by running my little application. You can download the complete source code from here.

Once you run the application, you will see all the images referred from ISharedImages like here:

listISharedImages

Moreover, if you click one of the button, you will get the text on the clipboard which you can paste wherever you want.

Eclipse RCP: Creating dynamic menu

Have you ever want to create a dynamic menu to your RCP application? Do you want to compute the contents of the menu on-the-fly at runtime? This is possible and many hacks can be done to do that.

But to do the correct thing is probably what annoyed your head every day. And actually, this has been supported by the framework.

First, what you need to do is creating a menu holder for your dynamic menu. This can be done by adding a new element named ‘dynamic’ under a menu contribution.

2009-11-13_1504

Now you need to implement a class that extends CompoundContributionItem.

2009-11-13_1506

Let’s make the menu like this;

public class ContributionItem1 extends CompoundContributionItem {

    @Override
    protected IContributionItem[] getContributionItems() {
        List<IContributionItem> menuContributionList = new ArrayList<IContributionItem>();

        menuContributionList.add(new CommandContributionItem(new CommandContributionItemParameter(Activator
                .getDefault().getWorkbench(), null, ActionFactory.QUIT.getCommandId(),
                CommandContributionItem.STYLE_PUSH)));

        return menuContributionList.toArray(new IContributionItem[menuContributionList.size()]);
    }

}

This is of course not a real example. In the reality, you will want to create a dynamic menu based on the condition. If the condition is changed, the menu will be automatically changed. I hope by using this small and unreal example, you will just get the idea.

Let’s try to run the application.

2009-11-13_1527

Great!

Embedding Jetty Server in Eclipse RCP

Eclipse has a nice tutorial on how to embed a Jetty server in an OSGI framework. Unfortunately, this tutorial doesn’t help you if you want to run the server in a standalone Eclipse RCP application. One use case where you need to have a web server in your RCP application is when your application need to be able to be called by an external entity.

Fortunately, as pointed by the tutorial, Eclipse has provided a Jetty plugin in its Java IDE. The documentation of Eclipse is provided using this Jetty server.

Eclipse documentation served from Jetty server

Eclipse documentation served from Jetty server

So, how do you embed a Jetty server in your Eclipse RCP application? I assume you already have a grasp knowledge on how to create a RCP application (after all, Eclipse provided you with a simple generator for that).

First, let’s add the jetty plugin to our dependencies. Open the tab ‘Dependencies’ in your plugin configuration. Then add these six plugins to the ‘Required Plug-ins’:

  • javax.servlet
  • org.eclipse.equinox.http.jetty
  • org.eclipse.equinox.http.regstry
  • org.eclipse.equinox.http.servlet
  • org.mortbay.jetty.server
  • org.mortbay.jetty.util

2009-11-06_1535

This is actually all that we need. However, the OSGI framework will not activate these plugins automatically. And since there is no code in your application that actually refers to these plugins, they will not be activated at all! We have to force the framework to run these plugins from start. It can be done by changing the ‘Auto-Start’ value from your Run configuration.

In the list of plugins included at the launch of application you need to change the ‘Auto-Start’ value for three plugins to true (if you are lazy, you can turn the default behavior to auto start but this is another concern):

  • org.eclipse.equinox.http.jetty
  • org.eclipse.equinox.http.regstry
  • org.eclipse.equinox.http.servlet

2009-11-06_1542

Now if you run the application you can check if your server is correctly running by accessing ‘http://localhost’. This should work flawlessly except maybe if you are not allowed to run server in port 80 or there is already a server running in port 80.

You can change the port by adding an argument to the VM arguments in ‘Run Configurations’. Add this value: ‘-Dorg.eclipse.equinox.http.jetty.http.port=8888′. Change ‘8888′ to whatever port you want the server to be running.

2009-11-06_1547

Now if you are running the application, you can access it from the port you mentioned before.

The next task is to define one (or several) servlet(s) that will serve any request the server gets. To do this, you need to open the ‘Extensions’ tab from your plugin configuration and add a new extension named ‘org.eclipse.equinox.http.registry.servlets’. After that add new ’servlet’. You need to mention the class name of the servlet, and an alias for that. One note here is you need to add slash in front of the alias. For example, if you want to make the servlet accessible from ‘http://localhost:8888/webserviceInterface’, then the alias value is ‘/webserviceInterface’. Of course, you need to implement a servlet which will do the work you want.

2009-11-06_1556

Now sit down, run the application, and enjoy the service from your web server (running directly from your RCP application)!