1 56 package org.objectstyle.cayenne.dataview; 57 58 import java.util.ArrayList ; 59 import java.util.EventListener ; 60 61 public class EventDispatcher { 62 protected transient ArrayList listeners = new ArrayList (1); 63 64 public void dispatch(DispatchableEvent e) { 65 EventListener [] listenersCopy = null; 66 synchronized(this) { 67 if (hasListeners()) 68 listenersCopy = (EventListener [])listeners.toArray(new EventListener [listeners.size()]); 69 } 70 71 if (listenersCopy != null) { 72 int count = listenersCopy.length; 73 for (int index = 0; index < count; ++index) { 74 e.dispatch(listenersCopy[index]); 75 } 76 } 77 } 78 79 public synchronized boolean hasListeners() { 80 return !listeners.isEmpty(); 81 } 82 83 public synchronized int getListenerCount() { 84 return listeners.size(); 85 } 86 87 public synchronized int find(EventListener listener) { 88 return listeners.indexOf(listener); 89 } 90 91 public synchronized void add(EventListener listener) { 92 if (find(listener) < 0) 93 listeners.add(listener); 94 } 95 96 public synchronized void remove(EventListener listener) { 97 listeners.remove(listener); 98 } 99 100 public synchronized void clear() { 101 listeners.clear(); 102 } 103 104 public static EventDispatcher add(EventDispatcher dispatcher, EventListener listener) { 105 if (dispatcher == null) 106 dispatcher = new EventDispatcher(); 107 dispatcher.add(listener); 108 return dispatcher; 109 } 110 111 public final static EventDispatcher remove(EventDispatcher dispatcher, EventListener listener) { 112 if (dispatcher != null) { 113 dispatcher.remove(listener); 114 if (!dispatcher.hasListeners()) 115 dispatcher = null; 116 } 117 return dispatcher; 118 } 119 } 120 | Popular Tags |