1 14 package org.wings.util; 15 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 19 import java.awt.event.ActionEvent ; 20 import java.awt.event.ActionListener ; 21 import java.io.Serializable ; 22 import java.util.Vector ; 23 24 30 31 35 public final class Timer 36 implements Serializable { 37 private final static Log log = LogFactory.getLog(Timer.class); 38 39 43 private long initialDelay; 44 45 48 private long delay; 49 50 53 private boolean repeats = true; 54 55 61 private boolean coalesce = true; 62 63 66 private final Vector listenerList = new Vector (); 67 68 boolean eventQueued = false; 69 70 73 private String actionCommand = null; 74 75 78 private static boolean logTimers; 79 80 long expirationTime; 84 Timer nextTimer; 85 boolean running; 86 87 96 public Timer(long delay, ActionListener listener) { 97 super(); 98 this.delay = delay; 99 this.initialDelay = delay; 100 101 addActionListener(listener); 102 } 103 104 107 public void addActionListener(ActionListener listener) { 108 listenerList.addElement(listener); 109 } 110 111 114 public void removeActionListener(ActionListener listener) { 115 listenerList.removeElement(listener); 116 } 117 118 121 public void setActionCommand(String command) { 122 actionCommand = command; 123 } 124 125 131 protected void fireActionPerformed(ActionEvent e) { 132 for (int i = listenerList.size() - 1; i >= 0; i--) { 135 ((ActionListener ) listenerList.elementAt(i)).actionPerformed(e); 136 } 137 } 138 139 142 TimerQueue timerQueue() { 143 return TimerQueue.sharedInstance(); 144 } 145 146 153 public static void setLogTimers(boolean flag) { 154 logTimers = flag; 155 } 156 157 163 public static boolean getLogTimers() { 164 return logTimers; 165 } 166 167 173 public void setDelay(long delay) { 174 TimerQueue queue; 175 176 if (delay < 0) { 177 throw new RuntimeException ("Invalid initial delay: " + delay); 178 } 179 this.delay = delay; 180 181 if (isRunning()) { 182 queue = timerQueue(); 183 queue.removeTimer(this); 184 cancelEvent(); 185 queue.addTimer(this, System.currentTimeMillis() + delay); 186 } 187 } 188 189 194 public long getDelay() { 195 return delay; 196 } 197 198 205 public void setInitialDelay(int initialDelay) { 206 if (initialDelay < 0) { 207 throw new RuntimeException ("Invalid initial delay: " + 208 initialDelay); 209 } 210 this.initialDelay = initialDelay; 211 } 212 213 218 public long getInitialDelay() { 219 return initialDelay; 220 } 221 222 226 public void setRepeats(boolean flag) { 227 repeats = flag; 228 } 229 230 236 public boolean isRepeats() { 237 return repeats; 238 } 239 240 251 public void setCoalesce(boolean flag) { 252 coalesce = flag; 253 } 254 255 261 public boolean isCoalesce() { 262 return coalesce; 263 } 264 265 271 public void start() { 272 timerQueue().addTimer(this, System.currentTimeMillis() + getInitialDelay()); 273 } 274 275 280 public boolean isRunning() { 281 return timerQueue().containsTimer(this); 282 } 283 284 290 public void stop() { 291 timerQueue().removeTimer(this); 292 cancelEvent(); 293 } 294 295 299 public void restart() { 300 stop(); 301 start(); 302 } 303 304 synchronized void cancelEvent() { 305 eventQueued = false; 306 } 307 308 synchronized void post() { 309 if (eventQueued == false) { 310 eventQueued = true; 311 if (logTimers) { 312 log.debug("Timer ringing: " + Timer.this); 313 } 314 if (eventQueued) { 315 fireActionPerformed(new ActionEvent (Timer.this, 0, this.actionCommand)); 316 cancelEvent(); 317 } 318 } 319 } 320 } 321 322 323 | Popular Tags |