Vasanthi Sathyanarayanan
Senior Software Developer at Genuitec
Senior Software Developer at Genuitec
A command is the declaration of a behavior by id. Commands are used to declare semantic behavior and the corresponding action implementations can be defined elsewhere by handlers.
There are several advantages of command handlers:
Complete the following steps to contribute a menu and define its action through command-handlers:
A simple example of menu contributions
Use the following table to view sample menu contributions. Refer to the image that follows the table to view where the menu contributions appear in the RCP application; the corresponding example number is listed in the table.
Note: The required command and handler extension code is given after the table and images.
main toolbar action toolbar:org.eclipse.ui.main.toolbar Main toolbar – Ex2 |
<extension point="org.eclipse.ui.menus"> <menuContribution allPopups="false" locationURI="toolbar:org.eclipse.ui.main.toolbar"> <toolbar id="demo.rcp.search.toolbar" label="Search toolbar"> <command commandId="rcp.demo.actions.searchcommand" icon="icons/search.png" label="Search Files" style="push" tooltip="Search files"> </command> </toolbar> </menuContribution> NOTE: A toolbar element needs to be created for adding a main toolbar item. |
view context menu action popup:viewId View context/popup menu – Ex6 |
<menuContribution locationURI="popup:RCP2.DemoView?after=additions"> <command commandId="rcp.demo.actions.runcommand" label="Run" style="push" icon="icons/search.png"> </command> </menuContribution>
|
view toolbar action toolbar:viewId View toolbar – Ex3 |
<menuContribution allPopups="false" locationURI="toolbar:RCP2.DemoView?after=additions"> <command commandId="rcp.demo.actions.searchcommand" label="Search" icon="icons/search.png" tooltip="Search files" style="push"> </command> </menuContribution>
|
main menu action menu:org.eclipse.ui.main.menu Main menu item with submenus – Ex4 & 5 |
<menuContribution allPopups="false" locationURI="menu:org.eclipse.ui.main.menu"> <menu id="demo.rcp.search.menubar" label="Search"> <!-- main menu --> <command commandId="rcp.demo.actions.searchcommand" icon="icons/search.png" label="Search Files" style="push" tooltip="Search files"> <!-- Sub menu --> </command> </menu> </menuContribution> main menu action
|
main menu action menu:org.eclipse.ui.main.menu direct menu Direct Main menu item – Ex1 |
<menuContribution allPopups="false" locationURI="menu:org.eclipse.ui.main.menu"> <command commandId="rcp.demo.actions.MyProductCommand" label="Application Info" style="push"> </command> </menuContribution>
|
Examples of menu contributions
In the examples below, the `rcp.demo.actions.searchcommand` command is defined a defaultHandler, whereas the `rcp.demo.actions.runcommand` command defines its handler using the `org.eclipse.ui.handlers` extension. Using the second method, we can define multiple handlers for the same command based on condition and also have more control when the handler has to be active based on the conditions defined.
Note: Additional details can be found at dzone.com and eclipse.org.
<extension point="org.eclipse.ui.commands"> <command defaultHandler="rcp2.handlers.SearchCommandHandler" id="rcp.demo.actions.searchcommand" name="Search Command Handler"> </command> <command id="rcp.demo.actions.runcommand" name="Run Command Handler"> </command> </extension> <extension point="org.eclipse.ui.handlers"> <handler class="rcp2.handlers.RunCommandHandler" commandId="rcp.demo.actions.runcommand"> </handler> </extension>
package rcp2.handlers; import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; public class RunCommandHandler extends AbstractHandler { @Override public Object execute(ExecutionEvent arg0) throws ExecutionException { // Define the required actions here return null; } }
If you have any comments or questions, we would love to hear from you @MyEclipseIDE on twitter or via the MyEclipse forum.