1 7 8 package java.beans; 9 10 import java.io.Serializable ; 11 import java.io.ObjectOutputStream ; 12 import java.io.ObjectInputStream ; 13 import java.io.IOException ; 14 import java.util.Arrays ; 15 import java.util.ArrayList ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 19 import sun.awt.EventListenerAggregate; 20 21 31 public class PropertyChangeSupport implements java.io.Serializable { 32 33 private transient EventListenerAggregate listeners; 35 36 41 42 public PropertyChangeSupport(Object sourceBean) { 43 if (sourceBean == null) { 44 throw new NullPointerException (); 45 } 46 source = sourceBean; 47 } 48 49 59 public synchronized void addPropertyChangeListener( 60 PropertyChangeListener listener) { 61 if (listener == null) { 62 return; 63 } 64 65 if (listener instanceof PropertyChangeListenerProxy ) { 66 PropertyChangeListenerProxy proxy = 67 (PropertyChangeListenerProxy )listener; 68 addPropertyChangeListener(proxy.getPropertyName(), 70 (PropertyChangeListener )proxy.getListener()); 71 } else { 72 if (listeners == null) { 73 listeners = new EventListenerAggregate(PropertyChangeListener .class); 74 } 75 listeners.add(listener); 76 } 77 } 78 79 90 public synchronized void removePropertyChangeListener( 91 PropertyChangeListener listener) { 92 if (listener == null) { 93 return; 94 } 95 96 if (listener instanceof PropertyChangeListenerProxy ) { 97 PropertyChangeListenerProxy proxy = 98 (PropertyChangeListenerProxy )listener; 99 removePropertyChangeListener(proxy.getPropertyName(), 101 (PropertyChangeListener )proxy.getListener()); 102 } else { 103 if (listeners == null) { 104 return; 105 } 106 listeners.remove(listener); 107 } 108 } 109 110 141 public synchronized PropertyChangeListener [] getPropertyChangeListeners() { 142 List returnList = new ArrayList (); 143 144 if (listeners != null) { 146 returnList.addAll(Arrays.asList(listeners.getListenersInternal())); 147 } 148 149 if (children != null) { 151 Iterator iterator = children.keySet().iterator(); 152 while (iterator.hasNext()) { 153 String key = (String )iterator.next(); 154 PropertyChangeSupport child = 155 (PropertyChangeSupport )children.get(key); 156 PropertyChangeListener [] childListeners = 157 child.getPropertyChangeListeners(); 158 for (int index = childListeners.length - 1; index >= 0; 159 index--) { 160 returnList.add(new PropertyChangeListenerProxy ( 161 key, childListeners[index])); 162 } 163 } 164 } 165 return (PropertyChangeListener [])(returnList.toArray( 166 new PropertyChangeListener [0])); 167 } 168 169 182 183 public synchronized void addPropertyChangeListener( 184 String propertyName, 185 PropertyChangeListener listener) { 186 if (listener == null || propertyName == null) { 187 return; 188 } 189 if (children == null) { 190 children = new java.util.Hashtable (); 191 } 192 PropertyChangeSupport child = (PropertyChangeSupport )children.get(propertyName); 193 if (child == null) { 194 child = new PropertyChangeSupport (source); 195 children.put(propertyName, child); 196 } 197 child.addPropertyChangeListener(listener); 198 } 199 200 213 214 public synchronized void removePropertyChangeListener( 215 String propertyName, 216 PropertyChangeListener listener) { 217 if (listener == null || propertyName == null) { 218 return; 219 } 220 if (children == null) { 221 return; 222 } 223 PropertyChangeSupport child = (PropertyChangeSupport )children.get(propertyName); 224 if (child == null) { 225 return; 226 } 227 child.removePropertyChangeListener(listener); 228 } 229 230 240 public synchronized PropertyChangeListener [] getPropertyChangeListeners( 241 String propertyName) { 242 ArrayList returnList = new ArrayList (); 243 244 if (children != null && propertyName != null) { 245 PropertyChangeSupport support = 246 (PropertyChangeSupport )children.get(propertyName); 247 if (support != null) { 248 returnList.addAll( 249 Arrays.asList(support.getPropertyChangeListeners())); 250 } 251 } 252 return (PropertyChangeListener [])(returnList.toArray( 253 new PropertyChangeListener [0])); 254 } 255 256 265 public void firePropertyChange(String propertyName, 266 Object oldValue, Object newValue) { 267 if (oldValue != null && newValue != null && oldValue.equals(newValue)) { 268 return; 269 } 270 firePropertyChange(new PropertyChangeEvent (source, propertyName, 271 oldValue, newValue)); 272 } 273 274 286 public void firePropertyChange(String propertyName, 287 int oldValue, int newValue) { 288 if (oldValue == newValue) { 289 return; 290 } 291 firePropertyChange(propertyName, new Integer (oldValue), new Integer (newValue)); 292 } 293 294 295 307 public void firePropertyChange(String propertyName, 308 boolean oldValue, boolean newValue) { 309 if (oldValue == newValue) { 310 return; 311 } 312 firePropertyChange(propertyName, Boolean.valueOf(oldValue), Boolean.valueOf(newValue)); 313 } 314 315 321 public void firePropertyChange(PropertyChangeEvent evt) { 322 Object oldValue = evt.getOldValue(); 323 Object newValue = evt.getNewValue(); 324 String propertyName = evt.getPropertyName(); 325 if (oldValue != null && newValue != null && oldValue.equals(newValue)) { 326 return; 327 } 328 329 if (listeners != null) { 330 Object [] list = listeners.getListenersInternal(); 331 for (int i = 0; i < list.length; i++) { 332 PropertyChangeListener target = (PropertyChangeListener )list[i]; 333 target.propertyChange(evt); 334 } 335 } 336 337 if (children != null && propertyName != null) { 338 PropertyChangeSupport child = null; 339 child = (PropertyChangeSupport )children.get(propertyName); 340 if (child != null) { 341 child.firePropertyChange(evt); 342 } 343 } 344 } 345 346 347 361 public void fireIndexedPropertyChange(String propertyName, int index, 362 Object oldValue, Object newValue) { 363 firePropertyChange(new IndexedPropertyChangeEvent 364 (source, propertyName, oldValue, newValue, index)); 365 } 366 367 384 public void fireIndexedPropertyChange(String propertyName, int index, 385 int oldValue, int newValue) { 386 if (oldValue == newValue) { 387 return; 388 } 389 fireIndexedPropertyChange(propertyName, index, 390 new Integer (oldValue), 391 new Integer (newValue)); 392 } 393 394 410 public void fireIndexedPropertyChange(String propertyName, int index, 411 boolean oldValue, boolean newValue) { 412 if (oldValue == newValue) { 413 return; 414 } 415 fireIndexedPropertyChange(propertyName, index, Boolean.valueOf(oldValue), 416 Boolean.valueOf(newValue)); 417 } 418 419 427 public synchronized boolean hasListeners(String propertyName) { 428 if (listeners != null && !listeners.isEmpty()) { 429 return true; 431 } 432 if (children != null && propertyName != null) { 433 PropertyChangeSupport child = (PropertyChangeSupport )children.get(propertyName); 434 if (child != null && child.listeners != null) { 435 return !child.listeners.isEmpty(); 436 } 437 } 438 return false; 439 } 440 441 448 private void writeObject(ObjectOutputStream s) throws IOException { 449 s.defaultWriteObject(); 450 451 if (listeners != null) { 452 Object [] list = listeners.getListenersCopy(); 453 454 for (int i = 0; i < list.length; i++) { 455 PropertyChangeListener l = (PropertyChangeListener )list[i]; 456 if (l instanceof Serializable ) { 457 s.writeObject(l); 458 } 459 } 460 } 461 s.writeObject(null); 462 } 463 464 465 private void readObject(ObjectInputStream s) throws ClassNotFoundException , IOException { 466 s.defaultReadObject(); 467 468 Object listenerOrNull; 469 while (null != (listenerOrNull = s.readObject())) { 470 addPropertyChangeListener((PropertyChangeListener )listenerOrNull); 471 } 472 } 473 474 480 private java.util.Hashtable children; 481 482 486 private Object source; 487 488 493 private int propertyChangeSupportSerializedDataVersion = 2; 494 495 498 static final long serialVersionUID = 6401253773779951803L; 499 } 500 | Popular Tags |