1 19 20 package org.apache.cayenne.dataview; 21 22 import java.util.ArrayList ; 23 import java.util.EventListener ; 24 25 public class EventDispatcher { 26 protected transient ArrayList listeners = new ArrayList (1); 27 28 public void dispatch(DispatchableEvent e) { 29 EventListener [] listenersCopy = null; 30 synchronized(this) { 31 if (hasListeners()) 32 listenersCopy = (EventListener [])listeners.toArray(new EventListener [listeners.size()]); 33 } 34 35 if (listenersCopy != null) { 36 int count = listenersCopy.length; 37 for (int index = 0; index < count; ++index) { 38 e.dispatch(listenersCopy[index]); 39 } 40 } 41 } 42 43 public synchronized boolean hasListeners() { 44 return !listeners.isEmpty(); 45 } 46 47 public synchronized int getListenerCount() { 48 return listeners.size(); 49 } 50 51 public synchronized int find(EventListener listener) { 52 return listeners.indexOf(listener); 53 } 54 55 public synchronized void add(EventListener listener) { 56 if (find(listener) < 0) 57 listeners.add(listener); 58 } 59 60 public synchronized void remove(EventListener listener) { 61 listeners.remove(listener); 62 } 63 64 public synchronized void clear() { 65 listeners.clear(); 66 } 67 68 public static EventDispatcher add(EventDispatcher dispatcher, EventListener listener) { 69 if (dispatcher == null) 70 dispatcher = new EventDispatcher(); 71 dispatcher.add(listener); 72 return dispatcher; 73 } 74 75 public final static EventDispatcher remove(EventDispatcher dispatcher, EventListener listener) { 76 if (dispatcher != null) { 77 dispatcher.remove(listener); 78 if (!dispatcher.hasListeners()) 79 dispatcher = null; 80 } 81 return dispatcher; 82 } 83 } 84 | Popular Tags |