Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » E4 - Bind handler to regular SWT button (considering canExecute)
E4 - Bind handler to regular SWT button (considering canExecute) [message #1005491] Fri, 25 January 2013 16:39 Go to next message
Kirsten M. Z. is currently offline Kirsten M. Z.Friend
Messages: 132
Registered: July 2010
Senior Member
Hi there,

I am just wondering, if it is possible to bind a handler to a custom (SWT) button. I have a view part with some "old fashioned" controls including Buttons. If a button is clicked, I want to execute a command. There is no problem to call the command programmatically using EHandlerService und selection events. However, this is no "real" binding. In this Special case, the "canExecute" mechanism is not bound, i.e. if the handeler is disabled, I also want my button to be disabled.

Re: E4 - Bind handler to regular SWT button (considering canExecute) [message #1005686 is a reply to message #1005491] Mon, 28 January 2013 09:21 Go to previous messageGo to next message
Sopot Cela is currently offline Sopot CelaFriend
Messages: 597
Registered: December 2010
Senior Member

No, it's not possible. A button in a SWT part is to granular to define in the application model and do proper binding. You can code it yourself though.
Re: E4 - Bind handler to regular SWT button (considering canExecute) [message #1015673 is a reply to message #1005686] Fri, 01 March 2013 16:20 Go to previous message
Kirsten M. Z. is currently offline Kirsten M. Z.Friend
Messages: 132
Registered: July 2010
Senior Member
I tried doing this. I am even using AspectJ in order to get a generic solution without much coding effort. Using AspectJ I can have a wrapper around @canExecute methods. Each time the @canExecute is evaluated, all according "CommandButtons" update their "enabled" state.

	// all methods with "CanExecute" annotation
	pointcut canExecuteCut(Object handlerObj) :
		execution(@org.eclipse.e4.core.di.annotations.CanExecute public boolean *.*(..)) && target(handlerObj);

	boolean around(Object handlerObj) : canExecuteCut(handlerObj) {
		final boolean enabled = proceed(handlerObj);
		for (final CommandButton btn : mapCommandButtons.get(getHandler(
			if (!btn.isDisposed())
		return enabled;

(mapCommandButtons is a static map which connects "command ids" with "command Buttons")

However, I still face a major Problem. I don't know exactly when @canExecute is re-evaluated or how this re-evaluation is triggered. When I used a toolbar the mechanism worked nicely, because canExecute is triggered all 400 ms via timer (cp.

The problems:

1. If I create such a timer on my own, I still have no idea which is the correct way to force the re-evaluation of @canExecute methods.
Direct way/possible workaround: I can use setEnabled for all "command buttons" in a loop, but then I have to inject on my own. How can this be done? (I found some snippets, but this is pretty ugly, and I never have an idea how this is done correctly).

2. I hate the idea of having a timer!?! I am using data binding everywhere in my project (also the reason why I want to bind a command/handler to a button), so why is there no clean solution for e4/DI?

This is the @CanExecute method of my handler:

public boolean canExecute(SomeData data)

"data" can be injected, because it is declared as @Singleton. My hope (!!!!) now was that the @canExecute is re-evaluated on each property Change within "data". BTW SomeData implements PropertyChangeFiring, which is a Basic requirement for JFace data binding. And a quote:

The framework calls this
method whenever the values defined in the parameters of the method change,
in our case the new command is only enabled if there's a IMailSession

Some further material (I cannot find it any more) stated that @CanExecute methods are only triggered if some parts of the context changes (IEclipseContext). Could not get this working either. People talked about some RAT (run and track) stuff. But I (again) have no idea what that is or more precisely how it can be used in my case. I wished I could use e4 without digging into each topic!

Very (!!!) often I come to the conclusion that e4 and DI magic only works if you are an absolute domain expert Sad
Previous Topic:Control over close button on Part tab
Next Topic:JRE is not being bundled
Goto Forum:

Current Time: Sun Jun 24 05:41:35 GMT 2018

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

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

Back to the top