1 19 20 package org.netbeans.editor; 21 22 import java.lang.ref.WeakReference ; 23 import java.lang.reflect.Array ; 24 import java.lang.reflect.Array ; 25 import java.io.ObjectInputStream ; 26 import java.io.ObjectOutputStream ; 27 import java.io.IOException ; 28 import java.io.Serializable ; 29 import java.util.EventListener ; 30 import javax.swing.event.EventListenerList ; 31 32 50 51 public class WeakEventListenerList extends EventListenerList { 52 53 private int listenerSize; 54 55 58 public synchronized Object [] getListenerList() { 59 int tgtInd = 0; 60 Object [] ret = new Object [listenerSize]; 61 for (int i = 1; i < listenerSize; i += 2) { 62 Object l = ((WeakReference )listenerList[i]).get(); 63 if (l != null) { 64 ret[tgtInd++] = listenerList[i - 1]; 65 ret[tgtInd++] = l; 66 } else { 71 System.arraycopy(listenerList, i + 1, listenerList, i - 1, 72 listenerSize - i - 1); 73 listenerSize -= 2; 74 i -= 2; 75 } 76 } 77 78 if (ret.length != tgtInd) { 79 Object [] tmp = new Object [tgtInd]; 80 System.arraycopy(ret, 0, tmp, 0, tgtInd); 81 ret = tmp; 82 } 83 84 return ret; 85 } 86 87 public synchronized EventListener [] getListeners(Class t) { 88 int tgtInd = 0; 89 EventListener [] ret = (EventListener [])Array.newInstance(t, listenerSize); 90 for (int i = 0; i < listenerSize; i++) { 91 if (listenerList[i++] == t) { 92 EventListener l = (EventListener )((WeakReference )listenerList[i]).get(); 93 if (l != null) { 94 ret[tgtInd++] = l; 95 } else { 100 System.arraycopy(listenerList, i + 1, listenerList, i - 1, 101 listenerSize - i - 1); 102 listenerSize -= 2; 103 i -= 2; 104 } 105 } 106 } 107 108 if (ret.length != tgtInd) { 109 EventListener [] tmp = (EventListener [])Array.newInstance(t, tgtInd); 110 System.arraycopy(ret, 0, tmp, 0, tgtInd); 111 ret = tmp; 112 } 113 114 return ret; 115 } 116 117 118 123 public synchronized void add(Class t, EventListener l) { 124 if (l==null) { 125 return; 129 } 130 131 if (!t.isInstance(l)) { 132 throw new IllegalArgumentException ("Listener " + l + " is not of type " + t); } 135 136 if (listenerSize == 0) { 137 listenerList = new Object [] { t, new WeakReference (l) }; 138 listenerSize = 2; 139 } else { 140 if (listenerSize == listenerList.length) { Object [] tmp = new Object [listenerSize * 2]; 142 System.arraycopy(listenerList, 0, tmp, 0, listenerSize); 143 listenerList = tmp; 144 } 145 146 listenerList[listenerSize++] = t; 147 listenerList[listenerSize++] = new WeakReference (l); 148 } 149 } 150 151 156 public synchronized void remove(Class t, EventListener l) { 157 if (l ==null) { 158 return; 162 } 163 164 if (!t.isInstance(l)) { 165 throw new IllegalArgumentException ("Listener " + l + " is not of type " + t); } 168 169 int index = -1; 171 for (int i = listenerSize - 2; i >= 0; i -= 2) { 172 if ((listenerList[i] == t) 173 && l == ((WeakReference )listenerList[ i + 1]).get() 174 ) { 175 index = i; 176 break; 177 } 178 } 179 180 if (index >= 0) { 182 System.arraycopy(listenerList, index + 2, listenerList, index, 183 listenerSize - index - 2); 184 listenerSize -= 2; 185 } 186 } 187 188 private synchronized void writeObject(ObjectOutputStream os) throws IOException { 189 os.defaultWriteObject(); 190 191 for (int i = 0; i < listenerSize; i += 2) { 193 Class t = (Class )listenerList[i]; 194 EventListener l = (EventListener )((WeakReference )listenerList[i + 1]).get(); 195 if ((l != null) && (l instanceof Serializable )) { 196 os.writeObject(t.getName()); 197 os.writeObject(l); 198 } 199 } 200 201 os.writeObject(null); 202 } 203 204 private void readObject(ObjectInputStream is) 205 throws IOException , ClassNotFoundException { 206 is.defaultReadObject(); 207 Object listenerTypeOrNull; 208 209 while (null != (listenerTypeOrNull = is.readObject())) { 210 EventListener l = (EventListener )is.readObject(); 211 add(Class.forName((String )listenerTypeOrNull), l); 212 } 213 } 214 215 public synchronized String toString() { 216 StringBuffer sb = new StringBuffer ("WeakEventListenerList: "); sb.append(listenerSize / 2); 218 sb.append(" listeners:\n"); for (int i = 0; i < listenerSize; i += 2) { 220 sb.append(" type " + ((Class )listenerList[i]).getName()); sb.append(" listener " + ((WeakReference )listenerList[i + 1]).get()); } 223 return sb.toString(); 224 } 225 226 } 227 | Popular Tags |