Advertisement
Guest User

Untitled

a guest
May 9th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package max4as.event
  2. {
  3.     import max4as.event.Event;
  4.     import max4as.event.enum.EventType;
  5.  
  6.     /**
  7.      * @author Stéphane Robert Richard - kabnot@gmail.com
  8.      */
  9.     public interface IEventDispatcher {
  10.  
  11.  
  12.         function addEventListener (listenerReference : ListenerReference) : Boolean ;
  13.  
  14.         function removeEventListener (listenerReference : ListenerReference) : Boolean ;
  15.  
  16.         function dispatchEvent (event : Event) : void ;
  17.  
  18.         function hasEventListener (listener : Object, type : EventType) : Boolean ;
  19.     }
  20. }
  21.  
  22.  
  23. package max4as.event
  24. {
  25.     import max4as.error.NullPointerError;
  26.     import max4as.event.IEventDispatcher;
  27.     import max4as.event.ListenerReference;
  28.     import max4as.event.enum.EventType;
  29.  
  30.     /**
  31.      * @author Stéphane Robert Richard - kabnot@gmail.com
  32.      */
  33.     public class EventDispatcher implements IEventDispatcher {
  34.  
  35.        
  36.         private var _listeners : Array = [];
  37.  
  38.        
  39.         public function addEventListener (listenerReference : ListenerReference) : Boolean {
  40.             if (listenerReference == null) throw new NullPointerError( );
  41.             for each (var i : ListenerReference in _listeners) {
  42.                 if (i == listenerReference || i.equals( listenerReference )) return false ;
  43.             }
  44.             _listeners.push( listenerReference );
  45.             return true;
  46.         }
  47.  
  48.         public function removeEventListener (listenerReference : ListenerReference) : Boolean {
  49.             if (listenerReference == null) throw new NullPointerError( );
  50.             for (var i : Number = 0; i < _listeners.length ; i++) {
  51.                 var l : ListenerReference = _listeners[i];
  52.                 if (l == listenerReference) {
  53.                     _listeners.splice( i, 1 );
  54.                     return true;
  55.                 }
  56.             }
  57.             return false;
  58.         }
  59.  
  60.         public function dispatchEvent (event : Event) : void {
  61.             for each (var i : ListenerReference in _listeners) {
  62.                 //Hack for compatibility with the parser, but we should be able to write : i.handlingFunction() directly.
  63.                 var f : Function = i.handlingFunction;
  64.                 f( );
  65.             }
  66.         }
  67.  
  68.         public function hasEventListener (listener : Object, type : EventType) : Boolean {
  69.  
  70.             for (var i : Number = 0; i < _listeners.length ; i++) {
  71.                 var l : ListenerReference = _listeners[i] as ListenerReference;
  72.                 if (l.listener == listener && l.type == type) return true;
  73.             }
  74.             return false;
  75.         }
  76.     }
  77. }
  78.  
  79.  
  80. package max4as.event
  81. {
  82.     import max4as.event.enum.EventType;
  83.  
  84.     /**
  85.      * @author Stéphane Robert Richard - kabnot@gmail.com
  86.      */
  87.     public class Event {
  88.  
  89.        
  90.         private var _type : EventType;
  91.  
  92.        
  93.         public function Event (type : EventType) {
  94.             _type = type;
  95.         }
  96.  
  97.         public function get type () : EventType {
  98.             return _type;
  99.         }
  100.  
  101.         public function set type (type : EventType) : void {
  102.             _type = type;
  103.         }
  104.     }
  105. }
  106.  
  107.  
  108. package max4as.event
  109. {
  110.     import max4as.error.NullPointerError;
  111.     import max4as.event.enum.EventType;
  112.     import max4as.util.IComparable;
  113.  
  114.     /**
  115.      * @author Stéphane Robert Richard - kabnot@gmail.com
  116.      */
  117.     public class ListenerReference implements IComparable {
  118.  
  119.  
  120.         private var _listener : Object;
  121.         private var _handlingFunction : Function;
  122.         private var _type : EventType;
  123.  
  124.  
  125.         public function ListenerReference (listener : Object, type : EventType, handlingFunction : Function) {
  126.             if(listener == null || type == null || handlingFunction == null ) throw new NullPointerError( );
  127.             _listener = listener;
  128.             _type = type;
  129.             _handlingFunction = handlingFunction;
  130.         }
  131.  
  132.         public function get listener () : Object {
  133.             return _listener;
  134.         }
  135.  
  136.         public function get type () : EventType {
  137.             return _type;
  138.         }
  139.  
  140.         public function get handlingFunction () : Function {
  141.             return _handlingFunction;
  142.         }
  143.  
  144.         public function equals (o : Object) : Boolean {
  145.             if ((o == this)) return true ;
  146.             var l:ListenerReference ;
  147.             if (!(o is ListenerReference)) {
  148.                 return false ;
  149.             }else{
  150.                 l = o as ListenerReference ;
  151.             }
  152.             if (l.listener == _listener && l.type == type && l.handlingFunction == _handlingFunction)
  153.                 return true ;
  154.             return false ;
  155.         }
  156.     }
  157. }
  158.  
  159.  
  160. package  max4as.event.enum
  161. {
  162.  
  163.     /**
  164.      * @author Stéphane Robert Richard - kabnot@gmail.com
  165.      */
  166.     public class EventType {
  167.     }
  168. }
  169.  
  170. /**
  171.  * Implémentation d'une enum
  172.  */
  173. package max4as.test.mock.object
  174. {
  175.     import max4as.event.enum.EventType;
  176.  
  177.     /**
  178.      * @author Stéphane Robert Richard - kabnot@gmail.com
  179.      */
  180.     public final class MockEventTypeEnum extends EventType {
  181.  
  182.        
  183.         public static const MOCK_EVENT : MockEventTypeEnum = new MockEventTypeEnum( );
  184.     }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement