Class DefaultEventService

    • Field Detail

      • PRIORITY

        public static final double PRIORITY
        The default event service's priority.

        Alternative event service implementations that wish to prioritize themselves above this one can still ensure preferential usage via priority = DefaultEventService.PRIORITY + 1 or similar.

        See Also:
        Constant Field Values
    • Constructor Detail

      • DefaultEventService

        public DefaultEventService()
    • Method Detail

      • subscribe

        public List<EventSubscriber<?>> subscribe​(Object o)
        Description copied from interface: EventService
        Subscribes all of the given object's @EventHandler annotated methods.

        This allows a single class to subscribe to multiple types of events by implementing multiple event handling methods and annotating each one with the EventHandler annotation.

        Note that it is not necessary to store a copy of the event subscribers (because the event service is expected to hold a weak mapping between the event handler object and the subscribers) unless the subscribers need to be unsubscribed explicitly.

        Most users will want to extend AbstractContextual, or call Context.inject(Object), instead of subscribing to the event service explicitly.

        Specified by:
        subscribe in interface EventService
        Parameters:
        o - the event handler object containing the EventHandler annotated methods
        Returns:
        The list of newly created EventSubscribers, weakly subscribed to the event service.
        See Also:
        AbstractContextual, Context.inject(Object)
      • subscribe

        public void subscribe​(EventSubscriber<?> subscriber)
        Description copied from interface: EventService
        Subscribes the given EventSubscriber to its associated event class. Its EventSubscriber.onEvent(E) method will be called whenever an event of the matching type is published.

        Important note: The event service does not keep a strong reference to the subscriber! If you use this method, you are also responsible for keeping a reference to the subscriber, or else it is likely to be garbage collected, and thus no longer respond to events as intended. One simple way to force a strong reference to exist is to add it to SciJava's ObjectService via ObjectService.addObject(java.lang.Object).

        Specified by:
        subscribe in interface EventService
        Parameters:
        subscriber - the event subscriber to register
      • initialize

        public void initialize()
        Description copied from interface: Service
        Performs any needed initialization when the service is first loaded.

        NB: This method is not intended to be called directly. It is called by the service framework itself (specifically by the ServiceHelper) when initializing the service. It should not be called a second time.

        Specified by:
        initialize in interface Initializable
        Specified by:
        initialize in interface Service
      • dispose

        public void dispose()
        Description copied from interface: Disposable
        Performs any needed cleanup of the object's services, in preparation for the object being retired (e.g., to make garbage collection possible).
        Specified by:
        dispose in interface Disposable