In the previous tutorial, I showed you how to create a basic Hello World Eclipse 3.x plugin using the plugin template wizard. This time I will show you how you can manually add (contribute) new menu and toolbar items to your own plugin.
Tutorial Outline:
Prerequisites
Command
Handler
Bindings
Menu Item
Toolbar Item
Run the plugin
Prerequisites
You have already created a basic Hello World plugin using the plugin template wizard.
Command
Each menu and toolbar item you contribute to Eclipse must be associated to a command. Command is action that is triggered whenever you click a menu or toolbar.
First step is to select the Extensions tab on the plugin configuration editor.
Right-click org.eclipse.ui.commands, then select New -> command from the popup menu.
While the new command is still selected, modify its properties with the following values:
The
handler is used to bind the command to a class that will execute the action.
The
first thing that you need to do is to create the handler class ClickMeHandler.
package com.melodycancode.plugin.helloworld.handlers;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;
public class ClickMeHandler extends AbstractHandler {
public ClickMeHandler() {
}
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
MessageDialog.openInformation(
window.getShell(),
"Click Me",
"Hello, I was just clicked.");
return null;
}
}
Right-click org.eclipse.ui.handlers, then select New -> handler from the popup menu.
While the new handler is still selected, modify its properties with the following values:
Now,
both the command and the class that you created from previous steps are already
associated to your newly created handler.
Note: If
you can’t see the ClickMe command
when you click the Browse button,
then you need to save the plugin configuration editor first.
Bindings
If you want to add shortcut key to the menu, then you need to create a binding for the menu command.
Right-click org.eclipse.ui.bindings, then select New -> key from the popup menu.
While the new binding is still selected, modify its properties with the following values:
The
shortcut key M1 + 7 has been assigned
to the ClickMe command. M1 is
equivalent to the CTRL key on Windows and the COMMAND key on Mac OS X. For a
complete list of possible Eclipse modifier keys, go to Eclipse documentation.
Menu Item
Right-click menu:org.eclipse.ui.main.menu?after=additions, then select New -> menu from the popup menu.
While the new menu is still selected, modify its properties with the following values:
Right-click the Custom Menu, then select New -> command from the popup menu. This action will create a command that will represent the Click Me menu item under Custom Menu.
While the new command is still selected, modify its properties with the following values:
Right-click toolbar:org.eclipse.ui.main.toolbar?after=additions, then select New -> toolbar from the popup menu.
While the new toolbar is still selected, modify its properties with the following values:
Right-click
the newly created toolbar, then select New
-> command from the popup menu. This action will create a command that
will represent the Click Me toolbar
item.
While
the new command is still selected, modify its properties with the following
values:
Right-click
on the plugin project, select Run As
-> Eclipse Application.
You
should now be able to see the menu and the toolbar that you added via plugin.
Selecting
the Click Me menu or toolbar item
will open a message dialog.
No comments:
Post a Comment