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,
 org.eclipse.core.runtime
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"?>
<plugin>
   <extension
         point="org.eclipse.ui.actionSets">
      <actionSet
            id="SampleView.actionSet"
            label="Sample Action Set"
            visible="true">
         <menu
               id="sampleMenu"
               label="Sample Menu">
            <separator
                  name="sampleGroup">
            </separator>
         </menu>
         <action
               class="sampleview.actions.SampleAction"
               icon="icons/sample.gif"
               id="sampleview.actions.SampleAction"
               label="Sample Action"
               menubarPath="sampleMenu/sampleGroup"
               toolbarPath="sampleGroup"
               tooltip="Hello, Eclipse world">
         </action>
      </actionSet>
   </extension>
</plugin>

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 SampleAction.java and Activator.java is straightforward if you don’t ask further about how the framework works.

Here is run method from SampleAction.java 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) {
		MessageDialog.openInformation(
			window.getShell(),
			&quot;HelloWorldMenu&quot;,
			&quot;Hello, Eclipse world&quot;);
	}

Complete SampleAction.java

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) {
		MessageDialog.openInformation(
			window.getShell(),
			"SampleView",
			"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.

3 thoughts on “Eclipse Plugin Development – Add a Menu Using Wizard”

  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!

  2. Hi

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

Leave a Comment