IServiceConstants.ACTIVE_SELECTION gotcha [message #1758899] |
Tue, 04 April 2017 11:12 |
Jay Norwood Messages: 155 Registered: July 2009 |
Senior Member |
|
|
I saw an example (vogella) suggesting use of injected IServiceConstants.ACTIVE_SELECTION to decouple monitoring of selection changed events ... the code example below. I used this in two MParts for signalling selectionchanged events, with the potential benefits of it being isolated locally per Window, without having to specify the specific viewer.
@Inject
public void setTodo(@Optional
@Named(IServiceConstants.ACTIVE_SELECTION)
However, I discovered during debug that just alternating selection of the two MPart menu bars would fire off events to the listener above, even though the treeviewer's selection hadn't changed!
So, for my case, where I really need to know that the tree selection has changed, I can't use this method without having to figure out how to screen for this extra selectionChanged event.
Anyone solved this kind of problem previously? I fell back to using IEventBroker events as a workaround, as demo'd in another piece of his examples below, but that gives up the IEclipseContext isolation.
@Inject
@Optional
private void subscribeTopicTodoUpdated
(@UIEventTopic(MyEventConstants.TOPIC_TODO_UPDATE)
Todo todo) {
|
|
|
Re: IServiceConstants.ACTIVE_SELECTION gotcha [message #1759426 is a reply to message #1758899] |
Tue, 11 April 2017 07:38 |
Eclipse User |
|
|
|
Realize that any parts that provide a selection will trigger your `ACTIVE_SELECTION` listener. The active selection is an example of a context variable in the IEclipseContext hierarchy. It's a named value that is hosted at some point in the context hierarchy. To change a variable, you use `IEclipseContext#modify("name", value)` instead of `#set("name", value)`: `#modify()` will climb the hierarchy to find the variable and then sets that value there.
You can define a context variable either by hand via `IEclipseContext#declareModifiable()` on the appropriate context (e.g., the window context), or
by declaring in your E4 model either using the model editors (Supplementary > Variables) or programmatically via `MContext#getVariables()`.
Checkout the wiki page on contexts:
https://wiki.eclipse.org/Eclipse4/RCP/Contexts
Brian.
|
|
|
Powered by
FUDForum. Page generated in 0.02396 seconds