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 next 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

Re: Execute a command programmatically, matching with a given selection [message #493230 is a reply to message #493183] Fri, 23 October 2009 17:53 Go to previous messageGo to next message
Prakash G.R. is currently offline Prakash G.R.Friend
Messages: 621
Registered: July 2009
Senior Member
Jacques LESCOT wrote:
> So is there something I missed to specify programmatically that
> condition or should I proceed in a complete different way ?

You are missing ISources.ACTIVE_EDITOR_INPUT_NAME :-) That variable is
available in your context and HandlerUtil should get it for you. If the
editor input is IFileEditorInput (which is the workspace file), you get
the IFile from that interface.


- Prakash

Platform UI Team, IBM
http://blog.eclipse-tips.com
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 Go to previous messageGo to next message
Jacques LESCOT is currently offline Jacques LESCOTFriend
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 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

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


Re: Execute a command programmatically, matching with a given selection [SOLVED] [message #493691 is a reply to message #493656] Tue, 27 October 2009 15:03 Go to previous messageGo to next message
Jacques LESCOT is currently offline Jacques LESCOTFriend
Messages: 247
Registered: July 2009
Senior Member
Paul, thanks for these clarifications.

Regards,
Jacques
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 Go to previous message
Craig Foote is currently offline Craig FooteFriend
Messages: 195
Registered: July 2009
Senior Member
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

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


Current Time: Mon Dec 22 03:00:36 GMT 2014

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

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