Execute a command programmatically, matching with a given selection [SOLVED] [message #493183] |
Fri, 23 October 2009 14:20 |
Jacques LESCOT Messages: 247 Registered: July 2009 |
Senior Member |
|
|
I am having some troubles while trying to execute a command programmatically, when a "count" condition is specified. Let me explain the problem :
I have defined a handler with the following enablement condition :
<enabledWhen>
<and>
[B]<count value="1"/>[/B]
<iterate operator="and">
<test property="org.eclipse.core.resources.projectNature" value="com.anwrt.platform.OperatingPortalNature"/>
<test property="org.eclipse.core.resources.name" value="*.properties"/>
</iterate>
</and>
</enabledWhen>
So my handler is active only when a single *.properties file is selected in the project explorer and that it is contained in a project with a specific nature. Great !
Now, I would like to provide a new way to execute that handler. In a FormEditor, when I am editing the *.properties file, I would like to let the user execute the handler when he clicks on a button for example. So I write the following code when the button is pressed :
// Retrieve the corresponding Services
IHandlerService handlerService = (IHandlerService) getSite().getService(IHandlerService.class);
ICommandService commandService = (ICommandService) getSite().getService(ICommandService.class);
// Retrieve the command
Command generateCmd = commandService.getCommand("my_command_id");
// Create an ExecutionEvent and specify the IFile associated
ExecutionEvent executionEvent = handlerService.createExecutionEvent(generateCmd, new Event());
((IEvaluationContext) executionEvent.getApplicationContext()).addVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME, new StructuredSelection(getInputFile()));
// Launch the command
generateCmd.executeWithChecks(executionEvent);
The problem is that when I try to execute the command, I get the following Exception :
org.eclipse.core.commands.NotEnabledException: Trying to execute the disabled command my_command_id
at org.eclipse.core.commands.Command.executeWithChecks(Command.java:469)
...
While trying to debug the problem, I noticed that in the CountExpression.java class, the call to the method public EvaluationResult evaluate(IEvaluationContext context) throws CoreException seems to indicate that my IEvaluationContext is not correct : the getDefaultVariable() returns an empty Collection ...
So I removed the <count value="1"/> condition from the handler definition, and then it works programmatically, but I can then select two files from the project explorer.
So is there something I missed to specify programmatically that condition or should I proceed in a complete different way ?
Regards,
Jacques
[Updated on: Mon, 26 October 2009 15:30] Report message to a moderator
|
|
|
|
Re: Execute a command programmatically, matching with a given selection [SOLVED] [message #493451 is a reply to message #493230] |
Mon, 26 October 2009 10:53 |
Jacques LESCOT Messages: 247 Registered: July 2009 |
Senior Member |
|
|
Hi Prakash,
Thanks for your response.
Unfortunately, that did not solve my problem : I still have the same Exception thrown.
However, your response helps me to explore deeply what was going wrong with the selection, and I figure out that there was a problem with the editor selection : it was empty. So, I just explicitly set the selection of my editor, and everything went well (it was not necessary anymore to add any variable to the ApplicationContext). Here is my final code :
// Retrieve the corresponding Services
IHandlerService handlerService = (IHandlerService) getSite().getService(IHandlerService.class);
ICommandService commandService = (ICommandService) getSite().getService(ICommandService.class);
// Retrieve the command
Command generateCmd = commandService.getCommand("my_command_id");
// Explicitly specify a selection when activating the button
getSite().getSelectionProvider().setSelection(new StructuredSelection(getInputFile()));
// Create an ExecutionEvent
ExecutionEvent executionEvent = handlerService.createExecutionEvent(generateCmd, new Event());
// Launch the command
generateCmd.executeWithChecks(executionEvent);
Regards,
Jacques
[Updated on: Mon, 26 October 2009 11:00] Report message to a moderator
|
|
|
Re: Execute a command programmatically, matching with a given selection [SOLVED] [message #493656 is a reply to message #493451] |
Tue, 27 October 2009 13:46 |
|
Right, the long and the short of it is that adding to the IEvaluationContext is a good way of passing information onto your handler during execute, and the pattern you used is OK (you can just pass null in for the SWT event when creating the ExecutionEvent).
But enabledWhen, activeWhen, etc, are controlled through the variables provided to the IEvaluationService, and it cannot see the changes you make to your local IEvaluationContext.
by setting your selection, you've posted an event that the IEvaluationService can see, and so it has updated your enabledWhen appropriately.
PW
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Menu_Contributions
|
|
|
|
Re: Execute a command programmatically, matching with a given selection [SOLVED] [message #725466 is a reply to message #493183] |
Wed, 14 September 2011 20:58 |
|
Hi Jacques et al,
I'm trying to do something similar and could use your help. Like Jacques originally started out doing, I'm trying to programmatically call a command and pass it a selection rather than using HandlerService.getCurrentState(). Like Jacques I'm adding a variable to the new ExecutionEvent using ISources.ACTIVE_CURRENT_SELECTION_NAME (i.e. "selection") but my called handler then gets null from the defaultVariable. Is there a way to set the defaultVariable rather than "selection"?
// Create an ExecutionEvent
ExecutionEvent executionEvent = handlerService.createExecutionEvent(command, null);
// Give it the selected object
((IEvaluationContext) executionEvent.getApplicationContext()).addVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME, new StructuredSelection(selectedObject));
// Launch the command
command.executeWithChecks(executionEvent);
[Updated on: Wed, 14 September 2011 21:55] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.04115 seconds