Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » How to determine the enabled state of a command programmatically?
How to determine the enabled state of a command programmatically? [message #1802036] Thu, 31 January 2019 08:11 Go to next message
Thomas Fahrmeyer is currently offline Thomas FahrmeyerFriend
Messages: 30
Registered: July 2009
Location: Germany
Member
Hi,

I'm trying to bind an UI control, a button, to the enabled state of a command. The reason is, that I want to have a contribution interface (a defined command) to allow external parties to provide logic that should be executed. The button shall execute this provided logic but have to be disabled if no contribution was provided. Therefore I'd like to bind it to the enabled state of a command.

I tried it with the big code snippet at the end.

It works, if I determine the enabled state via
eCommandService.getCommand(ZOOM_CMD_ID).isEnabled();


It provides the correct/expected state every time.

My question is what is the difference between
eCommandService.getCommand(ZOOM_CMD_ID).isEnabled();

and
eHandlerService.canExecute(getParametrizedCommand());


I'd expect that they are equal semantically. They deliver the same result if I dont leave the part and toggle the handler registration programmatically. BUT if the command is enabled and I switch to another part, the enabled state is different.

* cmd.isEnabled() is false as expected, because the handler was registered for the part
* canExecute() is still true, which is not correct in my opinion

So, to come to the end :) If someone could answer the following questions:

* What is the semantic of both approaches?
* In which use case I should use which version?

Thanks for your support
Thomas

// observes context changes. These will happen if a handler is registered for a command
part.getContext().runAndTrack(new RunAndTrack() {

	@Override
	public boolean changed(IEclipseContext context) {
		checkExecuteButtonEnablementLongVersion();
		return true;
	}	
});

private void checkExecuteButtonEnablementLongVersion() {
	// part is the MPart containing the control (button) that should be controled by 
	// the enablement of a command ZOOM_CMD_ID
	IEclipseContext partContext = part.getContext();
		
	EHandlerService eHandlerService = partContext.get(EHandlerService.class);
	ECommandService eCommandService = (ECommandService)partContext.get(ECommandService.class);
		
	// 
	boolean enabledViaCanExecute = eHandlerService.canExecute(getParametrizedCommand());
		
	boolean enabledViaIsEnabled = eCommandService.getCommand(ZOOM_CMD_ID).isEnabled();
		
	if (enabledViaCanExecute != enabledViaIsEnabled) {
		System.out.println(">>> enabled state is different!");
	} else {
		System.out.println("enabled state is the same!");
	}
		
	executeButton.setDisable(!enabledViaIsEnabled);
}

private ParameterizedCommand getParametrizedCommand() {
		ParameterizedCommand command = ParameterizedCommand.generateCommand(
				getCommandService().getCommand(ZOOM_CMD_ID), new HashMap());
		return command;
	}

Re: How to determine the enabled state of a command programmatically? [message #1802064 is a reply to message #1802036] Thu, 31 January 2019 14:56 Go to previous messageGo to next message
Brian de Alwis is currently offline Brian de AlwisFriend
Messages: 865
Registered: January 2012
Senior Member

Handlers can be installed on parts, windows, and applications, whereas the commands are global. The command service updates the command enablement states based on the active part's handler service.

Note that you are fetching your services from the part context, so that handler service is always resolving handlers from that part.


Eclipse Platform committer. Ask me about Eclipse support, training, and consulting.
Re: How to determine the enabled state of a command programmatically? [message #1802081 is a reply to message #1802064] Thu, 31 January 2019 22:22 Go to previous messageGo to next message
Thomas Fahrmeyer is currently offline Thomas FahrmeyerFriend
Messages: 30
Registered: July 2009
Location: Germany
Member
Brian de Alwis wrote on Thu, 31 January 2019 15:56
Handlers can be installed on parts, windows, and applications, whereas the commands are global. The command service updates the command enablement states based on the active part's handler service.


Ok, that is expected and wanted. In my understanding for my usecase the commandService approach is the right one. Only that gives me the command state the user would expect.

Brian de Alwis wrote on Thu, 31 January 2019 15:56

Note that you are fetching your services from the part context, so that handler service is always resolving handlers from that part.


Ok, got it :) The handlerservice instance still as the handler registered for the given command no matter whether is not active anymore from the global application point of view. If I would use the handlerservice from the perspective context, the state should be correct as well, isn't?

Thanks Brian for the explanation.
Thomas
Re: How to determine the enabled state of a command programmatically? [message #1802231 is a reply to message #1802081] Mon, 04 February 2019 18:38 Go to previous message
Brian de Alwis is currently offline Brian de AlwisFriend
Messages: 865
Registered: January 2012
Senior Member

Quote:
The handlerservice instance still as the handler registered for the given command no matter whether is not active anymore from the global application point of view. If I would use the handlerservice from the perspective context, the state should be correct as well, isn't?


Not quite: the handler service is a service that performs lookups. Each handler service starts a lookup from its associated context.

A handler service rooted at a perspective would start its lookups at the perspective. Which isn't what you want.

What you might want is to get the active part within the perspective and get that part's handler service.

Brian.


Eclipse Platform committer. Ask me about Eclipse support, training, and consulting.
Previous Topic:Problem with combination of restoring state + processing fragments with placeholders
Next Topic:Why the recent Eclipse "Language servers" features slow down so much the IDE
Goto Forum:
  


Current Time: Fri Dec 06 15:22:32 GMT 2019

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

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

Back to the top