Eclipse RCP, when is a Handler created?

481 views Asked by At

we have a RCP 3/4 hybrid application and a handler defined in fragment.e4xmi.

<fragments xsi:type="fragment:StringModelFragment" xmi:id="_X7-AID93EeKXHI5xGhqnKg" featurename="handlers" parentElementId="org.eclipse.[..].application">
<elements xsi:type="commands:Handler" [..]/>
<elements xsi:type="commands:Handler" xmi:id="_jx8voD93EeKXHI5xGhqnKg" elementId="[..].ui.handler.addToDatabase" contributionURI="bundleclass://[..].ui/[..].ui.handlers.AddToDatabaseHandler" command="_TlV-ID93EeKXHI5xGhqnKg"/>
<elements xsi:type="commands:Handler" [..]/>

public class AddToDatabaseHandler implements EventHandler {

    private ISelectionMSD selection;

    public AddToDatabaseHandler() {
        System.err.println("Created " + this);
    }

    @Execute
    public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart part) {

        System.err.println(this + ": Execute, selection is " + selection);

        if(selection != null) {
            // do something
        }
    }

    @Override
    public void handleEvent(Event event) {

        if(event.getTopic().equals(UPDATE_SELECTION)) {
            selection = (ISelectionMSD)event.getProperty(IEvents.PROPERTY_SELECTION);
            System.err.println(this + ": Handle event, selection is " + selection);
        }
    }
}

I added some System.err and I can see that two instances of this Handler are created implicitly via DI/ reflection.

1) Is there an easy explanation when an instance of this handler is created?

2) the firstly created instance (right at the platform startup) misses the events for which this class is listening. So it seems not to be added to the EventAdmin as an event listener. Can I debug this somehow?

3) Its seems like "magic" to me, when and where and how is the second class registered to listen for events? Is this done somewhere by the platform or do I just miss some of our own code?

Many thanks for helping out!

1

There are 1 answers

3
greg-449 On BEST ANSWER

I'm not sure what you mean by an 3/4 hybrid app.

An e4 handler only executes the @Execute (and @CanExecute) method, it is not expected to implement EventHandler and nothing will look for this or call the method.

If you want to handle events use @EventTopic or @UIEventTopic

@Inject
@Optional
public void handleEvent(@EventTopic("topic id") Event event)