Search code examples
eclipse-plugineclipse-rcpe4

Using e4 dependency injection to listen to part visibility changes


Using Eclipse 4.x, how can I listen to changes to the visibility of parts using dependency injection (or the event broker)?

For activation of parts the following is extremely elegant:

@Inject @Optional
void partActivated(@Active MPart part)

Its lower-level alternative using events is less elegant but still rather concise:

@Inject @Optional
void onActivation(@EventTopic(UIEvents.UILifeCycle.ACTIVATE) Event event)

But while there exists also a UIEvents.UILifeCycle.BRINGTOTOP topic, topics for visibility changes (analogous to IPartLisenter.partVisible and IPartLisenter.partHidden) are apparently missing. (UIEvents.UIElement.TOPIC_VISIBLE is a red herring here; just because a MUIElement.isVisible doesn’t mean it’s visible on screen.)

So, is it possible to use e4’s dependency injection magic to listen to visibility changes (for parts, at least)?

Note: I know about the low-level EPartService.addPartListener(IPartListener), but would rather avoid it, as it feels cumbersome and AFAICT would need to be called for every window.


Solution

  • If you look at the implementation of EPartService (org.eclipse.e4.ui.internal.workbench.PartServiceImpl) the part visible events are fired by subscribing to these events:

    UIEvents.ElementContainer.TOPIC_SELECTEDELEMENT
    UIEvents.ApplicationElement.TOPIC_TAGS
    

    The part toBeRendered flag and the minimized tag setting are then checked.

    Given that this is quite complex it seems better to use EPartService.addPartListener.