home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / VetoableChangeSupport.java < prev    next >
Text File  |  1997-10-01  |  5KB  |  164 lines

  1. /*
  2.  * @(#)VetoableChangeSupport.java    1.13 97/05/26  
  3.  * 
  4.  * Copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion bdk_beta
  20.  * 
  21.  */
  22.  
  23. package java.beans;
  24.  
  25. import java.io.Serializable;
  26. import java.io.ObjectOutputStream;
  27. import java.io.ObjectInputStream;
  28. import java.io.IOException;
  29.  
  30.  
  31. /**
  32.  * This is a utility class that can be used by beans that support constrained
  33.  * properties.  You can use an instance of this class as a member field
  34.  * of your bean and delegate various work to it.
  35.  */
  36.  
  37. public class VetoableChangeSupport implements java.io.Serializable {
  38.  
  39.     /**
  40.      * @sourceBean  The bean to be given as the source for any events.
  41.      */
  42.  
  43.     public VetoableChangeSupport(Object sourceBean) {
  44.     source = sourceBean;
  45.     }
  46.  
  47.     /**
  48.      * Add a VetoableListener to the listener list.
  49.      *
  50.      * @param listener  The VetoableChangeListener to be added
  51.      */
  52.  
  53.     public synchronized void addVetoableChangeListener(
  54.                     VetoableChangeListener listener) {
  55.     if (listeners == null) {
  56.         listeners = new java.util.Vector();
  57.     }
  58.     listeners.addElement(listener);
  59.     }
  60.  
  61.     /**
  62.      * Remove a VetoableChangeListener from the listener list.
  63.      *
  64.      * @param listener  The VetoableChangeListener to be removed
  65.      */
  66.     public synchronized void removeVetoableChangeListener(
  67.                     VetoableChangeListener listener) {
  68.     if (listeners == null) {
  69.         return;
  70.     }
  71.     listeners.removeElement(listener);
  72.     }
  73.  
  74.     /**
  75.      * Report a vetoable property update to any registered listeners.  If
  76.      * anyone vetos the change, then fire a new event reverting everyone to 
  77.      * the old value and then rethrow the PropertyVetoException.
  78.      * <p>
  79.      * No event is fired if old and new are equal and non-null.
  80.      *
  81.      * @param propertyName  The programmatic name of the property
  82.      *        that was changed.
  83.      * @param oldValue  The old value of the property.
  84.      * @param newValue  The new value of the property.
  85.      * @exception PropertyVetoException if the recipient wishes the property
  86.      *              change to be rolled back.
  87.      */
  88.     public void fireVetoableChange(String propertyName, 
  89.                     Object oldValue, Object newValue)
  90.                     throws PropertyVetoException {
  91.  
  92.     if (oldValue != null && oldValue.equals(newValue)) {
  93.         return;
  94.     }
  95.  
  96.     java.util.Vector targets;
  97.     synchronized (this) {
  98.         if (listeners == null) {
  99.             return;
  100.         }
  101.         targets = (java.util.Vector) listeners.clone();
  102.     }
  103.         PropertyChangeEvent evt = new PropertyChangeEvent(source,
  104.                         propertyName, oldValue, newValue);
  105.  
  106.     try {
  107.         for (int i = 0; i < targets.size(); i++) {
  108.             VetoableChangeListener target = 
  109.                 (VetoableChangeListener)targets.elementAt(i);
  110.             target.vetoableChange(evt);
  111.         }
  112.     } catch (PropertyVetoException veto) {
  113.         // Create an event to revert everyone to the old value.
  114.                evt = new PropertyChangeEvent(source, propertyName, newValue, oldValue);
  115.         for (int i = 0; i < targets.size(); i++) {
  116.         try {
  117.                 VetoableChangeListener target =
  118.                 (VetoableChangeListener)targets.elementAt(i);
  119.                 target.vetoableChange(evt);
  120.         } catch (PropertyVetoException ex) {
  121.              // We just ignore exceptions that occur during reversions.
  122.         }
  123.         }
  124.         // And now rethrow the PropertyVetoException.
  125.         throw veto;
  126.     }
  127.     }
  128.  
  129.     private void writeObject(ObjectOutputStream s) throws IOException {
  130.         s.defaultWriteObject();
  131.  
  132.     java.util.Vector v = null;
  133.     synchronized (this) {
  134.         if (listeners != null) {
  135.             v = (java.util.Vector) listeners.clone();
  136.             }
  137.     }
  138.  
  139.     if (v != null) {
  140.         for(int i = 0; i < listeners.size(); i++) {
  141.             VetoableChangeListener l = (VetoableChangeListener)v.elementAt(i);
  142.             if (l instanceof Serializable) {
  143.                 s.writeObject(l);
  144.             }
  145.             }
  146.         }
  147.         s.writeObject(null);
  148.     }
  149.  
  150.  
  151.     private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
  152.         s.defaultReadObject();
  153.       
  154.         Object listenerOrNull;
  155.         while(null != (listenerOrNull = s.readObject())) {
  156.         addVetoableChangeListener((VetoableChangeListener)listenerOrNull);
  157.         }
  158.     }
  159.  
  160.     transient private java.util.Vector listeners;
  161.     private Object source;
  162.     private int vetoableChangeSupportSerializedDataVersion = 1;
  163. }
  164.