JellyBean.java Color-Annotated Source


This example illustrates constrained properties, bound properties, and icon customization.

An object with constrained properties allows other objects to veto a constrained property change. Constrained property listeners can veto a change by throwing a PropertyVetoException. JellyBean has a constrained property called PriceInCents.

A bound property notifies other objects when its value changes. Each time its value is changed, the property fires a PropertyChange event which contains the property name, the old value, and the new value. JellyBean has a bound property called Color.



package sunw.demo.jelly;

import java.awt.*;
import java.beans.*;

public class JellyBean extends Canvas {

    /**
     * Construct a smallish JellyBean.
     */
    public JellyBean() {
	setSize(60,40);
    }

    public void paint(Graphics g) {
	g.setColor(ourColor);
	g.fillArc(5, 5, 30, 30, 0, 360);
	g.fillArc(25, 5, 30, 30, 0, 360);
	g.fillRect(20, 5, 20, 30);
    }
    /** 
     * Returns the color that the jelly bean is rendered with.
     * @see #setColor
     */
    public synchronized Color getColor() {
        return ourColor;
    }
    /** 
     * Sets the color that the jelly bean is rendered with.  This is a 
     * bound property.
     * @see #getColor
     */
    public void setColor(Color newColor) {
	Color oldColor = ourColor;
        ourColor = newColor;
	changes.firePropertyChange("color", oldColor, newColor);
	repaint();
    }
    /** 
     * Returns the current price.
     * @see #setPriceInCents
     */
    public synchronized int getPriceInCents() {
        return ourPriceInCents;
    }
    /**
     * Set the price in cents unless one of the VetoableChangeListeners
     * throws a PropertyVetoException.  This is a constrained property.
     * 
     * @exception PropertyVetoException if the proposed price was vetoed
     */
    public void setPriceInCents(int newPriceInCents)
                            throws PropertyVetoException {
	int oldPriceInCents = ourPriceInCents;

	// First tell the vetoers about the change.  If anyone objects, we
	// don't catch the exception but just let if pass on to our caller.

	vetos.fireVetoableChange("priceInCents", 
				new Integer(oldPriceInCents),
				new Integer(newPriceInCents));

	// No-one vetoed, so go ahead and make the change.
 	ourPriceInCents = newPriceInCents;

	changes.firePropertyChange("priceInCents", 
				new Integer(oldPriceInCents),
				new Integer(newPriceInCents));

    }

    //----------------------------------------------------------------------
    // Methods for registering listeners:

    /**
     * The specified PropertyChangeListeners propertyChange method will
     * be called each time the value of any bound property is changed.
     * The PropertyListener object is addded to a list of PropertyChangeListeners
     * managed by the JellyBean, it can be removed with removePropertyChangeListener.
     * Note: the JavaBeans specification does not require PropertyChangeListeners
     * to run in any particular order.
     *
     * @see #removePropertyChangeListener
     * @param l the PropertyChangeListener
     */      

    public void addPropertyChangeListener(PropertyChangeListener l) {
	changes.addPropertyChangeListener(l);
    }

    /** 
     * Remove this PropertyChangeListener from the JellyBeans internal list.  
     * If the PropertyChangeListener isn't on the list, silently do nothing.
     * 
     * @see #addPropertyChangeListener
     * @param l the PropertyChangeListener
     */      

    public void removePropertyChangeListener(PropertyChangeListener l) {
	changes.removePropertyChangeListener(l);
    }

    /**
     * The specified VetoableChangeListeners vetoableChange method will
     * be called each time the value of any constrained property is changed.
     * Currently, the only constrained property is "priceInCents".
     * The VetoableChangeListener object is addded to a list of VetoableChangeListeners
     * managed by the JellyBean, it can be removed with removeVetoableChangeListener.
     * Note: the JavaBeans specification does not require VetoableChangeListeners
     * to run in any particular order.
     *
     * @see #removeVetoableChangeListener
     * @param l the VetoableChangeListener
     */      

    public void addVetoableChangeListener(VetoableChangeListener l) {
	vetos.addVetoableChangeListener(l);
    }


    /** 
     * Remove this VetoableChangeListener from the JellyBeans internal list.  
     * If the VetoableChangeListener isn't on the list, silently do nothing.
     * 
     * @see #addVetoableChangeListener
     * @param l the VetoableChangeListener
     */      

    public void removeVetoableChangeListener(VetoableChangeListener l) {
	vetos.removeVetoableChangeListener(l);
    }

    //----------------------------------------------------------------------
    // Private data fields:

    private PropertyChangeSupport changes = new PropertyChangeSupport(this);

    private VetoableChangeSupport vetos = new VetoableChangeSupport(this);

    private Color ourColor = Color.orange;
    private int ourPriceInCents = 2;
}

Java, JavaBeans, and JavaSoft are trademarks of Sun Microsystems Inc.

Copyright © 1996 Sun Microsystems, Inc., 2550 Garcia Ave., Mtn. View, CA 94043-1100 USA.
All rights reserved.