Class EventGenerator

  • Direct Known Subclasses:
    Buttons, Command, DesktopEventGenerator, GenericEventGenerator, States

    public abstract class EventGenerator
    extends Object
    Event generators generate int-based events (see Event). They are responsible for holding data that cannot be sent within the event. Event generators are state machines that convert serialized (mostly external) integers to MicroUI events.

    There is a system pool that holds generators. A generator in the system pool has an identifier between 0 and 254, otherwise identifier is 0xFF. The advantage of putting a generator in the system pool is that it can then be looked-up using get(int) and get(Class, int).

    See Also:
    Event
    • Constructor Detail

      • EventGenerator

        public EventGenerator()
        Creates a new event generator. As soon as a new event generator is added to the system pool it has a valid identifier.
    • Method Detail

      • addToSystemPool

        public int addToSystemPool()
        Adds the generator in the system pool. The generator can only be added once as a system generator. When added, its identifier is set.
        Returns:
        the event generator's identifier in the system pool.
        Throws:
        MicroUIException - if the maximum number of event generators added to the system pool has been reached.
      • removeFromSystemPool

        public void removeFromSystemPool()
        Removes the generator from the system generators pool. If the generator is not in the pool, nothing is done.
      • setEventHandler

        public void setEventHandler​(@Nullable
                                    EventHandler eventHandler)
        Sets an event handler to this event generator. It will receive the generated events.

        It replaces the old one and can be null.

        Parameters:
        eventHandler - the event handler to add.
        Throws:
        SecurityException - if a security manager exists and does not allow the caller to handle events from this event generator.
        Since:
        2.0
      • get

        public static EventGenerator get​(int id)
        Gets a generator from its identifier.
        Parameters:
        id - the generator's identifier.
        Returns:
        the associated event generator.
        Throws:
        IndexOutOfBoundsException - if the generator does not exist.
        MicroUIException - if MicroUI is not started
      • get

        @Nullable
        public static <E extends EventGenerator> E get​(Class<E> clazz,
                                                       int fromIndex)
        Gets a generator whose class is clazz from the system pool starting the search from fromIndex. If class clazz is not an EventGenerator or if nothing is found, returns null.
        Parameters:
        clazz - the EventGenerator's class to return
        fromIndex - index from which starting to search
        Returns:
        the EventGenerator or null.
        Throws:
        MicroUIException - if MicroUI is not started
      • get

        public static <E extends EventGeneratorEventGenerator[] get​(Class<E> clazz)
        Gets all generators of the given type from the system pool in an array.
        Parameters:
        clazz - the type of the event generators to return.
        Returns:
        the arrays of event generators.
        Throws:
        MicroUIException - if MicroUI is not started
      • getId

        public final int getId()
        Gets the generator's unique identifier. Up to 254 EventGenerators can be installed as MicroUI system event generator.
        Returns:
        the generator's identifier as an int.
      • getEventHandler

        @Nullable
        public EventHandler getEventHandler()
        Gets the generator's event handler.
        Returns:
        the generator's event handler.
        Since:
        2.0
      • getEventType

        public abstract int getEventType()
        Gets the event type associated with the event generator
        Returns:
        the event type
      • sendEvent

        protected void sendEvent​(int event)
        Sends the given event to the current EventHandler. Does nothing if there is no event handler set to this event generator.
        Parameters:
        event - the event to send