Commands Part 3: Parameters for Commands
Posted On Jan 7, 2009 at by Prakash G.R.Think of a command which opens a file in an editor. You can't possibly create an extensive list of "Open File - somefile.txt", "Open File - someotherfile.txt", etc. Rather you want a generic command like "Open File" with the actual file as a parameter. In this tip, lets see how to create such a command.
The extension point for the command and the parameter:
<command name="Open File"
defaultHandler="com.eclipse_tips.commads.OpenFileHandler"
id="com.eclipse-tips.commands.openFileCommand">
<commandParameter
id="com.eclipse-tips.commands.filePathParameter"
name="File Path">
</commandParameter>
</command>
The handler code looks like:
public Object execute(ExecutionEvent event) throws ExecutionException {
String filePathParam = event.getParameter("com.eclipse-tips.commands.filePathParameter");
IPath filePath = new Path(filePathParam);
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(filePath);
IWorkbenchPage activePage = ...// get the activePage
try {
IDE.openEditor(activePage, file);
} catch (PartInitException e) {
;// display error message
}
return null;
}
As you can see, the ExecutionEvent will have all the parameters, which can be accessed thru their ids. Once you get the parameter string, you can use it in your own way. But exactly do we pass different parameters to that command? When you put the command in a toolbar/menu you can specify the parameter:
<menuContribution
locationURI="toolbar:org.eclipse.ui.main.toolbar">
<toolbar
id="com.eclipse-tips.commands.toolbar1">
<command
commandId="com.eclipse-tips.commands.someCommand"
id="com.eclipse-tips.commands.someCommandInToolBar">
</command>
<command style="push"
commandId="com.eclipse-tips.commands.openFileCommand"
label="Opens somefile">
<parameter
name="com.eclipse-tips.commands.filePathParameter"
value="MyProject/somefile.txt">
</parameter>
</command>
<command style="push"
commandId="com.eclipse-tips.commands.openFileCommand"
label="Opens some other file">
<parameter
name="com.eclipse-tips.commands.filePathParameter"
value="MyProject/someotherfile.txt">
</parameter>
</command>
</toolbar>
</menuContribution>
The above example is a naive one. Lets see the real world example: "Show View" command. The command is generic and can show any view. The view id is given to the command as a parameter:
<command
name="%command.showView.name"
description="%command.showView.description"
categoryId="org.eclipse.ui.category.views"
id="org.eclipse.ui.views.showView"
defaultHandler="org.eclipse.ui.handlers.ShowViewHandler">
<commandParameter
id="org.eclipse.ui.views.showView.viewId"
name="%command.showView.viewIdParameter"
values="org.eclipse.ui.internal.registry.ViewParameterValues" />
<commandParameter
id="org.eclipse.ui.views.showView.makeFast"
name="%command.showView.makeFastParameter"
optional="true">
</commandParameter>
The list of all possible values of the parameter is given by the class ViewParameterValues. The class would iterate thru the view registry and return it. This information is used in defining the key bindings:
If we define our own parameter values, we can also get our command in the key bindings page:
Defining the parameters is all about returning a map of display names & the ids:
public class FileParameters implements IParameterValues {
public Map getParameterValues() {
Map params = new HashMap();
params.put("Some File", "MyProject/somefile.txt");
params.put("Some Other File", "MyProject/someotherfile.txt");
return params;
}
}
The name would be displayed in the key bindings page and the id would be used to invoke the command when the key sequence is pressed.
See also:
Part 1: Actions Vs Commands
Part 2: Selection and Enablement of Handlers
Part 4: Misc items ...
Part 5: ISourceProvider & dynamically updating Commands
Part 6: 'toggle' & 'radio' style menu contribution
Tired of unwanted and irrelevant results in Google? Try Eclipse Search, the Customized Seach Engine that gives you only relevant results - Powered by Google! It has plugins for Eclipse and Firefox/IE
Are you interested in turning these posts into an article for Eclipse Corner?
Please see Bug 223445
Your example showed passing strings as parameters. But if my goal is to select a file in the Navigator for example, show a command label in the popup menu on the file and then have my handler take some action on the selected file - How does one pass the file name as a parameter back to the command and handler?
Nice series of tips on commands. It got me to think about reworking my old
actions.
Having one problem though...
The example shows passing static strings as parameters but if the goal is to run a handler on a user selected resource, how does one pass the selection/ resource back to the command/handler from the menuContribution/command?
(IActionDelegate had selectionChanged for this)
John C.
@Wayne,
Yes, I'll be very much interested :-)
@Anonymous #1:
I don't think you can change the name/label of a command.
@Anonymous #2:
In your handler's execute method,
HandlerUtil.getVariable(event, ISources.ACTIVE_CURRENT_SELECTION_NAME);
should give you the current selection. But you don't need a parameterized command for this. A normal command itself would do.