// 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 10 -- The BoilerCustomizer class
package BeansBook.Simulator;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
public class BoilerCustomizer extends Panel
implements Customizer, // to be a bean customizer
ItemListener, // to handle Choice list events
ActionListener // to handle action events
PropertyChangeListener // color property changes
{
// the boiler that we are customizing
protected Boiler theBoiler;
// an instance of property change support
protected PropertyChangeSupport support;
// the Choice list for the Running property
protected Choice runningChoice = new Choice();
// a text field for the Rate property
protected TextField rateField = new TextField("*******");
// a text field for the operating temperature
protected TextField tempField = new TextField("*******");
// the property editor for the running color
protected PropertyEditor colorEditor;
// the empty constructor
public BoilerCustomizer()
{
// create the instance of the support object
support = new PropertyChangeSupport(this);
// add a label for the Running property
add(new Label("Running State: "));
// add the Running property choice list
add(runningChoice);
// add a label for the Rate property
add(new Label("Pulse Rate: "));
// add the Rate property field
add(rateField);
// add a label for the operating temperature
add(new Label("Operating Temp: "));
// add the operating temperature field
add(tempField);
// add a label for the color editor
add(new Label("Running Color: "));
// find the editor and add its custom editor to the panel
colorEditor = PropertyEditorManager.findEditor(java.awt.Color.class);
add(colorEditor.getCustomEditor());
// add the choice strings to the Running choice
runningChoice.add("NO");
runningChoice.add("YES");
// become an item listener for the Choice list
runningChoice.addItemListener(this);
// become an action listener for the edit fields
rateField.addActionListener(this);
tempField.addActionListener(this);
}
public void setObject(Object o)
{
// save a reference to the bean we're customizing
theBoiler = (Boiler)o;
// get the state of the Running property and select it
// in the Choice list
if (theBoiler.getRunning().value())
{
runningChoice.select("YES");
}
else
{
runningChoice.select("NO");
}
// put the current value of the Rate property into the field
int rate = theBoiler.getRate();
rateField.setText(String.valueOf(rate));
// put the current value of the operating temperature into the field
double temp = theBoiler.retrieveOperatingTemperature();
tempField.setText(String.valueOf(temp));
// hookup the color editor
colorEditor.setValue(theBoiler.retrieveOnColor());
colorEditor.addPropertyChangeListener(this);
}
public void addPropertyChangeListener(PropertyChangeListener l)
{
// defer to the support object
support.addPropertyChangeListener(l);
}
public void removePropertyChangeListener(PropertyChangeListener l)
{
// defer to the support object
support.removePropertyChangeListener(l);
}
public Dimension preferredSize()
{
// defer to the getPreferredSize method
return getPreferredSize();
}
public Dimension getPreferredSize()
{
// we prefer a dimension of 480 by 80
return new Dimension(480, 80);
}
// handle an item state changed event
public void itemStateChanged(ItemEvent evt)
{
// if a new selection is made in the Running choice list...
if (evt.getSource() == runningChoice &&
evt.getStateChange() == ItemEvent.SELECTED)
{
// get the instance of the choice list
Choice pp = (Choice)evt.getSource();
// determine the newly selected item string
String sel = pp.getSelectedItem();
// the desired boolean state of the Running property
boolean state;
if (sel.equals("YES"))
{
state = true;
}
else
{
state = false;
}
// create a new instance of Running
Running newState = new Running(state);
// set the Running property of the boiler bean
theBoiler.setRunning(newState);
// fire a Running property change event
support.firePropertyChange("Running", null, newState);
}
}
// handle an actionPerformed event
public void actionPerformed(ActionEvent evt)
{
if (evt.getSource() == rateField)
{
// the event came from the rate field
// get the text from the field
String s = rateField.getText();
// get an integer representation of the new value
int r = Integer.valueOf(s).intValue();
// set the Rate property
theBoiler.setRate(r);
// fire an unspecified property change event
support.firePropertyChange("", null, null);
}
else
{
// the event came from the operating temp field
// get the text from the field
String s = tempField.getText();
// get a double representation of the new value
double r = Double.valueOf(s).doubleValue();
// set the new operating temperature
theBoiler.assignOperatingTemperature(r);
}
}
public void propertyChange(PropertyChangeEvent evt)
{
Color c = (Color)colorEditor.getValue();
theBoiler.assignOnColor(c);
}
}
|