// ==========================================================================
// $Id: DisplayTime.java,v 1.2 2002/09/01 15:48:30 hitoshi Exp $
// Display Time thread 
// ==========================================================================
// Copyright (C) 2002 Yamauchi, Hitoshi
//
//   Max-Planck-Institut fuer Informatik AG4
//   Stuhlsatzenhausweg 85, 66123 Saarbruecken
//   http://www.mpi-sb.mpg.de/
// 
// This software can be redistributed only under GNU copyleft.
//
// This code is based on Shin(shin@mpi-sb.mppg.de)'s TimeKeeper.java
// 
// @author Yamauchi Hitoshi »³Æâ ÀÆ
// ==========================================================================
// $Revision: 1.2 $
// $Date: 2002/09/01 15:48:30 $
// $Author: hitoshi $
// ==========================================================================
package de.mpg.mpisb.timerclock;

import  java.util.Vector;

/**
 * display time thread 
 */
public class DisplayTime extends Thread
{
  /**
   * constructor
   */
  public DisplayTime(TimerClockPanel tcp)
  {
    this.mTCP 	  = tcp;
    this.mActList = new ActionList();
    this.mActList.resetActionsForProc();
  }

  /**
   * display update thread, also treating actions
   */
  public void run()
  {
    try{
      while(true)
      {
	this.sleep(WAITMILLIS); // for stopping flicking
	synchronized(this)
	{
	  while(!(this.mIsRedisplay))
	  {
	    this.wait();
	  }
	}
	long elapsedtime = this.mTCP.updatePresentTime();
	this.mActList.procActions(elapsedtime);
	this.mTCP.repaint();
      }
    }
    catch(InterruptedException e)
    {
      System.out.println(e);
    }
  }

  /**
   * suspend redisplay
   */
  public synchronized void newSuspend()
  {
    this.mIsRedisplay = false;
  }

  /**
   * resume redisplay
   */
  public synchronized void newResume()
  {
    this.mIsRedisplay = true;
    notify();
  }

  /*============================================================
   * action related methods
   *===========================================================*/
  /**
   * remove all actions 
   */
  public synchronized void removeAllActions()
  {
    this.mActList = new ActionList();    
    this.mActList.resetActionsForProc();
  }

  /**
   * add actions : all actions are replaced with new actions
   */
  public synchronized void addAllActions(Vector actionVec)  
  {
    this.removeAllActions();
    for(int i = 0; i < actionVec.size(); i++)
    {
      this.mActList.addAction((Actionable)(actionVec.get(i)));
    }
    this.mActList.resetActionsForProc();
  }

  /**
   * peek actions 
   * @return reference of the ActionList of this object.
   */
  public ActionList peekActionList()  
  {
    return(this.mActList);
  }

  /**
   * reset Action
   */
  public void resetAction()
  {
    this.mActList.resetActionsForProc();
  }

  /*
   * private variables 
   */
  private volatile boolean  mIsRedisplay = false;
  private TimerClockPanel   mTCP 	 = null;
  // thread updates par WAITMILLIS
  private static final long WAITMILLIS   = 300;

  // Action management
  private ActionList        mActList	 = null;
}

