1 /* 2 * Copyright the original author or authors. 3 * 4 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.mozilla.org/MPL/MPL-1.1.html 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 import org.as2lib.env.event.EventListenerSource; 18 import org.as2lib.env.log.LogSupport; 19 import org.as2lib.env.event.distributor.CompositeEventDistributorControl; 20 import org.as2lib.env.event.distributor.SimpleConsumableCompositeEventDistributorControl; 21 22 /** 23 * {@code EventSupport} provides simple access to events. 24 * 25 * <p>To apply event abilities to your class its only necessary to extend 26 * {@code EventSupport}. It holds in the private instance variable 27 * {@code distributorControl} a reference to 28 * {@link SimpleConsumableCompositeEventDistributionControl}. 29 * 30 * <p>To allow additional listener types to be checked by {@link #addListener}, 31 * {@link #addAddListener} its only necessary to add {@code acceptListenerType(AnyType);} 32 * within the constructor of the extended class. 33 * 34 * <p>It is necessary for sending a event to recieve the matching distributor with 35 * {@code distributorControl.getDistributor(AnyType)} and execute the event to it. 36 * 37 * <p>Example code: 38 * <code> 39 * class Controller extends EventSupport { 40 * 41 * private var model:Model; 42 * private var distributor:View; 43 * 44 * public function Controller(Void) { 45 * distributorControl.acceptListenerType(View); 46 * distributor = distributorControl.getDistributor(View); 47 * } 48 * 49 * public function setTargetModel(model:Model) { 50 * this.model = model; 51 * distributor.onTargetModelChanged(this); 52 * } 53 * 54 * public function getTargetModel(Void):Model { 55 * return model; 56 * } 57 * } 58 * </code> 59 * 60 * @author Martin Heidegger 61 * @version 1.0 62 */ 63 class org.as2lib.env.event.EventSupport extends LogSupport implements EventListenerSource { 64 65 /** Access to event control. */ 66 private var distributorControl:CompositeEventDistributorControl; 67 68 /** 69 * Constructs a new {@code EventSupport} instance. 70 */ 71 function EventSupport(Void) { 72 distributorControl = new SimpleConsumableCompositeEventDistributorControl(); 73 } 74 75 /** 76 * Adds the passed-in {@code listener} to be executed by events. 77 * 78 * @param listener the listener to add 79 * @throws IllegalArgumentException if the listener does not match any expected type 80 */ 81 public function addListener(listener):Void { 82 distributorControl.addListener(listener); 83 } 84 85 /** 86 * Adds all listener contained in the passed-in {@code listeners} array. 87 * 88 * <p>All listeners get added after each other. If one listener does not match 89 * any expected type the rest of the listeners will not be added an a exception 90 * will raise. 91 * 92 * @param listeners the list of listeners to add 93 * @throws IllegalArgumentException if any listener does not match any expected type 94 */ 95 public function addAllListeners(listeners:Array):Void { 96 distributorControl.addAllListeners(listeners); 97 } 98 99 100 /** 101 * Removes the passed-in {@code listener} from beeing executed by events. 102 * 103 * @param listener the listener to remove 104 */ 105 public function removeListener(listener):Void { 106 distributorControl.removeListener(listener); 107 } 108 109 /** 110 * Removes all listeners from beeing executed by events. 111 */ 112 public function removeAllListeners(Void):Void { 113 distributorControl.removeAllListeners(); 114 } 115 116 /** 117 * Getter for the list of all added listeners. 118 * 119 * <p>This method returns a copy (not a reference) of the list of all added 120 * listeners. 121 * 122 * @return list that contains all added listeners 123 */ 124 public function getAllListeners(Void):Array { 125 return distributorControl.getAllListeners(); 126 } 127 128 /** 129 * Checks if the passed-in {@code listener} has been added. 130 * 131 * @return {@code true} if the listener has been added 132 */ 133 public function hasListener(listener):Boolean { 134 return distributorControl.hasListener(listener); 135 } 136 137 /** 138 * Internal method to accept a concrete listener type. 139 * 140 * <p>Any listener added with {@code addListener} will be checked against 141 * all accepted typed and added. 142 * 143 * @param type to accept as listener 144 */ 145 private function acceptListenerType(type:Function):Void { 146 distributorControl.acceptListenerType(type); 147 } 148 149 }