// 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 TemperatureModifier class
package BeansBook.Simulator;
import java.awt.*;
import java.beans.*;
import java.io.*;
import java.util.*;
// class definition for the temperature modifier
public abstract class TemperatureModifier extends Label implements Runnable
{
// a thread to send temperature pulses
protected transient Thread thrd;
// the temperature pulse listener
protected transient TemperaturePulseListener listener = null;
// the running state
protected boolean running = false;
// the rate (in msecs) that pulses are generated
protected int rate = 1000;
// the text to use when running
protected String onText;
// the text to use when not running
protected String offText;
// the color to use when running
protected Color onColor;
// the color to use when not running
protected Color offColor;
// the pulse temperature
protected double temp;
public TemperatureModifier(double tmp, String onT, String offT,
Color onC, Color offC)
{
// start the label out with the text for not running
super(offT);
// save the operating temperature
temp = tmp;
// save the various text and color values
onText = onT;
offText = offT;
onColor = onC;
offColor = offC;
setBackground(offColor);
// set text alignment to center
setAlignment(Label.CENTER);
// start the pulse thread
startThread();
}
// start the pulse thread
private void startThread()
{
// create and start the pulse thread
thrd = new Thread(this);
thrd.start();
}
// write the state of the object
private void writeObject(ObjectOutputStream stream)
throws IOException
{
// defer to the default process
stream.defaultWriteObject();
// get a copy
TemperaturePulseListener lstnr = null;
synchronized (this)
{
lstnr = listener;
}
boolean bWriteNull = (lstnr == null);
if (!bWriteNull)
{
// is it serializable?
if (lstnr instanceof Serializable)
{
stream.writeObject(lstnr);
}
else
{
// write a null to the stream
bWriteNull = true;
}
}
// should we write a null?
if (bWriteNull)
{
stream.writeObject(null);
}
}
// read the state of the object
private void readObject(ObjectInputStream stream)
throws IOException
{
try
{
// defer to the default process
stream.defaultReadObject();
// read back the listener
Object obj = stream.readObject();
addTemperaturePulseListener((TemperaturePulseListener)obj);
// start the pulse thread
startThread();
}
catch (Exception e)
{
System.out.println(e);
}
}
// set the value of the Rate property
public void setRate(int r)
{
rate = r;
}
// get the value of the Rate property
public int getRate()
{
return rate;
}
// set the value of the Running property
public synchronized void setRunning(boolean b)
{
// don't bother if there is no change
if (running == b)
{
return;
}
// save the new value
running = b;
// set the appropriate label text and background color
if (running)
{
setText(onText);
setBackground(onColor);
}
else
{
setText(offText);
setBackground(offColor);
}
}
// get the value of the Running property
public boolean isRunning()
{
return running;
}
// add a unicast temperature pulse listener
public synchronized void
addTemperaturePulseListener(TemperaturePulseListener l)
throws TooManyListenersException
{
// if there is already a listener, throw an exception
if (listener != null)
{
throw new TooManyListenersException();
}
// store the listener
listener = l;
}
// remove the unicast temperature pulse listener
public synchronized void
removeTemperaturePulseListener(TemperaturePulseListener l)
{
// make sure this is the listener we have
if (listener == l)
{
listener = null;
}
}
// the run method for the pulse thread
public void run()
{
// loop forever
while(true)
{
try
{
// sleep for the time specified by rate
thrd.sleep(rate);
// if there is a listener and the service is running, then
// send the pulse event
if (listener != null && running)
{
listener.temperaturePulse(new TemperaturePulseEvent(this, temp));
}
}
catch (Exception e)
{
}
}
}
}
|