|Execute a command programmatically, matching with a given selection [SOLVED] [message #493183]
||Fri, 23 October 2009 14:20
| Jacques LESCOT
Registered: July 2009
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 :
<test property="org.eclipse.core.resources.projectNature" value="com.anwrt.platform.OperatingPortalNature"/>
<test property="org.eclipse.core.resources.name" value="*.properties"/>
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
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
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 ?
[Updated on: Mon, 26 October 2009 15:30]
Report message to a moderator