Tag Archives: rcp

Helios: How to Prepare Your Eclipse for RCP/Plugin Development

Some RCP-newbies are having difficulties to correctly setup their environment for RCP/Plugin development. Most of them are familiar with Eclipse but not using the Eclipse for RCP/RAP developers package. The problem is sneaky because everything is working except that you don’t get proposals when adding child to the extension. See the following video.

How to fix that? You have to install Eclipse RCP Plug-in Developer Resources from Helios repository and this video shows you how.

Now you can develop the RCP application as you like.

What is Eclipse-RCP-based Application?

To get the answer of that question, we need to take a look at the global architecture of an Eclipse-RCP-based application. This picture below can summarize the information.

SWT is Standard Widget Toolkit for Java. Developed by IBM and later by Eclipse Foundation, the purpose of SWT is to give native performance and look to Java application. SWT is implemented using JNI and so is not write-once-run-anywhere-library (WORA). This mean that each OS need specific SWT distribution.

JFaces is a layer above SWT which provides more convenience classes for generic application. JFaces is a WORA library.

OSGi is a standard developed by consortium with the same name to provide Java environment that is more dynamic. The goal is extensible, loosely-coupled applications with modules behind it.

Equinox is Eclipse implementation of OSGi.

Eclipse Core and Eclipse UI are classes that form the platform of Eclipse. All Eclipse-RCP-based applications will use this classes. Among other things, it provides extension framework, command framework (and somewhat-deprecated Action framework), and basic application-window management.

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)!