home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 July / Chip_1998-07_cd.bin / zkuste / JBuilder / BDK / Win / bdk_sep97.exe / _SETUP.1 / JellyBean.java < prev    next >
Encoding:
Java Source  |  1997-09-10  |  4.8 KB  |  150 lines

  1.  
  2. package sunw.demo.jelly;
  3.  
  4. import java.awt.*;
  5. import java.beans.*;
  6.  
  7. /**
  8.  * A simple bean with bound properties and one constrained property.
  9.  * The constrained property is "priceInCents".  VetoablePropertyChange 
  10.  * listeners can reject a proposed value for this property by throwing
  11.  * a PropertyVetoException.
  12.  * 
  13.  * @see sunw.demo.jelly.Voter
  14.  */
  15. public class JellyBean extends java.awt.Component {
  16.  
  17.     /**
  18.      * Construct a smallish JellyBean.
  19.      */
  20.     public JellyBean() {
  21.     }
  22.  
  23.     public void paint(Graphics g) {
  24.     g.setColor(ourColor);
  25.     g.fillArc(5, 5, 30, 30, 0, 360);
  26.     g.fillArc(25, 5, 30, 30, 0, 360);
  27.     g.fillRect(20, 5, 20, 30);
  28.     }
  29.  
  30.     public Dimension getPreferredSize() {
  31.     return new Dimension(60,40);
  32.     }
  33.  
  34.     /** 
  35.      * Returns the color that the jelly bean is rendered with.
  36.      * @see #setColor
  37.      */
  38.     public synchronized Color getColor() {
  39.         return ourColor;
  40.     }
  41.  
  42.     /** 
  43.      * Sets the color that the jelly bean is rendered with.  This is a 
  44.      * bound property.
  45.      * @see #getColor
  46.      */
  47.     public void setColor(Color newColor) {
  48.     Color oldColor = ourColor;
  49.         ourColor = newColor;
  50.     changes.firePropertyChange("color", oldColor, newColor);
  51.     repaint();
  52.     }
  53.  
  54.     /** 
  55.      * Returns the current price.
  56.      * @see #setPriceInCents
  57.      */
  58.     public synchronized int getPriceInCents() {
  59.         return ourPriceInCents;
  60.     }
  61.  
  62.     /**
  63.      * Set the price in cents unless one of the VetoableChangeListeners
  64.      * throws a PropertyVetoException.  This is a constrained property.
  65.      * 
  66.      * @exception PropertyVetoException if the proposed price was vetoed
  67.      */
  68.     public void setPriceInCents(int newPriceInCents)
  69.                             throws PropertyVetoException {
  70.     int oldPriceInCents = ourPriceInCents;
  71.  
  72.     // First tell the vetoers about the change.  If anyone objects, we
  73.     // don't catch the exception but just let if pass on to our caller.
  74.     vetos.fireVetoableChange("priceInCents", 
  75.                 new Integer(oldPriceInCents),
  76.                 new Integer(newPriceInCents));
  77.     // No-one vetoed, so go ahead and make the change.
  78.      ourPriceInCents = newPriceInCents;
  79.     changes.firePropertyChange("priceInCents", 
  80.                 new Integer(oldPriceInCents),
  81.                 new Integer(newPriceInCents));
  82.     }
  83.  
  84.     //----------------------------------------------------------------------
  85.     // Methods for registering listeners:
  86.  
  87.     /**
  88.      * The specified PropertyChangeListeners <b>propertyChange</b> method will
  89.      * be called each time the value of any bound property is changed.
  90.      * The PropertyListener object is addded to a list of PropertyChangeListeners
  91.      * managed by the JellyBean, it can be removed with removePropertyChangeListener.
  92.      * Note: the JavaBeans specification does not require PropertyChangeListeners
  93.      * to run in any particular order.
  94.      *
  95.      * @see #removePropertyChangeListener
  96.      * @param l the PropertyChangeListener
  97.      */      
  98.     public void addPropertyChangeListener(PropertyChangeListener l) {
  99.     changes.addPropertyChangeListener(l);
  100.     }
  101.  
  102.     /** 
  103.      * Remove this PropertyChangeListener from the JellyBeans internal list.  
  104.      * If the PropertyChangeListener isn't on the list, silently do nothing.
  105.      * 
  106.      * @see #addPropertyChangeListener
  107.      * @param l the PropertyChangeListener
  108.      */      
  109.     public void removePropertyChangeListener(PropertyChangeListener l) {
  110.     changes.removePropertyChangeListener(l);
  111.     }
  112.  
  113.     /**
  114.      * The specified VetoableChangeListeners <b>vetoableChange</b> method will
  115.      * be called each time the value of any constrained property is changed.
  116.      * Currently, the only constrained property is "priceInCents".
  117.      * The VetoableChangeListener object is addded to a list of VetoableChangeListeners
  118.      * managed by the JellyBean, it can be removed with removeVetoableChangeListener.
  119.      * Note: the JavaBeans specification does not require VetoableChangeListeners
  120.      * to run in any particular order.
  121.      *
  122.      * @see #removeVetoableChangeListener
  123.      * @param l the VetoableChangeListener
  124.      */      
  125.     public void addVetoableChangeListener(VetoableChangeListener l) {
  126.     vetos.addVetoableChangeListener(l);
  127.     }
  128.  
  129.     /** 
  130.      * Remove this VetoableChangeListener from the JellyBeans internal list.  
  131.      * If the VetoableChangeListener isn't on the list, silently do nothing.
  132.      * 
  133.      * @see #addVetoableChangeListener
  134.      * @param l the VetoableChangeListener
  135.      */      
  136.     public void removeVetoableChangeListener(VetoableChangeListener l) {
  137.     vetos.removeVetoableChangeListener(l);
  138.     }
  139.  
  140.     //----------------------------------------------------------------------
  141.     // Private data fields:
  142.  
  143.     private PropertyChangeSupport changes = new PropertyChangeSupport(this);
  144.     private VetoableChangeSupport vetos = new VetoableChangeSupport(this);
  145.  
  146.     private Color ourColor = Color.orange;
  147.     private int ourPriceInCents = 2;
  148. }
  149.  
  150.