// This example is from the book Developing Java Beans by Robert Englander.
// Copyright (c) 1997 O'Reilly & Associates.
// You may study, use, modify, and distribute this example for any purpose.
// This example is provided WITHOUT WARRANTY either expressed or implied.
// Chapter 4 -- The ExampleApplet4 class and supporting classes
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import BeansBook.util.*;
// the NumberLabel class
class NumberLabel extends Label
implements PropertyChangeListener
{
// the support object for bound listeners
protected PropertyChangeSupport boundSupport;
// the support object for constrained listeners
protected VetoableChangeSupport vetoSupport;
// the implementation of the Value property
protected int theValue = 15;
// constructor
public NumberLabel(String text)
{
// call the super class
super(text);
// construct the support objects
boundSupport = new PropertyChangeSupport(this);
vetoSupport = new VetoableChangeSupport(this);
}
// add a bound property listener
public void addPropertyChangeListener(PropertyChangeListener l)
{
// defer to the support object
boundSupport.addPropertyChangeListener(l);
}
// remove a bound property listener
public void removePropertyChangeListener(PropertyChangeListener l)
{
// defer to the support object
boundSupport.removePropertyChangeListener(l);
}
// add a constrained property listener
public void addVetoableChangeListener(VetoableChangeListener l)
{
// defer to the support object
vetoSupport.addVetoableChangeListener(l);
}
// remove a constrained property listener
public void removeVetoableChangeListener(VetoableChangeListener l)
{
// defer to the support object
vetoSupport.removeVetoableChangeListener(l);
}
// the get method for the Value property
public int getValue()
{
return theValue;
}
// the set method for the Value property
public void setValue(int newValue)
throws PropertyVetoException
{
// fire the change to any constrained listeners
vetoSupport.fireVetoableChange("Value", new Integer(theValue),
new Integer(newValue));
// no veto, so save the old value and then change it
Integer oldVal = new Integer(theValue);
theValue = newValue;
setText(String.valueOf(theValue));
repaint();
// fire the change to any bound listeners
boundSupport.firePropertyChange("Value", oldVal,
new Integer(theValue));
}
// handle property change events from others
public void propertyChange(PropertyChangeEvent evt)
{
// only interested in changes to Value properties
if (evt.getPropertyName().equals("Value"))
{
// just change our own property
Integer val = (Integer)evt.getNewValue();
try
{
setValue(val.intValue());
}
catch (PropertyVetoException e)
{
}
}
}
}
class Constrainer implements VetoableChangeListener
{
// handle the vetoable change event
public void vetoableChange(PropertyChangeEvent evt)
throws PropertyVetoException
{
// we constrain the value to between 10 and 20
Integer val = (Integer)evt.getNewValue();
if (val.intValue() < 10 || val.intValue() > 20)
{
throw new PropertyVetoException("Bad Value", evt);
}
}
}
public class ExampleApplet4 extends Applet
{
// the button adapter for handling button action events
protected GenericButtonAdapter adapter;
// the decrement button
protected Button decButton = new Button("<<");
// the increment button
protected Button incButton = new Button(">>");
// the constrained label
protected NumberLabel primaryLabel = new NumberLabel("*****");
// the label that mirrors the primary label
protected NumberLabel mirrorLabel = new NumberLabel("*****");
// the constraining object
protected Constrainer cnstr = new Constrainer();
// the constructor
public ExampleApplet4()
{
}
// the applet init
public void init()
{
// add the user interface elements
add(decButton);
add(incButton);
add(primaryLabel);
add(mirrorLabel);
// register the constrainer with the primary label
primaryLabel.addVetoableChangeListener(cnstr);
// register the mirroring label with the primary label
primaryLabel.addPropertyChangeListener(mirrorLabel);
// setup the button adapter
try
{
adapter = new GenericButtonAdapter(this);
adapter.registerActionEventHandler(decButton, "handleDecrement");
adapter.registerActionEventHandler(incButton, "handleIncrement");
}
catch (NoSuchMethodException e)
{
}
catch (ClassNotFoundException e)
{
}
// start the labels at different values
try
{
primaryLabel.setValue(15);
mirrorLabel.setValue(5);
}
catch (PropertyVetoException e)
{
}
}
// handle the decrement button push
public void handleDecrement(ActionEvent evt)
{
// get the current value and subtract 1
int val = primaryLabel.getValue() - 1;
// try to set the new value
try
{
primaryLabel.setValue(val);
}
catch (PropertyVetoException e)
{
}
}
// handle the increment button push
public void handleIncrement(ActionEvent evt)
{
// get the current value and add 1
int val = primaryLabel.getValue() + 1;
// try to set the new value
try
{
primaryLabel.setValue(val);
}
catch (PropertyVetoException e)
{
}
}
}
|