Eclipse Plugin Development – Add a Menu Using Wizard

The following example is actually from eclipse. It simply adds a Menu to the menu bar and a icon to the tool bar. When the menu item in the new menu or the icon in tool bar is clicked, a message dialog pops up.

File->New->Project->Plug-in Project

Project name: HelloWordMenu

File Structure:

MANIFEST.MF specifies the dependency. The Menu depends on ui and runtime. The file can be generated by using the wizard, so no worry about this part, the same for plugin.xml.

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: HelloWorldMenu
Bundle-SymbolicName: HelloWorldMenu; singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: helloworldmenu.Activator
Require-Bundle: org.eclipse.ui,
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.6

plugin.xml specifies the extension. It is the central place for configuring the plugin being developed. This sample plugin's extension point is org.eclipse.ui.actionSets.

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
            label="Sample Action Set"
               label="Sample Menu">
               label="Sample Action"
               tooltip="Hello, Eclipse world">

The GUI for extension is as follows:

As shown above, in this example extension actionSets is required. (How to decide which is required will be explained later)

The Java code in and is straightforward if you don't ask further about how the framework works.

Here is run method from file. The SampleAction class implements IWorkbenchWindowActionDelegate interface. It is the callback method called by host plugin. The action proxy will be created by the workbench and shown in the UI. When the user tries to use the action, this delegate will be created and execution will be delegated to it.

public void run(IAction action) {
			&quot;Hello, Eclipse world&quot;);


package sampleview.actions;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PlatformUI;
import org.eclipse.jface.dialogs.MessageDialog;
 * Our sample action implements workbench action delegate.
 * The action proxy will be created by the workbench and
 * shown in the UI. When the user tries to use the action,
 * this delegate will be created and execution will be 
 * delegated to it.
 * @see IWorkbenchWindowActionDelegate
public class SampleAction implements IWorkbenchWindowActionDelegate {
	private IWorkbenchWindow window;
	 * The constructor.
	public SampleAction() {
	 * The action has been activated. The argument of the
	 * method represents the 'real' action sitting
	 * in the workbench UI.
	 * @see IWorkbenchWindowActionDelegate#run
	public void run(IAction action) {
			"Hello, Eclipse world");
	 * Selection in the workbench has been changed. We 
	 * can change the state of the 'real' action here
	 * if we want, but this can only happen after 
	 * the delegate has been created.
	 * @see IWorkbenchWindowActionDelegate#selectionChanged
	public void selectionChanged(IAction action, ISelection selection) {
	 * We can use this method to dispose of any system
	 * resources we previously allocated.
	 * @see IWorkbenchWindowActionDelegate#dispose
	public void dispose() {
	 * We will cache window object in order to
	 * be able to provide parent shell for the message dialog.
	 * @see IWorkbenchWindowActionDelegate#init
	public void init(IWorkbenchWindow window) {
		this.window = window;

Run this plugin as "Eclipse Application".

This is a very simple example. A real development job normally involved with program analysis, such as using JDT Java Model, ASTParser, etc.

Category >> Rich Client Platform(RCP)  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";

  1. artyom on 2012-8-5


    can I extends class, like this
    public class SampleAction extends MyClass implements IWorkbenchWindowActionDelegate ?

  2. Admin on 2012-8-6

    I’m not sure, but it seems no problem.

  3. Marlon Leite de Albuquerque on 2015-9-1

    Em Delphi é só escolher o componente menu, dar duplo clique para abrir o editor de menu!!
    Alguém vai dizer ah mas não é multi-plataforma! Não era, o cara só tem informação do tempo do Delphi 7.0!

Leave a comment