Tool items not receiving selection changes [message #1729889] |
Wed, 20 April 2016 01:28  |
Eclipse User |
|
|
|
I am porting an e4 SWT application to JavaFX on e(fx)clipse 2.3.0. It's mostly working now, except my tool item handlers are not receiving selection changes.
I have a part with a tree view that publishes selections, which should be triggering the canExecute of the several tool item handlers for my trim bar toolbar.
On a selected item change in the tree view, I update the selection service and post an event on the broker:
selectionService.setSelection(selectedItem);
eventBroker.post(UIEvents.REQUEST_ENABLEMENT_UPDATE_TOPIC, UIEvents.ALL_ELEMENT_ID);
My various handlers have a method that to receive the selection and a @CanExecute method, for example:
@Inject
public void setSelectedItem(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) MyItem item) {
...
}
@CanExecute
public boolean canExecute() {
return ...
}
This was all working fine in the SWT version of the app, but it's not working in the e(fx)clipse app. I have put breakpoints in, and found that the setSelectedItem() method only gets called when the handler is being created during startup, and never thereafter. The canExecute() method is getting invoked when the enablement event is posted, but always returns false because it has never received a selection.
Is there something different about the way handlers receive selections in e(fx)clipse?
|
|
|
Re: Tool items not receiving selection changes [message #1729923 is a reply to message #1729889] |
Wed, 20 April 2016 05:37   |
Eclipse User |
|
|
|
Never ever use @Inject in an handler this is discouraged and only works
by chance!
You should write:
-----8<-----
@CanExecute
public boolean canExecute(@Optional
@Named(IServiceConstants.ACTIVE_SELECTION) MyItem item) {
return item != null;
}
-----8<-----
Tom
On 20.04.16 07:28, Colin Sharples wrote:
> I am porting an e4 SWT application to JavaFX on e(fx)clipse 2.3.0. It's
> mostly working now, except my tool item handlers are not receiving
> selection changes.
>
> I have a part with a tree view that publishes selections, which should
> be triggering the canExecute of the several tool item handlers for my
> trim bar toolbar.
>
> On a selected item change in the tree view, I update the selection
> service and post an event on the broker:
>
> selectionService.setSelection(selectedItem);
> eventBroker.post(UIEvents.REQUEST_ENABLEMENT_UPDATE_TOPIC,
> UIEvents.ALL_ELEMENT_ID);
>
> My various handlers have a method that to receive the selection and a
> @CanExecute method, for example:
>
> @Inject
> public void setSelectedItem(@Optional
> @Named(IServiceConstants.ACTIVE_SELECTION) MyItem item) {
> ...
> }
>
> @CanExecute
> public boolean canExecute() {
> return ...
> }
>
>
> This was all working fine in the SWT version of the app, but it's not
> working in the e(fx)clipse app. I have put breakpoints in, and found
> that the setSelectedItem() method only gets called when the handler is
> being created during startup, and never thereafter. The canExecute()
> method is getting invoked when the enablement event is posted, but
> always returns false because it has never received a selection.
>
> Is there something different about the way handlers receive selections
> in e(fx)clipse?
|
|
|
|
|
|
|
|
|
Re: Tool items not receiving selection changes [message #1730276 is a reply to message #1730269] |
Sat, 23 April 2016 02:19   |
Eclipse User |
|
|
|
I had tried following Lars Vogel's tutorial [1], but couldn't get it to work. I could see the constructor of the function class being hit, but the compute method was never called, so I gave up on that and just created the controllers in an addon. Given what you said I gave the context function another go, and this time got it working. I had a couple of problems. I didn't have quite the correct component definition in the OSGI/services file. The tutorial is a bit confusing as it does configuration in the services file and with a @Component annotation as well, but it only works for me if I use the OSGI/services file.
The other issue was that I had defined my handlers at the application level, not the window, so the context function was not getting called for those because they were looking in the application context. I moved the handlers to the window level, and now they all pick up the correct controller instance depending on which window they are defined in. That's exactly what I want, as I can now reused the parts and handlers between windows. Cool. Thanks for your help, Tom.
[1] http://www.vogella.com/tutorials/Eclipse4ContextFunctions/article.html
|
|
|
Re: Tool items not receiving selection changes [message #1730278 is a reply to message #1730276] |
Sat, 23 April 2016 03:25  |
Eclipse User |
|
|
|
For completeness, here is the code I came up with if other people are interested:
Component definition, in file plugin/OSGI/services/controller.xml
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0"
name="nz.co.ctg.mote.editor.controller">
<implementation class="nz.co.ctg.mote.editor.ControllerFunction" />
<service>
<provide interface="org.eclipse.e4.core.contexts.IContextFunction"/>
</service>
<property name="service.context.key" type="String" value="nz.co.ctg.mote.editor.IEditorController"/>
</scr:component>
Added line to MANIFEST.MF:
Service-Component: OSGI/services/controller.xml
Context function class:
public class ControllerFunction extends ContextFunction {
@Override
public Object compute(IEclipseContext context, String contextKey) {
MWindow window = context.get(MWindow.class);
IEclipseContext windowCtx = window.getContext();
IEditorController controller = ContextInjectionFactory.make(EditorController.class, context);
windowCtx.set(IEditorController.class, controller);
return controller;
}
}
The important point is that the controller is injected into the window context rather than application context, so any class that has a dependency on IEditorController will get the instance belonging to the same window.
|
|
|
Powered by
FUDForum. Page generated in 0.09605 seconds