// 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 8 -- The Cooler class
package BeansBook.Simulator;
import java.awt.*;
import java.beans.*;
// the Cooler bean class definition
public class Cooler extends TemperatureModifier
implements CoolingRequestListener, VetoableChangeListener
{
// constructor
public Cooler()
{
// a cooler is a temperature modifier that operates at
// 0 degress celsius
super(0.0, "COOLER ON", "COOLER OFF",
Color.cyan, Color.yellow);
}
// handle a cooling request event
public void coolingRequest(ServiceRequestEvent evt)
{
// set the Running property based on the value returned
// by the isStart method from the event
setRunning(evt.isStart());
}
// handle a vetoable change event
public void vetoableChange(PropertyChangeEvent evt)
throws PropertyVetoException
{
// only interested in ComfortTemperature
if (evt.getPropertyName().equals("ComfortTemperature"))
{
// get the proposed temperature
Double d = (Double)evt.getNewValue();
// veto a temperature under 0 degrees Celsius
if (d.doubleValue() < 0.0)
{
throw new PropertyVetoException("Invalid Comfort Temperature", evt);
}
}
}
}
|