Listing 1: The Clock Bean
package mybeans;
import java.awt.*;
import java.util.*;
import java.text.*;
import javax.swing.*;
import java.io.*;
public class Clock extends JLabel
implements Runnable,
Serializable{
DateFormat formatter12;
DateFormat formatter24;
private boolean running;
private boolean isTwentyFourHourTime;
public Clock() {
formatter12 =
new SimpleDateFormat("h:mm:ss
a");
formatter24 =
new SimpleDateFormat("H:mm:ss");
setRunning(true);
}
public void run() {
while(running)
{
if (isTwentyFourHourTime())
this.setText(
formatter24.format(new
Date()));
else
this.setText(
formatter12.format(new
Date()));
try {
Thread.sleep(1000);
}
catch (InterruptedException
e) {}
}
}
// The running property should be hidden
public void setRunning(boolean newRunning) {
boolean oldRunning = running;
running = newRunning;
Thread t = new Thread(this);
t.start();
firePropertyChange("running",
new Boolean(oldRunning),
new Boolean(newRunning));
}
public boolean isRunning() {
return running;
}
// The twentyFourHourTime property
// should be exposed
public void setTwentyFourHourTime(
boolean
newTwentyFourHourTime) {
boolean oldTwentyFourHourTime =
isTwentyFourHourTime;
isTwentyFourHourTime = newTwenty-
FourHourTime;
firePropertyChange("twentyFourHourTime",
new Boolean(oldTwentyFourHourTime),
new Boolean(newTwentyFourHourTime));
}
public boolean isTwentyFourHourTime() {
return isTwentyFourHourTime;
}
}
Listing 2: The Property Editor
package mybeans;
import java.beans.*;
public class ClockPropertyEditor
extends PropertyEditorSupport
{
private boolean is_24 = false;
public ClockPropertyEditor() {
}
public String getAsText() {
return (is_24 ? "24 hour"
: "12
hour");
}
public Object getValue() {
return new Boolean(is_24);
}
public void setAsText(String value)
throws IllegalArgumentException {
if (value.equals("24 hour"))
setValue(new Boolean(true));
else if (value.equals("12
hour"))
setValue(new Boolean(false));
else
throw new IllegalArgumentException(
"Unrecognized value: " + value);
}
public void setValue(Object value) {
Boolean b = (Boolean)value;
is_24 = b.booleanValue();
firePropertyChange();
}
public String[] getTags() {
String [] tags = {"24 hour",
"12 hour"};
return tags;
}
}
Listing 3: The ClockBeanInfo Class
package mybeans;
import java.beans.*;
public class ClockBeanInfo
extends
SimpleBeanInfo {
public ClockBeanInfo() {
}
public
PropertyDescriptor[] getPropertyDescriptors()
{
try {
// PropertyDescriptor
for the
// "twentyFourHourTime"
property
PropertyDescriptor
pd1 =
new
PropertyDescriptor(
"twentyFourHourTime",
Clock.class,
"isTwentyFourHourTime",
"setTwentyFourHourTime");
pd1.setDisplayName("Time
Format");
pd1.setShortDescription(
"Controls
whether the time is "+
"displayed
in 12 or 24 hour mode");
pd1.setBound(true);
pd1.setPropertyEditorClass(
ClockPropertyEditor.class);
// PropertyDescriptor
to hide the
// "running" property
PropertyDescriptor
pd2 =
new
PropertyDescriptor(
"running",
Clock.class,
"isRunning",
"setRunning");
pd2.setHidden(true);
PropertyDescriptor[]
pds = new PropertyDescriptor[] {pd1, pd2};
return pds;
}
catch(IntrospectionException
ex) {
return null;
}
}
public BeanDescriptor getBeanDescriptor()
{
return new BeanDescriptor(
Clock.class,
ClockCustomizer.class);
}
public java.awt.Image getIcon(int iconKind)
{
switch (iconKind) {
case BeanInfo.ICON_COLOR_16x16:
return
loadImage("Clock16x16Color.gif");
case BeanInfo.ICON_COLOR_32x32:
return
loadImage("Clock32x32Color.gif");
case BeanInfo.ICON_MONO_16x16:
return
loadImage("Clock16x16Mono.gif");
case BeanInfo.ICON_MONO_32x32:
return
loadImage("Clock32x32Mono.gif");
}
return null;
}
public BeanInfo[] getAdditionalBeanInfo()
{
Class superclass = Clock.class.getSuperclass();
try {
BeanInfo superBeanInfo
= Introspector.getBeanInfo(superclass);
return new BeanInfo[]
{ superBeanInfo };
}
catch(IntrospectionException
ex) {
return null;
}
}
}
Listing 4: The Clock Customizer
package mybeans;
import java.awt.*;
import java.beans.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class ClockCustomizer extends JPanel
implements Customizer{
protected Clock theClockBean = null;
private JRadioButton button24 =
new JRadioButton("24
Hour Mode");
private JRadioButton button12 =
new JRadioButton("12
Hour Mode");
public ClockCustomizer() {
JLabel topLabel = new JLabel();
topLabel.setFont(
new java.awt.Font("Dialog",
1, 20));
topLabel.setHorizontalAlignment(
SwingConstants.CENTER);
topLabel.setText(
"This controls
the mode of the clock");
this.setLayout(new BorderLayout());
button24.addChangeListener(
new ChangeListener()
{
public
void stateChanged(ChangeEvent e) {
button24Changed(e);}});
this.add(topLabel, BorderLayout.NORTH);
JPanel radioPanel = new JPanel();
this.add(radioPanel, BorderLayout.CENTER);
radioPanel.add(button24, null);
radioPanel.add(button12, null);
ButtonGroup grp = new ButtonGroup();
grp.add(button12);
grp.add(button24);
button12.setSelected(true);
}
public void setObject(Object bean) {
theClockBean = (Clock)bean;
button24.setSelected(
theClockBean.isTwentyFourHourTime());
}
void button24Changed(ChangeEvent e) {
theClockBean.setTwentyFourHourTime(
button24.isSelected());
}
}