import java.io.Serializable;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class EventClockBean implements Serializable {
	// how to format the time for this locale
	java.text.DateFormat f =
		java.text.DateFormat.getTimeInstance(
			java.text.DateFormat.MEDIUM);
	boolean keepRunning = true;
	protected transient Thread t;
	protected ActionListener listener;

	public void addActionListener( ActionListener ae ) { listener = ae;}
	public void removeActionListener( ActionListener ae ) { listener=null;}

	public EventClockBean() { 
		t = new Thread( new Runnable() {
			public void run() { runClock();}} );
		t.start();
	}
	protected void runClock() {
		while( keepRunning ) {
			// get current time
			String time = f.format(new java.util.Date());
			// System.out.println( time );
			if (listener != null) {
				ActionEvent ae = new ActionEvent(this,0,time);
				listener.actionPerformed( ae );
			}
			try {
				Thread.sleep( 1000 );
			} catch (InterruptedException e) {}
		}
	}
	public void terminate() { keepRunning = false; }
	public void stopClock() { t.suspend(); }
	public void startClock() { t.resume(); }
}

