Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Execute a command programmatically, matching with a given selection [SOLVED]
Execute a command programmatically, matching with a given selection [SOLVED] [message #493183] Fri, 23 October 2009 14:20 Go to previous message
Jacques LESCOT is currently offline Jacques LESCOTFriend
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

 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic:The Proxied Handler ' ...' could not be loaded
Next Topic:Share common features/plugins
Goto Forum:
  


Current Time: Mon Apr 22 22:28:29 GMT 2019

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

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

Back to the top