From a56080769b8108a4a7e7b15db0456cbeabf7164e Mon Sep 17 00:00:00 2001 From: ray Date: Sun, 27 Jul 2003 17:31:36 +0000 Subject: [PATCH] Added a constructor that allows us to be a drop-in replacement for our superclass and implement a reasonable toString() so that if setLogTimers() is enabled, our timers will at least have reasonable output. Swing's own timers will still be cryptically logged like so: "Timer ringing: javax.swing.Timer@12022b7" git-svn-id: https://samskivert.googlecode.com/svn/trunk@1177 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../src/java/com/samskivert/swing/Timer.java | 38 +++++++++++++++++-- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/projects/samskivert/src/java/com/samskivert/swing/Timer.java b/projects/samskivert/src/java/com/samskivert/swing/Timer.java index 8a7e37ea..da89d01d 100644 --- a/projects/samskivert/src/java/com/samskivert/swing/Timer.java +++ b/projects/samskivert/src/java/com/samskivert/swing/Timer.java @@ -1,10 +1,11 @@ // -// $Id: Timer.java,v 1.1 2002/11/05 01:47:32 ray Exp $ +// $Id: Timer.java,v 1.2 2003/07/27 17:31:36 ray Exp $ package com.samskivert.swing; import java.awt.Component; import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; /** * A timer that posts commands to a controller. @@ -21,16 +22,42 @@ public class Timer extends javax.swing.Timer */ public Timer (int delay, Component source, String command) { - super(delay, Controller.DISPATCHER); + this(delay, Controller.DISPATCHER); _event = new RepeatingActionEvent(source, command); } + /** + * Construct a samskivert timer with the same functionality of a swing + * Timer except for an improved toString(). + */ + public Timer (int delay, ActionListener listener) + { + super(delay, listener); + + try { + _source = new Throwable().getStackTrace()[1].toString(); + } catch (Throwable oopsie) { + _source = ""; + } + } + + // documentation inherited + public String toString () + { + return "Timer [source=" + _source + + ((_event != null) ? ", cmd=" + _event.getActionCommand() : "") + + "]"; + } + // documentation inherited protected void fireActionPerformed (ActionEvent e) { - _event.setWhen(e.getWhen()); - super.fireActionPerformed(_event); + if (_event != null) { + _event.setWhen(e.getWhen()); + e = _event; + } + super.fireActionPerformed(e); } /** @@ -71,4 +98,7 @@ public class Timer extends javax.swing.Timer /** The event we re-use to post the command. */ protected RepeatingActionEvent _event; + + /** The line of code where this Timer was constructed. */ + protected String _source; }