|
|
Re: E4 - Bind handler to regular SWT button (considering canExecute) [message #1015673 is a reply to message #1005686] |
Fri, 01 March 2013 16:20 |
Kirsten M. Z. 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(
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
|
|
|
Powered by
FUDForum. Page generated in 0.02056 seconds