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 20 29 30 public class VetoableChangeSupport implements java.io.Serializable { 31 32 37 38 public VetoableChangeSupport(Object sourceBean) { 39 if (sourceBean == null) { 40 throw new NullPointerException (); 41 } 42 source = sourceBean; 43 } 44 45 55 56 public synchronized void addVetoableChangeListener( 57 VetoableChangeListener listener) { 58 if (listener == null) { 59 return; 60 } 61 if (listener instanceof VetoableChangeListenerProxy ) { 62 VetoableChangeListenerProxy proxy = 63 (VetoableChangeListenerProxy )listener; 64 addVetoableChangeListener(proxy.getPropertyName(), 66 (VetoableChangeListener )proxy.getListener()); 67 } else { 68 if (listeners == null) { 69 listeners = new java.util.Vector (); 70 } 71 } 72 listeners.addElement(listener); 73 } 74 75 86 public synchronized void removeVetoableChangeListener( 87 VetoableChangeListener listener) { 88 if (listener == null) { 89 return; 90 } 91 if (listener instanceof VetoableChangeListenerProxy ) { 92 VetoableChangeListenerProxy proxy = 93 (VetoableChangeListenerProxy )listener; 94 removeVetoableChangeListener(proxy.getPropertyName(), 96 (VetoableChangeListener )proxy.getListener()); 97 } else { 98 if (listeners == null) { 99 return; 100 } 101 listeners.removeElement(listener); 102 } 103 } 104 105 113 public synchronized VetoableChangeListener [] getVetoableChangeListeners(){ 114 List returnList = new ArrayList (); 115 116 if (listeners != null) { 118 returnList.addAll(listeners); 119 } 120 121 if (children != null) { 123 Iterator iterator = children.keySet().iterator(); 124 while (iterator.hasNext()) { 125 String key = (String )iterator.next(); 126 VetoableChangeSupport child = 127 (VetoableChangeSupport )children.get(key); 128 VetoableChangeListener [] childListeners = 129 child.getVetoableChangeListeners(); 130 for (int index = childListeners.length - 1; index >= 0; 131 index--) { 132 returnList.add(new VetoableChangeListenerProxy ( 133 key, childListeners[index])); 134 } 135 } 136 } 137 return (VetoableChangeListener [])(returnList.toArray( 138 new VetoableChangeListener [0])); 139 } 140 141 154 155 public synchronized void addVetoableChangeListener( 156 String propertyName, 157 VetoableChangeListener listener) { 158 if (listener == null || propertyName == null) { 159 return; 160 } 161 if (children == null) { 162 children = new java.util.Hashtable (); 163 } 164 VetoableChangeSupport child = (VetoableChangeSupport )children.get(propertyName); 165 if (child == null) { 166 child = new VetoableChangeSupport (source); 167 children.put(propertyName, child); 168 } 169 child.addVetoableChangeListener(listener); 170 } 171 172 185 186 public synchronized void removeVetoableChangeListener( 187 String propertyName, 188 VetoableChangeListener listener) { 189 if (listener == null || propertyName == null) { 190 return; 191 } 192 if (children == null) { 193 return; 194 } 195 VetoableChangeSupport child = (VetoableChangeSupport )children.get(propertyName); 196 if (child == null) { 197 return; 198 } 199 child.removeVetoableChangeListener(listener); 200 } 201 202 212 public synchronized VetoableChangeListener [] getVetoableChangeListeners( 213 String propertyName) { 214 List returnList = new ArrayList (); 215 216 if (children != null && propertyName != null) { 217 VetoableChangeSupport support = 218 (VetoableChangeSupport )children.get(propertyName); 219 if (support != null) { 220 returnList.addAll( 221 Arrays.asList(support.getVetoableChangeListeners())); 222 } 223 } 224 return (VetoableChangeListener [])(returnList.toArray(new 225 VetoableChangeListener [0])); 226 } 227 228 242 public void fireVetoableChange(String propertyName, 243 Object oldValue, Object newValue) 244 throws PropertyVetoException { 245 if (listeners == null && children == null) { 246 return; 247 } 248 249 PropertyChangeEvent evt = new PropertyChangeEvent (source, propertyName, 250 oldValue, newValue); 251 fireVetoableChange(evt); 252 } 253 254 266 public void fireVetoableChange(String propertyName, 267 int oldValue, int newValue) 268 throws PropertyVetoException { 269 if (oldValue == newValue) { 270 return; 271 } 272 fireVetoableChange(propertyName, new Integer (oldValue), new Integer (newValue)); 273 } 274 275 287 public void fireVetoableChange(String propertyName, 288 boolean oldValue, boolean newValue) 289 throws PropertyVetoException { 290 if (oldValue == newValue) { 291 return; 292 } 293 fireVetoableChange(propertyName, Boolean.valueOf(oldValue), Boolean.valueOf(newValue)); 294 } 295 296 297 308 public void fireVetoableChange(PropertyChangeEvent evt) 309 throws PropertyVetoException { 310 311 Object oldValue = evt.getOldValue(); 312 Object newValue = evt.getNewValue(); 313 String propertyName = evt.getPropertyName(); 314 if (oldValue != null && newValue != null && oldValue.equals(newValue)) { 315 return; 316 } 317 318 java.util.Vector targets = null; 319 VetoableChangeSupport child = null; 320 synchronized (this) { 321 if (listeners != null) { 322 targets = (java.util.Vector ) listeners.clone(); 323 } 324 if (children != null && propertyName != null) { 325 child = (VetoableChangeSupport )children.get(propertyName); 326 } 327 } 328 329 if (listeners != null) { 330 try { 331 for (int i = 0; i < targets.size(); i++) { 332 VetoableChangeListener target = 333 (VetoableChangeListener )targets.elementAt(i); 334 target.vetoableChange(evt); 335 } 336 } catch (PropertyVetoException veto) { 337 evt = new PropertyChangeEvent (source, propertyName, newValue, oldValue); 339 for (int i = 0; i < targets.size(); i++) { 340 try { 341 VetoableChangeListener target = 342 (VetoableChangeListener )targets.elementAt(i); 343 target.vetoableChange(evt); 344 } catch (PropertyVetoException ex) { 345 } 347 } 348 throw veto; 350 } 351 } 352 353 if (child != null) { 354 child.fireVetoableChange(evt); 355 } 356 } 357 358 359 367 public synchronized boolean hasListeners(String propertyName) { 368 if (listeners != null && !listeners.isEmpty()) { 369 return true; 371 } 372 if (children != null && propertyName != null) { 373 VetoableChangeSupport child = (VetoableChangeSupport )children.get(propertyName); 374 if (child != null && child.listeners != null) { 375 return !child.listeners.isEmpty(); 376 } 377 } 378 return false; 379 } 380 381 388 389 private void writeObject(ObjectOutputStream s) throws IOException { 390 s.defaultWriteObject(); 391 392 java.util.Vector v = null; 393 synchronized (this) { 394 if (listeners != null) { 395 v = (java.util.Vector ) listeners.clone(); 396 } 397 } 398 399 if (v != null) { 400 for(int i = 0; i < v.size(); i++) { 401 VetoableChangeListener l = (VetoableChangeListener )v.elementAt(i); 402 if (l instanceof Serializable ) { 403 s.writeObject(l); 404 } 405 } 406 } 407 s.writeObject(null); 408 } 409 410 411 private void readObject(ObjectInputStream s) throws ClassNotFoundException , IOException { 412 s.defaultReadObject(); 413 414 Object listenerOrNull; 415 while(null != (listenerOrNull = s.readObject())) { 416 addVetoableChangeListener((VetoableChangeListener )listenerOrNull); 417 } 418 } 419 420 425 transient private java.util.Vector listeners; 426 427 433 private java.util.Hashtable children; 434 435 439 private Object source; 440 441 445 private int vetoableChangeSupportSerializedDataVersion = 2; 446 447 450 static final long serialVersionUID = -5090210921595982017L; 451 } 452 | Popular Tags |