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 11:39 Go to next message
Kirsten M. Z. is currently offline Kirsten M. Z.
Messages: 131
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.

Thanks!
Re: E4 - Bind handler to regular SWT button (considering canExecute) [message #1005686 is a reply to message #1005491] Mon, 28 January 2013 04:21 Go to previous messageGo to next message
Sopot Cela is currently offline Sopot Cela
Messages: 596
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 11:20 Go to previous message
Kirsten M. Z. is currently offline Kirsten M. Z.
Messages: 131
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(
				handlerObj).getCommand().getElementId()))
			if (!btn.isDisposed())
				btn.setEnabled(enabled);
		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. http://www.vogella.com/articles/EclipseRCP/article.html).

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:

@CanExecute
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:

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
available.


http://www.google.com/url?sa=t&rct=j&q=canexecute%20e4%20tutorial&source=web&cd=7&cad=rja&ved=0CHoQFjAG&url=http%3A%2F%2Fwww.bestsolution.at%2Fen%2Fdownload.php%3Fcat%3D01_Services%26file%3DEclipse4Tutorial_WritingApplicationWithE4_BestSolution.pdf&ei=p9EwUYLeCOKC4gSlmYA4&usg=AFQjCNF6JGXn4hPJgogNDtmo8ijmUgrysA&bvm=bv.43148975,d.bGE

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: Thu Jul 24 23:46:00 EDT 2014

Powered by FUDForum. Page generated in 0.08349 seconds