home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / VCAFE.3.0A / Main.bin / VetoableChangeSupport.java < prev    next >
Text File  |  1998-04-21  |  5KB  |  175 lines

  1. package com.symantec.itools.beans;
  2.  
  3.  
  4. import java.io.Serializable;
  5. import java.io.ObjectOutputStream;
  6. import java.io.ObjectInputStream;
  7. import java.io.IOException;
  8. import java.beans.PropertyVetoException;
  9. import java.beans.PropertyChangeListener;
  10. import java.beans.VetoableChangeListener;
  11. import java.beans.PropertyChangeEvent;
  12.  
  13.  
  14. /**
  15.  * This is a utility class that can be used by beans that support constrained
  16.  * properties.  Your can either inherit from this class or you can use
  17.  * an instance of this class as a member field of your bean and delegate
  18.  * various work to it.
  19.  * <p>
  20.  * This extension of the java.beans.VetoableChangeSupport class adds
  21.  * functionality to handle individual property changes.
  22.  *
  23.  * @author Symantec
  24.  */
  25. public class VetoableChangeSupport extends java.beans.VetoableChangeSupport implements java.io.Serializable
  26. {
  27.  
  28.     /**
  29.      * Constructs a VetoableChangeSupport object.
  30.      * @param sourceBean the bean to be given as the source for any events
  31.      */
  32.  
  33.     public VetoableChangeSupport(Object sourceBean)
  34.     {
  35.         super(sourceBean);
  36.         source = sourceBean;
  37.     }
  38.  
  39.     /**
  40.      * Adds a VetoableListener to the listener list.
  41.      *
  42.      * @param propertyName    the name of the property to add a listener for
  43.      * @param listener  the VetoableChangeListener to be added
  44.      * @see #removeVetoableChangeListener
  45.      */
  46.     public synchronized void addVetoableChangeListener(String propertyName, VetoableChangeListener listener)
  47.     {
  48.         java.util.Vector listenerList;
  49.  
  50.         if(listenerTable == null)
  51.         {
  52.             listenerTable = new java.util.Hashtable();
  53.         }
  54.  
  55.         if(listenerTable.containsKey(propertyName))
  56.         {
  57.             listenerList = (java.util.Vector)listenerTable.get(propertyName);
  58.         }
  59.         else
  60.         {
  61.             listenerList = new java.util.Vector();
  62.         }
  63.  
  64.         listenerList.addElement(listener);
  65.         listenerTable.put(propertyName, listenerList);
  66.     }
  67.  
  68.     /**
  69.      * Removes a VetoableChangeListener from the listener list.
  70.      *
  71.      * @param propertyName    the name of the property to remove a listener for.
  72.      * @param listener    the VetoableChangeListener to be removed
  73.      * @see #addVetoableChangeListener
  74.      */
  75.     public synchronized void removeVetoableChangeListener(String propertyName, VetoableChangeListener listener)
  76.     {
  77.         java.util.Vector listenerList;
  78.  
  79.         if (listenerTable == null || !listenerTable.containsKey(propertyName))
  80.         {
  81.             return;
  82.         }
  83.         listenerList = (java.util.Vector)listenerTable.get(propertyName);
  84.         listenerList.removeElement(listener);
  85.     }
  86.  
  87.     /**
  88.      * Reports a vetoable property update to any registered listeners.
  89.      * If anyone vetos the change, then a new event is fired reverting everyone to
  90.      * the old value, and then the PropertyVetoException is rethrown.
  91.      * <p>
  92.      * No event is fired if old and new are equal and non-null.
  93.      *
  94.      * @param propertyName  the programmatic name of the property
  95.      *        that was changed
  96.      * @param oldValue  the old value of the property
  97.      * @param newValue  the new value of the property
  98.      *
  99.      * @exception PropertyVetoException
  100.      * if the specified property value is unacceptable
  101.      */
  102.     public void fireVetoableChange(String propertyName, Object oldValue, Object newValue) throws PropertyVetoException
  103.     {
  104.         if (oldValue != null && oldValue.equals(newValue))
  105.         {
  106.             return;
  107.         }
  108.  
  109.         super.fireVetoableChange(propertyName, oldValue, newValue);
  110.  
  111.         java.util.Hashtable templistenerTable = null;
  112.  
  113.         synchronized (this)
  114.         {
  115.             if(listenerTable == null || !listenerTable.containsKey(propertyName))
  116.             {
  117.                 return;
  118.             }
  119.               templistenerTable = (java.util.Hashtable) listenerTable.clone();
  120.         }
  121.  
  122.         java.util.Vector listenerList;
  123.  
  124.         listenerList = (java.util.Vector)templistenerTable.get(propertyName);
  125.  
  126.         PropertyChangeEvent evt = new PropertyChangeEvent(source, propertyName, oldValue, newValue);
  127.  
  128.         try
  129.         {
  130.             for (int i = 0; i < listenerList.size(); i++)
  131.             {
  132.                 VetoableChangeListener target = (VetoableChangeListener)listenerList.elementAt(i);
  133.                 target.vetoableChange(evt);
  134.             }
  135.         }
  136.         catch (PropertyVetoException veto)
  137.         {
  138.             // Create an event to revert everyone to the old value.
  139.                evt = new PropertyChangeEvent(source, propertyName, newValue, oldValue);
  140.             for (int i = 0; i < listenerList.size(); i++)
  141.             {
  142.                 try
  143.                 {
  144.                     VetoableChangeListener target = (VetoableChangeListener)listenerList.elementAt(i);
  145.                     target.vetoableChange(evt);
  146.                 }
  147.                 catch (PropertyVetoException ex)
  148.                 {
  149.                      // We just ignore exceptions that occur during reversions.
  150.                 }
  151.             }
  152.             // And now rethrow the PropertyVetoException.
  153.             throw veto;
  154.         }
  155.     }
  156.  
  157.     /* !!! LAB !!!    05/06/97
  158.     If we want to support non-serializable listeners we will have to
  159.     implement the folowing functions and serialize out the listenerTable
  160.     HashTable on our own.
  161.  
  162.     private void writeObject(ObjectOutputStream s) throws IOException
  163.     private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException
  164.     */
  165.  
  166.     /**
  167.      * The listener table.
  168.      * @see #addVetoableChangeListener
  169.      * @see #removeVetoableChangeListener
  170.      */
  171.     protected java.util.Hashtable listenerTable;
  172.     private Object source;
  173.     private int vetoableChangeSupportSerializedDataVersion = 1;
  174. }
  175.