

import java.awt.*;
import java.awt.event.*;
import java.util.*;


/** 
 * This is a label which displays a spining bar just using
 * ascii characters.
 *
 * @author matth3wbishop<at>yahoo!<dot>com
 */

public class WindMillLabel extends Label implements Runnable
{

  //--------------------------------------------
  private StringBuffer text;
  //-------------------------------------------------
  private Date startTime;
  //-------------------------------------------------
  private Date stopTime;
  //-------------------------------------------------
  Color backgroundColour;
  //-------------------------------------------------
  private Thread spinThread;
  //-------------------------------------------------
  private boolean spin;
  //-------------------------------------------------
  private int spinDelay;


  //-------------------------------------------------
  /**  */
   public WindMillLabel() 
   {
     initialize();
   } 

  //-------------------------------------------------
  /**  */
   public WindMillLabel(String sText) 
   {
     super(sText);
     initialize();
     this.text.append(sText);
   } 

  //-------------------------------------------------
  /** set the member variables to something */
   public void initialize() 
   {
     this.spin = false;
     this.spinDelay = 100;
     this.text = new StringBuffer("");
   } //-- const:()

  //--------------------------------------------
  public void spin()
  {
    this.spin = true;
    this.spinThread = new Thread(this);
    this.spinThread.start();
  }

  //--------------------------------------------
  public void stopSpinning()
  {
    this.spin = false;
  }

  //--------------------------------------------
  public void setLabel(String sText)
  {
    this.setText(sText);
    this.text.setLength(0);
    this.text.append(sText);
  }

  //--------------------------------------------
  /** the delay in milliseconds between animation frames */
  public void setSpeed(int iSpinDelay)
  {
    if (iSpinDelay < 1)
     { return; }

    this.spinDelay = iSpinDelay;
  }

  //--------------------------------------------
  public void run()
  {
    while (this.spin)
    {
      try
      {
       Thread.sleep(this.spinDelay);
       this.setText(this.text + " /");
       Thread.sleep(this.spinDelay);
       this.setText(this.text + " -");
       Thread.sleep(this.spinDelay);
       this.setText(this.text + " \\");
       Thread.sleep(this.spinDelay);
       this.setText(this.text + " |");
       Thread.sleep(this.spinDelay);
       this.setText(this.text + " /");
       Thread.sleep(this.spinDelay);
       this.setText(this.text + " -");
       Thread.sleep(this.spinDelay);
       this.setText(this.text + " \\");
       Thread.sleep(this.spinDelay);
       this.setText(this.text + " |");
      }
      catch (InterruptedException e)
      {
        return;
      }
    } //-- while

    this.setText(this.text.toString());
  } //-- m: run

  //--------------------------------------------
  /** A main method to test */
  public static void main(String[] args) throws Exception
  {
    StringBuffer sbUsageMessage = new StringBuffer("");
    sbUsageMessage.append("usage: java WindMillLabel .");

    /*
    if (args.length == 0)
    {	    
      System.out.println(sbUsageMessage);
      System.exit(-1);
    }
    */
    String sLabelText;

    if (args.length == 1)
    {
      sLabelText = args[0];  
    }
    else
    {
      sLabelText = "windmill";
    }

    Frame frame = new Frame("Testing WindMillLabel");
    frame.setBackground(new Color(0, 33, 0));

    WindMillLabel windMill = new WindMillLabel(sLabelText);
    Font displayFont = new Font("Monospaced", Font.PLAIN, 18);

    windMill.setForeground(new Color(255, 255, 255));
    windMill.setFont(displayFont);
    windMill.setLabel(sLabelText);

    frame.add("Center", windMill);
    frame.pack();
    //-- exit when the window is closed
    //--
    frame.addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent ee)
      {
        System.exit(0);
      }
    });


    frame.show();
    try
    {
      Thread.sleep(2000);
      windMill.spin();
      Thread.sleep(4000);
      windMill.setLabel("stopped");
      windMill.stopSpinning();
      Thread.sleep(4000);
      windMill.setLabel("starting");
      windMill.spin();
    }
    catch (InterruptedException e)
    {
        return;
    }


  } //-- main()    
    
} //-- class: WindMillLabel


