001package systems.dmx.core.service;
002
003import java.util.HashMap;
004import java.util.Map;
005
006
007
008/**
009 * Base class for all events.
010 * That is core events as well as plugin events.
011 */
012public abstract class DMXEvent {
013
014    // ------------------------------------------------------------------------------------------------- Class Variables
015
016    /**
017     * A map of all known events (contains core events as well as plugin events).
018     * Used to find the event that corresponds to a certain listener interface.
019     */
020    private static Map<String, DMXEvent> events = new HashMap();
021
022    // ---------------------------------------------------------------------------------------------- Instance Variables
023
024    /**
025     * The corresponding listener interface for this event.
026     */
027    private final Class listenerInterface;
028
029    // ---------------------------------------------------------------------------------------------------- Constructors
030
031    public DMXEvent(Class listenerInterface) {
032        this.listenerInterface = listenerInterface;
033        putEvent(this, listenerInterface);
034    }
035
036    // -------------------------------------------------------------------------------------------------- Public Methods
037
038    /**
039     * Delivers this event to the given listener.
040     */
041    public abstract void dispatch(EventListener listener, Object... params);
042
043    /**
044     * Returns the corresponding listener interface for this event.
045     */
046    public Class getListenerInterface() {
047        return listenerInterface;
048    }
049
050    /**
051     * Returns the event that corresponds to the given listener interface.
052     */
053    public static DMXEvent getEvent(Class listenerInterface) {
054        DMXEvent event = events.get(listenerInterface.getName());
055        //
056        if (event == null) {
057            throw new RuntimeException("The event object for listener " + listenerInterface + " is unknown");
058        }
059        //
060        return event;
061    }
062
063    // ------------------------------------------------------------------------------------------------- Private Methods
064
065    private void putEvent(DMXEvent event, Class listenerInterface) {
066        events.put(listenerInterface.getName(), event);
067    }
068}