Category Archives: eclipse

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

Using Eclipse WTP for developing application under Websphere Application Server Community Edition (WAS CE)

This tutorial assumes that you already have Eclipse WTP (Galileo) and WAS CE (2.1) installed correctly. To make sure that your Websphere is installed correctly, start the server and go to the console. If you can do so, you’re in the right track. After that, just shutdown the server because we will manage the server from Eclipse.

After that, the first step is to install the Eclipse plugin for WAS CE. This plugin contains several things, and one of it is WTP server adapter for WAS CE. The update site for the plugin is http://download.boulder.ibm.com/ibmdl/pub/software/websphere/wasce/updates/

Restart Eclipse like usual after installing the plugin.

Now we are ready to develop our first project under WAS CE. Create a new ‘Dynamic Web Project’. You’ll get something like this:

2009-10-20_1235

What’s important here is to change the Target runtime to ‘IBM WASCE 2.1’, so if you haven’t had one, you should create a new target runtime by clicking the ‘New…’ button. I think the creation of target runtime is pretty self explanatory. You just need to put the directory name of where you installed the WAS CE. This step is critical because if you don’t change the Target runtime now, the application can’t be run correctly from Eclipse.

After that, you will be asked to put the usual information for a dynamic web project but if click next and next, you will get a custom ‘Geronimo Deployment Plan’. Here you need to put the name of group id, artifact id, version, and artifact type. This information is needed because your application will be seen like a normal system resource, which can be a library or an web application.

2009-10-20_1241

After that you will get a new web project. What’s different in this web project is there is a geronimo-web.xml under WEB-INF and IBM WASCE 2.1.will added as referenced library.

You can start developing your web application and running it using normal way.

Eclipse Tips: Debugging your multi thread application (2)

This post is another tips of debugging your multi thread application. If you are interested on how to debugging a multi thread application, probably you will find my first post useful as well.

In a multi thread application, it’s still possible to use a certain class from different threads. The problem is if you try to set a breakpoint in this shared class, the breakpoint will be valid for all threads. This is typically just slowing our debugging session.

With Eclipse, you can set a breakpoint that only valid in certain threads. The catch is since there is no way that Eclipse knows what threads will be available, you can only set this on runtime.

To do this, you need to first set the breakpoint you want (remember, you probably need to Suspend the VM as well).

After that, you can start the debugging session and wait until the application is suspended. In this occasion, let open the breakpoint properties. In this window, there are two elements in the left tree: Breakpoint Properties and Filtering. Select Filtering and after that you can set to which thread the breakpoint should be valid for.

2009-08-26_1300

There are two more catches.

  • If the intended thread is not running yet, this method is not usable.
  • Every time the debugging session is restarted, you’ll need to filter it again.

Despite that catches, I think this is nice feature to know by multi-thread application developers and who know, it may be useful sometime in the future.

Eclipse Tips: (Debugging) Ignoring certain classes from being stepped into

Some of us may have encountered a not so nice experience where you basically got an strange result from a method and decide to step into that method as deep as possible. The problem is somehow you are lost in track and you can’t debug furthermore except if you’re exiting the debug mode or resuming the debug. Both are not the best solution because you have to inspect the application one more time.

The symptom of the problem is usually you a “Source not found” on your editor. If you are trying to ‘Step Return’ you are basically lost in track because you can not return to a class with source code.

For testing, you may try to write a log, for example:

logger.info("whatever");

If you’re trying to step into this method, you may get (since this obviously depends on your Eclipse configuration, you may also not get this screenshot):

2009-08-24_1210

Now try to ‘Step Return’ several times… do you still get the ‘Source not found’ message?

Why?

It’s actually easy to understand if you look at the debug view.
2009-08-24_1215

You are basically step into a long method calls, which most of them doesn’t have line information. Eclipse will automatically ignore these methods when you are stepping into. But then suddenly, it touched a method equals from class ‘Class’ which does have line information, but no source attached for it.

So, if you are patient enough, you can just do ‘Step Return’ several times (in this case about 16 times) and you’ll return to your class (with nice source code 🙂 ). This is obviously too much work.

One other alternative is to ignore certain classes to be stepped into. In Eclipse, the name of the functionality is called ‘Step Filters’. To use this functionality to ignore JDK classes, you have to configure Eclipse.

Go to the Preferences dialog, and open Java → Debug → Step Filtering.

2009-08-24_1227

Just ‘Select All’ and click ‘OK’. Now… If you are still trapped in the equals method, you can simply ‘Step Return’ and you’ll be back to your source code.

Once you’ve set the configuration in the Preferences, you can enable and disable this Step Filters anytime from your Debug perspective by clicking this button on Debug view.

2009-08-24_1230

Now… there is no reason to redo your entire debug session if you’re trapped in such situation.

Happy debugging!

Eclipse Tips: Auto generate ‘final’ on your variable, field, and parameter

If you are reading a lot of blogs about Java lately, you’ll find more and more people suggesting to use ‘final’ everywhere. The main argument is by using ‘final’ we will have a code with less bug. This post will not argue with the up/down side of the suggestion, if you eager to do that, just do that somewhere else (here for example).

However, if you are convince with such argumentation, probably the first thing on your mind is: should I now change all my code? How much time do I need to develop this instinct until all of my code is using ‘final’ everywhere?

Fortunately for Eclipse user, there is a functionality in Eclipse to help we develop this instinct. And it even will fixes our mistakes and we will get a consistent look of our code.

Let’s look at a code example:

2009-08-13_1139

There are some places where ‘final’ could be used. Now… open the Preferences dialog (under Window on Windows and Linux, under Eclipse on Mac). Navigate to Java->Editor->Save Actions.

2009-08-13_1142

Tick the ‘Perform the selected actions on save’ checkbox and ‘Additional actions’ checkbox. After that, click the ‘Configure…’ button.
2009-08-13_1146

What we’re interested in is in tab ‘Code Style’. Tick all ‘Private fields’, ‘Parameters field’, and ‘Local variables’. Click ‘OK’

Now you can go back to your source code and do small change, revert it back (Eclipse prevents saving if there is no change in the source code), and save it. Tarammm…. you get ‘final’ everywhere.

2009-08-13_1151

Note that this doesn’t put final on your method and class, you still have to do that manually.

Now you can enforce your team to use ‘final’ everywhere if you like!

Lambda4JDT: lambda expression on your Java code

This is just another proof how many people are starting to want some functional syntaxes on Java. Some lambda expressions are possible to be implemented using current Java, but there are just too many noise in the result.

Lambda4JDT provides a new plugin for Eclipse that can collapse the noise in the implementation without changing the syntax of Java. I must say that the approach it takes is pretty unique and original.


My top Eclipse keyboard shortcut

eclipse logo

If you use Eclipse a lot you must have your favorite keyboard shortcut. Here are mine:

  1. Context Assist (CTRL+SPACE), I believe this is all favorite since they just have so many functionality. You want assistance about class name/method name/variable name. You get all. More than that, it also shows the template available for the context. It even suggests the possible variable name/method name. And it works in all places, even in the dialogs. This is a must know keyboard shortcut for all Eclipse user. Without it, you just will not get anything from Eclipse.
  2. Format (Shift+Ctrl+F), I usually type anything as fast as I can without bothering the format of the code. This shortcut will tidy up all the clutter and make my code readable.
  3. Save (Ctrl+S). OK, I know that this is a must for nearly all applications.
  4. Quick Fix (Ctrl+1). You get error or warning? Many times you will find that this shortcut can solve the problem for you.
  5. Rename (Ctrl+2+R). Since I’m a refactoring guy, I never escape from this task, renaming variables/methods/classes.
  6. Quick Type Hierarchy (Ctrl+T). Nice way to get information about classes tree without opening JavaDoc

So if you are also using Eclipse, what’s your favorite keyboard shortcut?