0

In my Eclipse e4 RCP application, I'm using annotations to get notified when a Part is activated, using the approach proposed here : http://www.vogella.com/tutorials/Eclipse4ModelEvents/article.html

@Inject
@Optional
public void      subscribeTopicPartActivation(@UIEventTopic(UIEvents.UILifeCycle.ACTIVATE)  Event event) {

  Object element = event.getProperty(EventTags.ELEMENT);
  if (!(element instanceof MPart)) {
    return;
  }

  MPart part = (MPart) element;

  System.out.println("Part activated: " + part.getLabel());
} 

It works fine, but I've noticed that the activate event is triggered more than once for the same part in cases where we would expect a single activate event (i.e for exemple simple switch to the part...). The event message sent seems to be exactly the same (same target, same topic). Am I missing something ? Is it a normal behavior for the event framework ?

greg-449
  • 109,219
  • 232
  • 102
  • 145
FH35
  • 75
  • 11

1 Answers1

0

Yes, it appears to be normal. There is a difference in the 'tags' value of the part between the two events. In the second event 'active' has been added to the tags.

This will show the difference:

 System.out.println("part " + part.getElementId() + " tags " +
            part.getTags().stream().collect(Collectors.joining(", ")));

You can also use the EPartService addPartListener method to listen for just part changes.

greg-449
  • 109,219
  • 232
  • 102
  • 145
  • Thank you Greg. Sometimes we have 3 activate events (one with no tag as you mention, and two with the activate tag...). Which is strange anyway. – FH35 Dec 20 '15 at 15:02
  • As far as I can see the only place this event is generated is in `org.eclipse.e4.ui.internal.workbench.PartServiceImpl` (the implementation of `EPartService`) in the internal `activate` method. You would have to trace through the code to see exactly how this works. – greg-449 Dec 20 '15 at 18:21