How to add and enable a menu button using the eclipse rcp extension (by code and using the plugin UI Interface)

How to add and enable a menu button using the eclipse rcp extension (by code and using the plugin UI Interface)

To learn eclipse-rcp, I keep on reading, try some tutorials available on the net. My favorite is this one: http://www.vogella.de/articles/RichClientPlatform/article.html, it's very informative and covers a lot of topic.

Since eclipse is constantly updating, I found some features that are already updated in eclipse. For example the Command-Menu mapping.

Consider the default Hello RCP Example which has the default classes: ApplicationActionBarAdvisor, etc. To add a main menu, which has an Exit Button, we simply edit this class like this:
package de.vogella.rcp.intro.dialogs.standard;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.commands.ActionHandler;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;

public class ApplicationActionBarAdvisor extends ActionBarAdvisor {
 private IAction exitAction;

    public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
        super(configurer);
    }

    protected void makeActions(IWorkbenchWindow window) {
     exitAction = ActionFactory.QUIT.create(window);
     register(exitAction); //this line enables the menu
    }

    protected void fillMenuBar(IMenuManager menuBar) {
     MenuManager fileMenu = new MenuManager("&File");     
     fileMenu.add(exitAction);     
     menuBar.add(fileMenu); //add the File Menu group to the main horizontal menu
    }    
}
This set of action can easily be done using the plugin interface already available in eclipse with plugin development tools. Following the tutorial on de.vogella we should arrive at the screen similar to the image attached.


The menu should look like this:
+Horizontal Menu
+File
+Exit //if you will notice this menu is disable by default

To enable the menu we have to look at the defaulthandler class associated with the Exit Menu, as of eclipse-rcp 3.5 there are 2 methods that we should be concerned with isEnabled and isHandled:

+isEnabled - makes the menu enabled
+isHandled - triggers the event in the execute method

These 2 methods must be enabled to complete add and execute a command in the main menu.

Also take note, that the action now implements the interface IHandler and not extend the AbstractHandler class.
How to add and enable a menu button using the eclipse rcp extension (by code and using the plugin UI Interface) How to add and enable a menu button using the eclipse rcp extension (by code and using the plugin UI Interface) Reviewed by czetsuya on Friday, October 30, 2009 Rating: 5

No comments:

Powered by Blogger.