/** * AlarmBean.java 1.00 97/07/09 Merlin Hughes * * Copyright (c) 1997 Merlin Hughes, All Rights Reserved. * * Permission to use, copy, modify, and distribute this software * for commercial and non-commercial purposes and without fee is * hereby granted provided that this copyright notice appears in * all copies. * * http://prominence.com/ ego@merlin.org */ package org.merlin.beans.alarm; import java.io.*; import java.util.*; public class AlarmBean implements Runnable, Serializable { protected int timeout = 1000; public AlarmBean () { } public void setTimeout (int t) { timeout = t; } public int getTimeout () { return timeout; } transient protected Thread alarm; public synchronized void start () { if (alarm == null) { alarm = new Thread (this); alarm.start (); } } public synchronized void stop () { Thread t = alarm; if (t != null) { t.stop (); alarm = null; } } public void run () { try { Thread.sleep (timeout); alarm = null; dispatchAlarmCall (); } catch (InterruptedException ex) { } } protected Vector listeners = new Vector (); public void addAlarmListener (AlarmListener listener) { listeners.addElement (listener); } public void removeAlarmListener (AlarmListener listener) { listeners.removeElement (listener); } protected void dispatchAlarmCall () { AlarmEvent event = new AlarmEvent (this); Vector listeners = (Vector) this.listeners.clone (); for (int i = 0; i < listeners.size (); ++ i) ((AlarmListener) listeners.elementAt (i)).alarmCalled (event); } }