1 24 25 package swingwtx.swing.event; 26 27 import java.lang.reflect.Array ; 28 import java.util.EventListener ; 29 import java.util.Vector ; 30 31 public class EventListenerList { 32 33 protected Vector listenerList = new Vector (); 34 35 public Object [] getListenerList() { 36 return listenerList.toArray(); 37 } 38 39 public synchronized void add(Class t, EventListener l) { 40 listenerList.add(l); 41 } 42 43 public synchronized void remove(Class t, EventListener l) { 44 45 listenerList.remove(l); 46 } 47 48 79 80 public EventListener [] getListeners(Class t) { 81 int n = getListenerCount(t); 82 EventListener [] listeners = (EventListener [])Array.newInstance(t, n); 83 int listenersIndex = 0; 84 for (int i = listenerList.size() -2; i>=0; i-=2) { 85 if (listenerList.get(i) == t) { 86 listeners[listenersIndex] = listeners[i+1]; 87 listenersIndex++; 88 } 89 } 90 return listeners; 91 } 92 93 public int getListenerCount() { 94 return listenerList.size(); 95 } 96 97 public int getListenerCount(Class t) { 98 int listenerCount = 0; 99 Object [] listeners = getListenerList(); 100 for (int i = 0; i < listeners.length; i +=2 ) { 101 if (t == listeners[i]) 102 listenerCount++; 103 } 104 return listenerCount; 105 } 106 107 public String toString() { 108 Object [] list = listenerList.toArray(); 109 String s = "EventListenerList: "; 110 s += list.length + " listeners: "; 111 for (int i = 0 ; i < list.length ; i++) { 112 s += " type " + ((Class )list[i]).getName(); 113 s += " listener " + list[i].toString(); 114 } 115 return s; 116 } 117 } 118 | Popular Tags |