Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » Passing parameters while executing command programmatically
Passing parameters while executing command programmatically [message #334206] Mon, 26 January 2009 17:34 Go to next message
Raja Kannappan is currently offline Raja KannappanFriend
Messages: 24
Registered: July 2009
Junior Member
I want to execute my command programmatically. I've defined a command in
plugin.xml and it has a corresponding handler (also defined in
plugin.xml). The following code calls the handler programmatically:

IHandlerService handlerService = (IHandlerService)
getSite().getService(IHandlerService.class);
try {
handlerService.executeCommand("MyCommandId", null);

}
catch (Exception ex) {
throw new RuntimeException("MyCommandId not found");
}

This works, but I want to pass in parameters to my handler. My handler has
the execute method and it takes ExecutionEvent object. The signature looks
like this:

public Object execute(ExecutionEvent event) throws ExecutionException {}

But, handlerService.executeCommand take Event as the second parameter and
not ExecutionEvent.

Is there any way to pass the parameters to my handler while calling it
programmatically?

Thanks in advance,

- Raja.
Re: Passing parameters while executing command programmatically [message #334211 is a reply to message #334206] Mon, 26 January 2009 18:53 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

There are parameters (part of the command) and variables (part of the
application context). You can create a ParameterizedCommand by using
org.eclipse.core.commands.ParameterizedCommand.generateComma nd(Command, Map)

If you want to add a variable to the execution context on the fly, you
would have to use something like
org.eclipse.ui.handlers.IHandlerService.createExecutionEvent (ParameterizedCommand,
Event) to generate the execution event, and then you can use:

((IEvaluationContext)event.getApplicationContext()).addVaria ble( "variable",
value);

Appropriate tests before casts, of course

PW

--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/ganymede/index.jsp?topic=/org.eclips e.platform.doc.isv/guide/workbench.htm


Re: Passing parameters while executing command programmatically [message #334219 is a reply to message #334211] Mon, 26 January 2009 21:38 Go to previous messageGo to next message
Raja Kannappan is currently offline Raja KannappanFriend
Messages: 24
Registered: July 2009
Junior Member
Thanks for the explanation on how to create parameterizedcommand and
executionevent.

But, the iHandlerService.executionCommand takes the command id and Event
(not an executionevent). My question is how do I get the values passed to
the invocation so that I can retrieve them using ExecutionEvent in my
handler?

My invocation looks like this:

handlerService.executeCommand("MyCommandId", null);

and handler's execute method looks like this:

public Object execute(ExecutionEvent executionEvent) throws
ExecutionException {}

The executeCommand method calls the exceute() method on the handler, but
executionEvent comes as null. What should I do so that I get a valid
executionEvent during execute()?

Thanks.
Re: Passing parameters while executing command programmatically [message #334228 is a reply to message #334219] Tue, 27 January 2009 14:41 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

You know how to create an execution event (my last post).

So use ICommandService to get the Command and then call
org.eclipse.core.commands.Command.executeWithChecks(Executio nEvent)

PW


--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/ganymede/index.jsp?topic=/org.eclips e.platform.doc.isv/guide/workbench.htm


Re: Passing parameters while executing command programmatically [message #334230 is a reply to message #334228] Tue, 27 January 2009 21:00 Go to previous messageGo to next message
Raja Kannappan is currently offline Raja KannappanFriend
Messages: 24
Registered: July 2009
Junior Member
I followed your steps but ParameterizedCommand.generateCommand(command,
params) returns null, even though command and params are valid objects. My
code looks like this:

ICommandService commandService = (ICommandService)
getSite().getService(ICommandService.class);
IHandlerService handlerService = (IHandlerService)
getSite().getService(IHandlerService.class);

Command showElement =
commandService.getCommand("com.xyz.commands.showElement");

Map<String, Object> params = new HashMap<String, Object>();
params.put("viewId", ID);
ParameterizedCommand paramShowElement =
ParameterizedCommand.generateCommand(showElement, params);

ExecutionEvent execEvent =
handlerService.createExecutionEvent(paramShowElement, new Event());
try {
showElement.executeWithChecks(execEvent);
}
catch (Exception ex) {
throw new RuntimeException("com.xyz.commands.showElement
could not be executed");
}

Am I missing something? Thanks,

- Raja.
Re: Passing parameters while executing command programmatically [message #334236 is a reply to message #334230] Wed, 28 January 2009 14:23 Go to previous message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Raja Kannappan wrote:
> I followed your steps but ParameterizedCommand.generateCommand(command,
> params) returns null, even though command and params are valid objects.
> My code looks like this:
>
> ICommandService commandService = (ICommandService)
> getSite().getService(ICommandService.class);
> IHandlerService handlerService = (IHandlerService)
> getSite().getService(IHandlerService.class);
> Command showElement =
> commandService.getCommand("com.xyz.commands.showElement");
> Map<String, Object> params = new HashMap<String,
> Object>();
> params.put("viewId", ID);
> ParameterizedCommand paramShowElement =
> ParameterizedCommand.generateCommand(showElement, params);
> ExecutionEvent execEvent =
> handlerService.createExecutionEvent(paramShowElement, new Event());
> try {
> showElement.executeWithChecks(execEvent);
> } catch (Exception ex) {
> throw new RuntimeException("com.xyz.commands.showElement
> could not be executed");
> }
>
> Am I missing something? Thanks,

Does your command definition include a commandParameter? parameters are
defined with the command, and tend to be static in nature (or simple,
a viewId is acceptable). For example, our showView command takes the
viewId, and our definition looks like:


<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>
</command>
[/xml]

Other than that, use
handlerService.createExecutionEvent(paramShowElement, null) to create
the ExecutionEvent (if you don't have an SWT event, don't create one).

PW

--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/ganymede/index.jsp?topic=/org.eclips e.platform.doc.isv/guide/workbench.htm


Previous Topic:Bundle Location in RCP
Next Topic:How to remove "New Editor" menu
Goto Forum:
  


Current Time: Thu Dec 26 20:15:26 GMT 2024

Powered by FUDForum. Page generated in 0.04206 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top