Reference Link: http://www.vogella.de/articles/EclipseCommands/article.html
Command is generally a certain operation that can be performed based on a certain UI component in the Eclipse workbench.
Commands are extended via "org.eclipse.ui.commands". The behavior of a command can be defined via a handler.While programming for Eclipse Commands, you will need the following 3 items.
-
Command - Declarative description of the component
-
Handler - Defines the behavior of the command. The handler is the class which will be executed once the command is called and must implement the interface "org.eclipse.core.commands.IHandler". In most cases you extend "org.eclipse.core.commands.AbstractHandler" which provides a default implementation for most of the IHandler methods.
-
UI - Where and how should the command be included.
Once created Commands can be used at many locations in an eclipse workbench, such as (menus, toolbars and / or context menus). Menu's are similar to Actions, but not exactly the same.
____________________________________
>>>Defining commands :
Let's create a command that will exit an application.1. Create an RCP project with th e"Hello" template.
2. Add "org.eclipse.ui.commands" to the "extensions".
3. now, add "command" to this extension in the plugin.xml.
4. Set the Id & Name parameters of thsi 'command' based on your required operation. In our case, it is "Exit"
5. Press the hyperlink "defaultHandler" to create the class which should extend
"org.eclipse.core.commands.AbstractHandler".
(Or)
You could extend another plugin, "org.eclipse.ui.handlers" and add "handler" to it. Now, specify the enter the command ID text with the ID of your previously created "Command" and Press the hyperlink "class" to create the class for the handler.
Now, our handler class can be implemented with 2 options:
(i) Exit the application when the menu item from our main menu(in the menu bar) is clicked
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
MessageDialog.openInformation(
window.getShell(),
"Properties",
"MyCommand Msg Box");
return null;
}
or
(ii) pop up a message dialog
public Object execute(ExecutionEvent event) throws ExecutionException {
// TODO Auto-generated method stub
HandlerUtil.getActiveWorkbenchWindow(event).close();
return null;
}
The Handler class could be modified based on one of the 2 options above.
The "execute" method in your Handler class is responsible for the action performed when your command is executed.
We have now created a command. NOw we need to use it.
*****************************
>>>>Using commands in menus :
Let's add our 'exit' command to the MainMenu of our application.1. Add "org.eclipse.ui.menus" to the "extensions".
2. now, add "menuContribution" to this extension in the plugin.xml with the location URI "menu:org.eclipse.ui.main.menu". This will add your command to the main menu of yoru application.. Similarly, your could modify the URI field for adding yoru command to the toolbar or to the View-toolbar, etc based on the table below:
Contribution to | Description | Uri |
---|---|---|
Application menu | Displays the command in the menu of the application | "menu:org.eclipse.ui.main.menu" |
Application toolbar | displays the command in the toolbar of the application | "toolbar:org.eclipse.ui.main.toolbar" |
View toolbar | displays the command in the toolbar of the view | "toolbar:viewId". For example to display a menu to view with the Id "View1" use "toolbar:View1". |
Context menu / pop-up | Command is displayed in a content menu, e.g. right mouse click on an object | n.a. |
3.now, add "menu" to the "menuContribution" and set the parameters as below:
***************************
You have now
1.created a command
2. created a file menu with the command as its functionality.
Now run your application as an RCP application/Eclipse-Workbench and check to see that the file closes when the filemenu is clicked.
If your application is run , then you could access your command from the main menu.