Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » How to programmatically create ParameterizedCommand
How to programmatically create ParameterizedCommand [message #510636] Thu, 28 January 2010 02:24 Go to next message
Gerald Rosenberg is currently offline Gerald Rosenberg
Messages: 104
Registered: July 2009
Senior Member
Trying to figure out how to programmatically define and add parameters to a Command.

Is this the correct approach:

Command command = getCommandService().getCommand(commandId);
IParameter[] parameters = { new Parameter(....) } ; // << this is the problem
command.define(command.getName(), command.getDescription(),command.getCategory(), parameters);
ParameterizedCommand pcommand = ParameterizedCommand.generateCommand(command, null);
getHandlerService().executeCommand(pcommand, event);

If so, is there a helper for creating the "Parameter(...)" element? It is a bit more complex than
first seems.

Thanks...
Re: How to programmatically create ParameterizedCommand [message #510794 is a reply to message #510636] Thu, 28 January 2010 06:38 Go to previous messageGo to next message
Paul Webster is currently offline Paul Webster
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Gerald Rosenberg wrote:
> Trying to figure out how to programmatically define and add parameters to a Command.
>

There's no public definition of a Parameter, only
org.eclipse.ui.internal.commands.Parameter ... see
org.eclipse.ui.internal.commands.CommandPersistence.readComm andsFromRegistry(IConfigurationElement[],
int, ICommandService) and
org.eclipse.ui.internal.commands.CommandPersistence.readPara meters(IConfigurationElement,
List, ICommandService) for an example of how we define a Command with
IParameters in the workbench.

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/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/guide/workbench.htm


Re: How to programmatically create ParameterizedCommand [message #510806 is a reply to message #510794] Thu, 28 January 2010 07:16 Go to previous messageGo to next message
Gerald Rosenberg is currently offline Gerald Rosenberg
Messages: 104
Registered: July 2009
Senior Member
In article <hjsdut$gvk$1@build.eclipse.org>, pwebster@ca.ibm.com says...
>
> Gerald Rosenberg wrote:
> > Trying to figure out how to programmatically define and add parameters to a Command.
> >
>
> There's no public definition of a Parameter, only
> org.eclipse.ui.internal.commands.Parameter ... see
> org.eclipse.ui.internal.commands.CommandPersistence.readComm andsFromRegistry(IConfigurationElement[],
> int, ICommandService) and
> org.eclipse.ui.internal.commands.CommandPersistence.readPara meters(IConfigurationElement,
> List, ICommandService) for an example of how we define a Command with
> IParameters in the workbench.
>
> PW

Yes, I have been looking at those. Seems that Parameters can only be statically defined and valued
at design-time. Bummer.

So, maybe I should back up and ask the question this way. I am generating and executing commands
programmatically. While I could generate *alot* of discrete commands, I would prefer to just add
some run-time determined data to a chosen command just before execution. A key, value string pair
is sufficient. Is there some other command mechanism I could use?

Thanks...
Re: How to programmatically create ParameterizedCommand [message #510834 is a reply to message #510806] Thu, 28 January 2010 13:42 Go to previous messageGo to next message
Gerald Rosenberg is currently offline Gerald Rosenberg
Messages: 104
Registered: July 2009
Senior Member
In article <MPG.25cb63924e994b939896d3@news.eclipse.org>, gerald@certiv.net says...
>
> In article <hjsdut$gvk$1@build.eclipse.org>, pwebster@ca.ibm.com says...
> >
> > Gerald Rosenberg wrote:
> > > Trying to figure out how to programmatically define and add parameters to a Command.
> > >
> >
> > There's no public definition of a Parameter, only
> > org.eclipse.ui.internal.commands.Parameter ... see
> > org.eclipse.ui.internal.commands.CommandPersistence.readComm andsFromRegistry(IConfigurationElement[],
> > int, ICommandService) and
> > org.eclipse.ui.internal.commands.CommandPersistence.readPara meters(IConfigurationElement,
> > List, ICommandService) for an example of how we define a Command with
> > IParameters in the workbench.
> >
> > PW
>
> Yes, I have been looking at those. Seems that Parameters can only be statically defined and valued
> at design-time. Bummer.
>
> So, maybe I should back up and ask the question this way. I am generating and executing commands
> programmatically. While I could generate *alot* of discrete commands, I would prefer to just add
> some run-time determined data to a chosen command just before execution. A key, value string pair
> is sufficient. Is there some other command mechanism I could use?
>
> Thanks...

Looks like I can package the parameters in a subclass of the command's event parameter (and
retrieve using ExecutionEvent#getTrigger). Unless there is a better alternative?

Thanks...
Re: How to programmatically create ParameterizedCommand [message #510835 is a reply to message #510806] Thu, 28 January 2010 13:52 Go to previous message
Paul Webster is currently offline Paul Webster
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Gerald Rosenberg wrote:
>
> Yes, I have been looking at those. Seems that Parameters can only be statically defined and valued
> at design-time. Bummer.
>
> So, maybe I should back up and ask the question this way. I am generating and executing commands
> programmatically. While I could generate *alot* of discrete commands, I would prefer to just add
> some run-time determined data to a chosen command just before execution. A key, value string pair
> is sufficient. Is there some other command mechanism I could use?

ah, OK. Yes, the parameterization of commands is very much a static
thing. It's designed to accommodate things like keybindings and menu
items, that are very static once instantiated.

If you are executing your commands programmatically, there are a couple
of options.

You can take any defined command and execute it through the
org.eclipse.ui.handlers.IHandlerService. You can also modify the
application context (which is an IEvaluationContext) before executing
the command.

example (add appropriate check before casting):

ExecutionEvent event = handlerService.createExecutionEvent(cmd, null);
IEvaluationContext c
= (IEvaluationContext) event.getApplicationContext();
c.addVariable("my.data", new MyObject());
cmd.executeWithChecks(event);

You could do a similar thing by creating and contributing an
AbstractSourceProvider (extension point org.eclipse.ui.services). It's
a lot more work, but it would make the variables available for when any
event executed your command ... that might not be necessary in your case.

Later,
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/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/guide/workbench.htm


Previous Topic:[Team] How to add files in the commit dialog
Next Topic:Is it possible to create Patch file with Eclipse Team API?
Goto Forum:
  


Current Time: Fri Jul 25 06:47:26 EDT 2014

Powered by FUDForum. Page generated in 0.02300 seconds