Home » Eclipse Projects » e(fx)clipse » Tool items not receiving selection changes
Tool items not receiving selection changes [message #1729889] |
Wed, 20 April 2016 05:28 |
|
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?
Colin Sharples
CTG Games Ltd
Wellington, New Zealand
|
|
|
Re: Tool items not receiving selection changes [message #1729923 is a reply to message #1729889] |
Wed, 20 April 2016 09:37 |
Thomas Schindl Messages: 6651 Registered: July 2009 |
Senior Member |
|
|
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 #1729994 is a reply to message #1729923] |
Wed, 20 April 2016 18:01 |
|
Well, as I said, it was working fine in the SWT application. I changed the canExecute methods to inject the selection, but the item is always null, so it seems like the selection service is not getting the selection for some reason.
Colin Sharples
CTG Games Ltd
Wellington, New Zealand
|
|
| | |
Re: Tool items not receiving selection changes [message #1730265 is a reply to message #1730188] |
Fri, 22 April 2016 20:50 |
|
It was due to the problem I described on the other post - the controller was using the wrong selection service. I fixed it by putting two instances of the controller into the context, so that the parts in each window get the correct controller. I asked a question on the E4 forum about a better way to manage the controllers, but didn't get a response - perhaps you can help? Essentially what I want is for the controller to be created in the window context, rather than the application context. At the moment the parts have to look for a specific name, which now means that I can't reuse parts in both windows.
Colin Sharples
CTG Games Ltd
Wellington, New Zealand
|
|
| | |
Re: Tool items not receiving selection changes [message #1730276 is a reply to message #1730269] |
Sat, 23 April 2016 06:19 |
|
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
Colin Sharples
CTG Games Ltd
Wellington, New Zealand
|
|
|
Re: Tool items not receiving selection changes [message #1730278 is a reply to message #1730276] |
Sat, 23 April 2016 07:25 |
|
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.
Colin Sharples
CTG Games Ltd
Wellington, New Zealand
|
|
|
Goto Forum:
Current Time: Fri Sep 20 07:24:05 GMT 2024
Powered by FUDForum. Page generated in 0.06010 seconds
|