1 14 package org.wings.util; 15 16 import java.beans.PropertyChangeEvent ; 17 import java.beans.PropertyChangeListener ; 18 import java.lang.ref.ReferenceQueue ; 19 import java.lang.ref.WeakReference ; 20 21 33 public class WeakPropertyChangeSupport { 34 39 public WeakPropertyChangeSupport(Object sourceBean) { 40 if (sourceBean == null) { 41 throw new NullPointerException (); 42 } 43 source = sourceBean; 44 } 45 46 52 public synchronized void addPropertyChangeListener(PropertyChangeListener listener) { 53 if (listeners == null) { 54 listeners = new java.util.LinkedList (); 55 } else 56 processQueue(); 57 listeners.add(WeakEntry.create(listener, queue)); 58 } 59 60 67 public synchronized void removePropertyChangeListener(PropertyChangeListener listener) { 68 if (listeners == null) { 69 return; 70 } else 71 processQueue(); 72 listeners.remove(WeakEntry.create(listener)); 73 } 74 75 83 public synchronized void addPropertyChangeListener(String propertyName, 84 PropertyChangeListener listener) { 85 if (children == null) { 86 children = new java.util.WeakHashMap (); 87 } 88 WeakPropertyChangeSupport child = (WeakPropertyChangeSupport) children.get(propertyName); 89 if (child == null) { 90 child = new WeakPropertyChangeSupport(source); 91 children.put(propertyName, child); 92 } 93 child.addPropertyChangeListener(listener); 94 } 95 96 102 public synchronized void removePropertyChangeListener(String propertyName, 103 PropertyChangeListener listener) { 104 if (children == null) { 105 return; 106 } 107 WeakPropertyChangeSupport child = (WeakPropertyChangeSupport) children.get(propertyName); 108 if (child == null) { 109 return; 110 } 111 child.removePropertyChangeListener(listener); 112 } 113 114 123 public void firePropertyChange(String propertyName, Object oldValue, Object newValue) { 124 if (oldValue != null && newValue != null && oldValue.equals(newValue)) { 125 return; 126 } 127 128 java.util.LinkedList targets = null; 129 WeakPropertyChangeSupport child = null; 130 synchronized (this) { 131 if (listeners != null) { 132 targets = (java.util.LinkedList ) listeners.clone(); 133 } 134 if (children != null && propertyName != null) { 135 child = (WeakPropertyChangeSupport) children.get(propertyName); 136 } 137 } 138 139 PropertyChangeEvent evt = new PropertyChangeEvent (source, propertyName, oldValue, newValue); 140 141 if (targets != null) { 142 for (int i = 0; i < targets.size(); i++) { 143 WeakEntry entry = (WeakEntry) targets.get(i); 144 PropertyChangeListener target = (PropertyChangeListener ) entry.get(); 145 if (target != null) 146 target.propertyChange(evt); 147 } 148 } 149 150 if (child != null) { 151 child.firePropertyChange(evt); 152 } 153 } 154 155 167 public void firePropertyChange(String propertyName, int oldValue, int newValue) { 168 if (oldValue == newValue) { 169 return; 170 } 171 firePropertyChange(propertyName, new Integer (oldValue), new Integer (newValue)); 172 } 173 174 175 187 public void firePropertyChange(String propertyName, 188 boolean oldValue, boolean newValue) { 189 if (oldValue == newValue) { 190 return; 191 } 192 firePropertyChange(propertyName, Boolean.valueOf(oldValue), Boolean.valueOf(newValue)); 193 } 194 195 202 public void firePropertyChange(PropertyChangeEvent evt) { 203 Object oldValue = evt.getOldValue(); 204 Object newValue = evt.getNewValue(); 205 String propertyName = evt.getPropertyName(); 206 if (oldValue != null && newValue != null && oldValue.equals(newValue)) { 207 return; 208 } 209 210 java.util.LinkedList targets = null; 211 WeakPropertyChangeSupport child = null; 212 synchronized (this) { 213 if (listeners != null) { 214 targets = (java.util.LinkedList ) listeners.clone(); 215 } 216 if (children != null && propertyName != null) { 217 child = (WeakPropertyChangeSupport) children.get(propertyName); 218 } 219 } 220 221 if (targets != null) { 222 for (int i = 0; i < targets.size(); i++) { 223 WeakEntry entry = (WeakEntry) targets.get(i); 224 PropertyChangeListener target = (PropertyChangeListener ) entry.get(); 225 if (target != null) 226 target.propertyChange(evt); 227 } 228 } 229 if (child != null) { 230 child.firePropertyChange(evt); 231 } 232 } 233 234 240 public synchronized boolean hasListeners(String propertyName) { 241 if (listeners != null && !listeners.isEmpty()) { 242 return true; 244 } 245 if (children != null) { 246 WeakPropertyChangeSupport child = (WeakPropertyChangeSupport) children.get(propertyName); 247 if (child != null && child.listeners != null) { 248 return !child.listeners.isEmpty(); 249 } 250 } 251 return false; 252 } 253 254 259 transient private java.util.LinkedList listeners; 260 261 267 private java.util.WeakHashMap children; 268 269 274 private Object source; 275 276 277 private ReferenceQueue queue = new ReferenceQueue (); 278 279 286 private void processQueue() { 287 WeakEntry wk; 288 while ((wk = (WeakEntry) queue.poll()) != null) { 289 listeners.remove(wk); 290 } 291 } 292 293 static private class WeakEntry extends WeakReference { 294 private int hash; 296 297 private WeakEntry(Object k) { 298 super(k); 299 hash = k.hashCode(); 300 } 301 302 private static WeakEntry create(Object k) { 303 if (k == null) 304 return null; 305 else 306 return new WeakEntry(k); 307 } 308 309 private WeakEntry(Object k, ReferenceQueue q) { 310 super(k, q); 311 hash = k.hashCode(); 312 } 313 314 private static WeakEntry create(Object k, ReferenceQueue q) { 315 if (k == null) 316 return null; 317 else 318 return new WeakEntry(k, q); 319 } 320 321 323 public boolean equals(Object o) { 324 if (this == o) return true; 325 if (!(o instanceof WeakEntry)) return false; 326 Object t = this.get(); 327 Object u = ((WeakEntry) o).get(); 328 if ((t == null) || (u == null)) return false; 329 if (t == u) return true; 330 return t.equals(u); 331 } 332 333 public int hashCode() { 334 return hash; 335 } 336 } 337 } 338 339 340 | Popular Tags |