1 /* 2 Copyright aswing.org, see the LICENCE.txt. 3 */ 4 5 import org.aswing.Event; 6 import org.aswing.IEventDispatcher; 7 import org.aswing.utils.ArrayUtils; 8 import org.aswing.utils.Delegate; 9 import org.aswing.utils.StringUtils; 10 11 /** 12 * 13 * @author iiley 14 */ 15 class org.aswing.EventDispatcher implements IEventDispatcher{ 16 17 /** 18 * When the action performed 19 *<br> 20 * onActionPerformed Event{target:EventDispatcher} 21 */ 22 public static var ON_ACT:String = "onActionPerformed"; 23 24 private var listeners:Array; 25 26 public function EventDispatcher(){ 27 listeners = new Array(); 28 } 29 30 /** 31 * addEventListener(listener:Object)<br> 32 * addEventListener(eventName:String, func:Function, obj:Object)<br> 33 * addEventListener(eventName:String, func:Function)<br> 34 * Add a event listener, if you add event listener by the first way, add the 35 * listener of the parameter and return it. you can remove the listener later. 36 * But if you added by the last two way, the returned value is a new object of 37 * listener, so when you want to remove it, you should use the returned value. 38 * @param eventTypeOrLis it can be listener instance have the listener method, 39 * and can be a event type to indicate what type of event it want to handle. 40 * @param func if this param is not empty(undefined), the first param listener must be 41 * a String of event name. 42 * @param func the function which want to handler the event indicated by event name. 43 * @param obj Context in which to run the function of param func. 44 * @return the listener just be added. If you add event listener by event name, the return is a 45 * new object of listener, if you add event listener by first way(a listener object), the return 46 * is IT(the first parameter). 47 * @see #removeEventListener() 48 */ 49 public function addEventListener(eventTypeOrLis:Object, func:Function, obj:Object):Object{ 50 if(func != undefined){ 51 var listenerObj:Object = new Object(); 52 var eventName:String = StringUtils.castString(eventTypeOrLis); 53 if(obj != undefined){ 54 listenerObj[eventName] = Delegate.create(obj, func); 55 }else{ 56 listenerObj[eventName] = func; 57 } 58 listeners.push(listenerObj); 59 return listenerObj; 60 }else{ 61 listeners.push(eventTypeOrLis); 62 return eventTypeOrLis; 63 } 64 } 65 66 /** 67 * Remove the specified event listener. 68 * @param listener the listener which will be removed. 69 */ 70 public function removeEventListener(listener:Object):Void{ 71 ArrayUtils.removeFromArray(listeners, listener); 72 } 73 74 /** 75 * dispatchEvent(name:String, event:Event)<br> 76 * dispatchEvent(event:Event) default the the listener's method name to event's type name. 77 * <p> 78 * @param name the listener's method name 79 * @param event the event object, event.target = the component object 80 */ 81 public function dispatchEvent(name:Object, event:Event):Void{ 82 var funcName:String; 83 if(event == undefined){ 84 event = Event(name); 85 funcName = event.getType(); 86 }else{ 87 funcName = String(name); 88 } 89 if(listeners.length > 0){ 90 for(var i:Number=0; i<listeners.length; i++){ 91 listeners[i][funcName](event); 92 } 93 } 94 } 95 96 private function createEventObj(type:String):Event{ 97 var event:Event = new Event(this,type); 98 return event; 99 } 100 101 } 102