Adding Commands and Menus in RCP

In this tutorial, I intend to create a simple File menu in my RCP application.
Here I will assume that the readers already some basics about creating a sample RCP application. If you have no idea how to do it, you an visit these tutorials - Creating Simple RCP Application and Export RCP Product.

So, we want to create a File menu , with some sub menus. And, we want to do something when we click on them. For that purpose, we need to add some commands to the menus. Let's see how.

Step 1: Create a simple RCP application with Hello World template. (visit this tutorial for reference)

Step 2: Now we will open MANIFEST.MF and add some extensions for commands to it.

a. Click on Add. type commands in the search box. Select org.eclipse.ui.commands.




b. The extension will be added to your MANIFEST.MF.

Step 3: Adding some commands.

a. Right click on org.eclipse.ui.commands from the list of added extensions. Click on New -> Command.





b. New command section will be opened. Provide your desired id and name for the command in this section. Then save it.

















c. We need to add some functionalities as well to the commands. Let' say we want to print "Hello World" when newCommand is executed. For this purpose, we need to create a handler for this command.
Click on defaultHandler (blue hyperlink) in Extension Element details , A new Java file wizard is opened. Let us give a name newFileHandler and put it inside package com.src.handlers. Remove existing handler interfaces from the handler class and implement AbstractHandler.
A blank java class will be generated.

package com.src.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;

public class newFileHandler extends AbstractHandler {

 @Override
 public Object execute(ExecutionEvent event) throws ExecutionException {
  // TODO Auto-generated method stub
  return null;
 }

}

d. Now add some functionality to it.
Inside execute function we will add -

System.out.println("Hello World");


Now, the method looks like this
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
 System.out.println("Hello World");
 return null;
}

Step 4: Now, add menu in the application.

a. Add extension org.eclipse.ui.menus to the application.
b. Right click on org.eclipse.ui.menus. Then , click New -> menuContribution. Type menu:org.eclipse.ui.main.menu in locationURI* and leave all the sections as it is. Save it.












Remember to add proper locationURI, otherwise you can have exceptions while running the code :
Invalid menu URI: HelloWorld.myMenu

c. Again, right click on menuContribution -> New ->  command. New command section will be opened.




d. Add the desired id, label and tooltip for the command. Save.




e. Now, we will attach a command to a menu. So, create a new command by right click on the menu -> New -> command. Then, provide the command id(the command we previously created) by searching using Browse button and provide command label. Save.




Step 5: Now, it is time to run the application. The application now looks like this.


Click on New . The console will print "Hello World".

Step 6: Now it's time to add some icons to those mundane menus! For that, open plugin.xml. In this file, you will find some entry corresponding to the Command we have generated. It looks something like this:

 
           <command

                  commandId="HelloCommand.NewCommand"

                  label="New"

                  style="push">

            </command>


Now we will simply add some image to it. After adding the image it will look like this:

 
          <command

                  commandId="HelloCommand.NewCommand"

                  label="New"

                  icon="/icons/new_con.gif"

                  style="push">

            </command>


I have added several Commands now in my application. Now, it looks just wonderful!