FileDocCategorySizeDatePackage
TemperatureModifier.javaAPI DocExample5608Sun Jan 04 21:07:36 GMT 1998BeansBook.Simulator

TemperatureModifier

public abstract class TemperatureModifier extends Label implements Runnable

Fields Summary
protected transient Thread
thrd
protected transient TemperaturePulseListener
listener
protected boolean
running
protected int
rate
protected String
onText
protected String
offText
protected Color
onColor
protected Color
offColor
protected double
temp
Constructors Summary
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();
   
Methods Summary
public synchronized voidaddTemperaturePulseListener(TemperaturePulseListener l)

      // if there is already a listener, throw an exception
      if (listener != null)
      {
         throw new TooManyListenersException();
      }
      
      // store the listener
      listener = l;
   
public intgetRate()

      return rate;
   
public booleanisRunning()

      return running;
   
private voidreadObject(java.io.ObjectInputStream stream)

      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);
      }
   
public synchronized voidremoveTemperaturePulseListener(TemperaturePulseListener l)

      // make sure this is the listener we have
      if (listener == l)
      {
         listener = null;
      }
   
public voidrun()

      // 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)
         {
         }
      }
   
public voidsetRate(int r)

      rate = r;
   
public synchronized voidsetRunning(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);
      }
   
private voidstartThread()

      // create and start the pulse thread
      thrd = new Thread(this);
      thrd.start();
   
private voidwriteObject(java.io.ObjectOutputStream stream)

      // 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);
      }