From 6c5df91f83295a5aec2e38c2371cb826a0efe641 Mon Sep 17 00:00:00 2001 From: ray Date: Tue, 5 Nov 2002 01:47:32 +0000 Subject: [PATCH] Samskivert Timer: posts commands to a Controller on the awt thread, all the manueverability of javax.swing.Timer. git-svn-id: https://samskivert.googlecode.com/svn/trunk@886 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../src/java/com/samskivert/swing/Timer.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 projects/samskivert/src/java/com/samskivert/swing/Timer.java diff --git a/projects/samskivert/src/java/com/samskivert/swing/Timer.java b/projects/samskivert/src/java/com/samskivert/swing/Timer.java new file mode 100644 index 00000000..8a7e37ea --- /dev/null +++ b/projects/samskivert/src/java/com/samskivert/swing/Timer.java @@ -0,0 +1,74 @@ +// +// $Id: Timer.java,v 1.1 2002/11/05 01:47:32 ray Exp $ + +package com.samskivert.swing; + +import java.awt.Component; +import java.awt.event.ActionEvent; + +/** + * A timer that posts commands to a controller. + */ +public class Timer extends javax.swing.Timer +{ + /** + * Construct a samskivert timer. + * + * @param delay the delay between posts. + * @param source the source component to use for finding the + * appropriate controller. + * @param command the command to post. + */ + public Timer (int delay, Component source, String command) + { + super(delay, Controller.DISPATCHER); + + _event = new RepeatingActionEvent(source, command); + } + + // documentation inherited + protected void fireActionPerformed (ActionEvent e) + { + _event.setWhen(e.getWhen()); + super.fireActionPerformed(_event); + } + + /** + * We reuse this event each time so that we don't have to create a new one. + */ + protected static class RepeatingActionEvent extends ActionEvent + { + /** + * Construct a repeating action event. + */ + public RepeatingActionEvent (Object src, String cmd) + { + super(src, 0, cmd); + } + + /** + * Get the time at which this event happened. Note that this + * is provided for convenience but should not be depended on + * due to the repeating nature: the event could get re-fired + * before your controller has dealt with it. + */ + public long getWhen () + { + return _when; + } + + /** + * Set the time at which this event happened. + */ + public void setWhen (long when) + { + _when = when; + } + + /** The latest time that we were fired. */ + protected long _when; + } + + /** The event we re-use to post the command. */ + protected RepeatingActionEvent _event; +}