Changeset 3

Show
Ignore:
Timestamp:
06/17/05 16:00:06 (4 years ago)
Author:
schst
Message:

added global event listeners, that will be triggered, after the local listeners handled the event

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/net/schst/EventDispatcher/EventDispatcher.java

    r1 r3  
    1414public class EventDispatcher { 
    1515     
    16    /**     
     16   /** 
    1717    * Stores all listeners 
    1818    */ 
    1919    private HashMap listeners = new HashMap(); 
     20 
     21   /** 
     22    * Stores global listeners, that handle all events 
     23    */ 
     24    private EventListenerCollection globalListeners = new EventListenerCollection(); 
    2025     
    2126   /** 
     
    3742    */ 
    3843    public static EventDispatcher getInstance() { 
    39         return EventDispatcher.getInstance("default"); 
     44        return EventDispatcher.getInstance("__default"); 
    4045    } 
    4146     
     
    6368         this.addListener(eventName, listener, false); 
    6469     } 
    65      
     70 
    6671   /** 
    6772    * Add an event listener object 
     
    9095 
    9196    /** 
     97     * Add an event listener object 
     98     *  
     99     * @param eventName      name of the event to listen on 
     100     * @param listener       instance of the event listener 
     101     */ 
     102     public void addGlobalListener(EventListener listener) { 
     103         this.addGlobalListener(listener, false); 
     104     } 
     105 
     106    /** 
     107     * Add an event listener object 
     108     *  
     109     * @param eventName      name of the event to listen on 
     110     * @param listener       instance of the event listener 
     111     * @param autoRemove     whether to remove the listener after the first event it has handled 
     112     */ 
     113     public void addGlobalListener(EventListener listener, boolean autoRemove) { 
     114         // TODO implement autoRemove 
     115          
     116         this.globalListeners.addListener(listener); 
     117          
     118         // check the event queue 
     119         ArrayList events = this.queue.getQueuedEvents(); 
     120          
     121         for (Iterator iter = events.iterator(); iter.hasNext();) { 
     122             Event e = (Event)iter.next(); 
     123             this.propagate(e, false); 
     124         } 
     125     } 
     126     
     127    /** 
    92128     * Trigger an event, if you already created an event object 
    93129     *  
     
    100136         return this.propagate(e, false); 
    101137     } 
    102      
     138 
     139      
    103140   /** 
    104141    * Trigger an event, if you already created an event object 
     
    176213            col.propagate(e); 
    177214        } 
     215 
     216        if (e.isCancelled()) { 
     217            return e; 
     218        } 
     219         
     220        this.globalListeners.propagate(e); 
    178221 
    179222        if (e.isCancelled() || queue == false) { 
  • trunk/src/net/schst/EventDispatcher/Examples/DebugHandler.java

    r1 r3  
    99public class DebugHandler implements EventListener{ 
    1010 
     11    private String name = "No-Name"; 
     12 
     13    public DebugHandler() { 
     14    } 
     15     
     16    public DebugHandler(String name) { 
     17        this.name = name; 
     18    } 
     19     
    1120    /** 
    1221     * Handle the event 
    1322     */ 
    1423    public void handleEvent(Event e) { 
    15         System.out.println("Event has been triggered."); 
     24        System.out.println("Event caught by '" + this.name + "'"); 
    1625        System.out.println("Event-name    : " + e.getName()); 
    1726        System.out.println("Event-context : " + e.getContext());