1 7 package javax.swing.event; 8 9 import java.io.*; 10 import java.util.*; 11 import java.lang.reflect.Array ; 12 13 82 public class EventListenerList implements Serializable { 83 84 private final static Object [] NULL_ARRAY = new Object [0]; 85 86 protected transient Object [] listenerList = NULL_ARRAY; 87 88 105 public Object [] getListenerList() { 106 return listenerList; 107 } 108 109 117 public <T extends EventListener> T[] getListeners(Class <T> t) { 118 Object [] lList = listenerList; 119 int n = getListenerCount(lList, t); 120 T[] result = (T[])Array.newInstance(t, n); 121 int j = 0; 122 for (int i = lList.length-2; i>=0; i-=2) { 123 if (lList[i] == t) { 124 result[j++] = (T)lList[i+1]; 125 } 126 } 127 return result; 128 } 129 130 133 public int getListenerCount() { 134 return listenerList.length/2; 135 } 136 137 141 public int getListenerCount(Class <?> t) { 142 Object [] lList = listenerList; 143 return getListenerCount(lList, t); 144 } 145 146 private int getListenerCount(Object [] list, Class t) { 147 int count = 0; 148 for (int i = 0; i < list.length; i+=2) { 149 if (t == (Class )list[i]) 150 count++; 151 } 152 return count; 153 } 154 155 160 public synchronized <T extends EventListener> void add(Class <T> t, T l) { 161 if (l==null) { 162 return; 166 } 167 if (!t.isInstance(l)) { 168 throw new IllegalArgumentException ("Listener " + l + 169 " is not of type " + t); 170 } 171 if (listenerList == NULL_ARRAY) { 172 listenerList = new Object [] { t, l }; 175 } else { 176 int i = listenerList.length; 178 Object [] tmp = new Object [i+2]; 179 System.arraycopy(listenerList, 0, tmp, 0, i); 180 181 tmp[i] = t; 182 tmp[i+1] = l; 183 184 listenerList = tmp; 185 } 186 } 187 188 193 public synchronized <T extends EventListener> void remove(Class <T> t, T l) { 194 if (l ==null) { 195 return; 199 } 200 if (!t.isInstance(l)) { 201 throw new IllegalArgumentException ("Listener " + l + 202 " is not of type " + t); 203 } 204 int index = -1; 206 for (int i = listenerList.length-2; i>=0; i-=2) { 207 if ((listenerList[i]==t) && (listenerList[i+1].equals(l) == true)) { 208 index = i; 209 break; 210 } 211 } 212 213 if (index != -1) { 215 Object [] tmp = new Object [listenerList.length-2]; 216 System.arraycopy(listenerList, 0, tmp, 0, index); 218 if (index < tmp.length) 222 System.arraycopy(listenerList, index+2, tmp, index, 223 tmp.length - index); 224 listenerList = (tmp.length == 0) ? NULL_ARRAY : tmp; 226 } 227 } 228 229 private void writeObject(ObjectOutputStream s) throws IOException { 231 Object [] lList = listenerList; 232 s.defaultWriteObject(); 233 234 for (int i = 0; i < lList.length; i+=2) { 236 Class t = (Class )lList[i]; 237 EventListener l = (EventListener)lList[i+1]; 238 if ((l!=null) && (l instanceof Serializable)) { 239 s.writeObject(t.getName()); 240 s.writeObject(l); 241 } 242 } 243 244 s.writeObject(null); 245 } 246 247 private void readObject(ObjectInputStream s) 248 throws IOException, ClassNotFoundException { 249 listenerList = NULL_ARRAY; 250 s.defaultReadObject(); 251 Object listenerTypeOrNull; 252 253 while (null != (listenerTypeOrNull = s.readObject())) { 254 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 255 EventListener l = (EventListener)s.readObject(); 256 add((Class <EventListener>)Class.forName((String )listenerTypeOrNull, true, cl), l); 257 } 258 } 259 260 263 public String toString() { 264 Object [] lList = listenerList; 265 String s = "EventListenerList: "; 266 s += lList.length/2 + " listeners: "; 267 for (int i = 0 ; i <= lList.length-2 ; i+=2) { 268 s += " type " + ((Class )lList[i]).getName(); 269 s += " listener " + lList[i+1]; 270 } 271 return s; 272 } 273 } 274 | Popular Tags |