jdk/src/share/classes/javax/swing/Timer.java
author malenkov
Wed, 07 May 2008 23:20:32 +0400
changeset 466 6acd5ec503a8
parent 2 90ce3da70b43
child 1301 15e81207e1f2
permissions -rw-r--r--
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE Summary: Add the Transient annotation and support it (JSR-273) Reviewed-by: peterz, loneid
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.concurrent.atomic.AtomicBoolean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.concurrent.locks.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.swing.event.EventListenerList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Fires one or more {@code ActionEvent}s at specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * intervals. An example use is an animation object that uses a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <code>Timer</code> as the trigger for drawing its frames.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Setting up a timer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * involves creating a <code>Timer</code> object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * registering one or more action listeners on it,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * and starting the timer using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * the <code>start</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * the following code creates and starts a timer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * that fires an action event once per second
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * (as specified by the first argument to the <code>Timer</code> constructor).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * The second argument to the <code>Timer</code> constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * specifies a listener to receive the timer's action events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *  int delay = 1000; //milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *  ActionListener taskPerformer = new ActionListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *      public void actionPerformed(ActionEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *          <em>//...Perform a task...</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *  };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *  new Timer(delay, taskPerformer).start();</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * {@code Timers} are constructed by specifying both a delay parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * and an {@code ActionListener}. The delay parameter is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * to set both the initial delay and the delay between event
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * firing, in milliseconds. Once the timer has been started,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * it waits for the initial delay before firing its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * first <code>ActionEvent</code> to registered listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * After this first event, it continues to fire events
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * every time the between-event delay has elapsed, until it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * is stopped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * After construction, the initial delay and the between-event
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * delay can be changed independently, and additional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <code>ActionListeners</code> may be added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * If you want the timer to fire only the first time and then stop,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * invoke <code>setRepeats(false)</code> on the timer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * Although all <code>Timer</code>s perform their waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * using a single, shared thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * (created by the first <code>Timer</code> object that executes),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * the action event handlers for <code>Timer</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * execute on another thread -- the event-dispatching thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * This means that the action handlers for <code>Timer</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * can safely perform operations on Swing components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * However, it also means that the handlers must execute quickly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * to keep the GUI responsive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * In v 1.3, another <code>Timer</code> class was added
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * to the Java platform: <code>java.util.Timer</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * Both it and <code>javax.swing.Timer</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * provide the same basic functionality,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * but <code>java.util.Timer</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * is more general and has more features.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * The <code>javax.swing.Timer</code> has two features
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * that can make it a little easier to use with GUIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * First, its event handling metaphor is familiar to GUI programmers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * and can make dealing with the event-dispatching thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * a bit simpler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * Second, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * automatic thread sharing means that you don't have to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * take special steps to avoid spawning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * too many threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * Instead, your timer uses the same thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * used to make cursors blink,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * tool tips appear,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * and so on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * You can find further documentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * and several examples of using timers by visiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * target = "_top">How to Use Timers</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * a section in <em>The Java Tutorial.</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * For more examples and help in choosing between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * this <code>Timer</code> class and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * <code>java.util.Timer</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * <a href="http://java.sun.com/products/jfc/tsc/articles/timer/"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * target="_top">Using Timers in Swing Applications</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * an article in <em>The Swing Connection.</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * @see java.util.Timer <code>java.util.Timer</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * @author Dave Moore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
public class Timer implements Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * NOTE: all fields need to be handled in readResolve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    protected EventListenerList listenerList = new EventListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    // The following field strives to maintain the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    //    If coalesce is true, only allow one Runnable to be queued on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    //    EventQueue and be pending (ie in the process of notifying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    //    ActionListener). If we didn't do this it would allow for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    //    situation where the app is taking too long to process the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    //    actionPerformed, and thus we'ld end up queing a bunch of Runnables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    //    and the app would never return: not good. This of course implies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    //    you can get dropped events, but such is life.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    // notify is used to indicate if the ActionListener can be notified, when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    // the Runnable is processed if this is true it will notify the listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    // notify is set to true when the Timer fires and the Runnable is queued.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    // It will be set to false after notifying the listeners (if coalesce is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    // true) or if the developer invokes stop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    private transient final AtomicBoolean notify = new AtomicBoolean(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    private volatile int     initialDelay, delay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    private volatile boolean repeats = true, coalesce = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    private transient final Runnable doPostEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    private static volatile boolean logTimers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    private transient final Lock lock = new ReentrantLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    // This field is maintained by TimerQueue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    // eventQueued can also be reset by the TimerQueue, but will only ever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    // happen in applet case when TimerQueues thread is destroyed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    // access to this field is synchronized on getLock() lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    transient TimerQueue.DelayedTimer delayedTimer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    private volatile String actionCommand;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Creates a {@code Timer} and initializes both the initial delay and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * between-event delay to {@code delay} milliseconds. If {@code delay}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * is less than or equal to zero, the timer fires as soon as it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * is started. If <code>listener</code> is not <code>null</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * it's registered as an action listener on the timer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param delay milliseconds for the initial and between-event delay
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @param listener  an initial listener; can be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @see #addActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @see #setInitialDelay
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @see #setRepeats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public Timer(int delay, ActionListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        this.delay = delay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        this.initialDelay = delay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        doPostEvent = new DoPostEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (listener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            addActionListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * DoPostEvent is a runnable class that fires actionEvents to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * the listeners on the EventDispatchThread, via invokeLater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @see Timer#post
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    class DoPostEvent implements Runnable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            if (logTimers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                System.out.println("Timer ringing: " + Timer.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            if(notify.get()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                fireActionPerformed(new ActionEvent(Timer.this, 0, getActionCommand(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                                                    System.currentTimeMillis(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                                                    0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                if (coalesce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    cancelEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        Timer getTimer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            return Timer.this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * Adds an action listener to the <code>Timer</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @param listener the listener to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @see #Timer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public void addActionListener(ActionListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        listenerList.add(ActionListener.class, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Removes the specified action listener from the <code>Timer</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @param listener the listener to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    public void removeActionListener(ActionListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        listenerList.remove(ActionListener.class, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * Returns an array of all the action listeners registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * on this timer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @return all of the timer's <code>ActionListener</code>s or an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *         array if no action listeners are currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @see #addActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @see #removeActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public ActionListener[] getActionListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return (ActionListener[])listenerList.getListeners(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                ActionListener.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Notifies all listeners that have registered interest for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * notification on this event type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @param e the action event to fire
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    protected void fireActionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        // Guaranteed to return a non-null array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        // Process the listeners last to first, notifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        // those that are interested in this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        for (int i=listeners.length-2; i>=0; i-=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            if (listeners[i]==ActionListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                ((ActionListener)listeners[i+1]).actionPerformed(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * Returns an array of all the objects currently registered as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * <code><em>Foo</em>Listener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * upon this <code>Timer</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * <code><em>Foo</em>Listener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * are registered using the <code>add<em>Foo</em>Listener</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * You can specify the <code>listenerType</code> argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * with a class literal, such as <code><em>Foo</em>Listener.class</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * For example, you can query a <code>Timer</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * instance <code>t</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * for its action listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * with the following code:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * <pre>ActionListener[] als = (ActionListener[])(t.getListeners(ActionListener.class));</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * If no such listeners exist,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * this method returns an empty array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @param listenerType  the type of listeners requested;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *          this parameter should specify an interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *          that descends from <code>java.util.EventListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @return an array of all objects registered as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *          <code><em>Foo</em>Listener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *          on this timer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *          or an empty array if no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *          listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @exception ClassCastException if <code>listenerType</code> doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *          specify a class or interface that implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *          <code>java.util.EventListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @see #getActionListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @see #addActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @see #removeActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        return listenerList.getListeners(listenerType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * Returns the timer queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    private TimerQueue timerQueue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        return TimerQueue.sharedInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * Enables or disables the timer log. When enabled, a message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * is posted to <code>System.out</code> whenever the timer goes off.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @param flag  <code>true</code> to enable logging
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @see #getLogTimers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    public static void setLogTimers(boolean flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        logTimers = flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * Returns <code>true</code> if logging is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * @return <code>true</code> if logging is enabled; otherwise, false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @see #setLogTimers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    public static boolean getLogTimers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        return logTimers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * Sets the <code>Timer</code>'s between-event delay, the number of milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * between successive action events. This does not affect the initial delay
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * property, which can be set by the {@code setInitialDelay} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @param delay the delay in milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @see #setInitialDelay
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    public void setDelay(int delay) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        if (delay < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            throw new IllegalArgumentException("Invalid delay: " + delay);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            this.delay = delay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * Returns the delay, in milliseconds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * between firings of action events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @see #setDelay
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @see #getInitialDelay
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    public int getDelay() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        return delay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * Sets the <code>Timer</code>'s initial delay, the time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * in milliseconds to wait after the timer is started
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * before firing the first event. Upon construction, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * is set to be the same as the between-event delay,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * but then its value is independent and remains unaffected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * by changes to the between-event delay.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @param initialDelay the initial delay, in milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @see #setDelay
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    public void setInitialDelay(int initialDelay) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        if (initialDelay < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            throw new IllegalArgumentException("Invalid initial delay: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                                               initialDelay);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            this.initialDelay = initialDelay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * Returns the <code>Timer</code>'s initial delay.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @see #setInitialDelay
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @see #setDelay
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    public int getInitialDelay() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        return initialDelay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * If <code>flag</code> is <code>false</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * instructs the <code>Timer</code> to send only one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * action event to its listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * @param flag specify <code>false</code> to make the timer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *             stop after sending its first action event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    public void setRepeats(boolean flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        repeats = flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * Returns <code>true</code> (the default)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * if the <code>Timer</code> will send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * an action event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * to its listeners multiple times.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @see #setRepeats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    public boolean isRepeats() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        return repeats;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * Sets whether the <code>Timer</code> coalesces multiple pending
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * <code>ActionEvent</code> firings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * A busy application may not be able
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * to keep up with a <code>Timer</code>'s event generation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * causing multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * action events to be queued.  When processed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * the application sends these events one after the other, causing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * <code>Timer</code>'s listeners to receive a sequence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * events with no delay between them. Coalescing avoids this situation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * by reducing multiple pending events to a single event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * <code>Timer</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * coalesce events by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * @param flag specify <code>false</code> to turn off coalescing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    public void setCoalesce(boolean flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        boolean old = coalesce;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        coalesce = flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        if (!old && coalesce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            // We must do this as otherwise if the Timer once notified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            // in !coalese mode notify will be stuck to true and never
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            // become false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            cancelEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * Returns <code>true</code> if the <code>Timer</code> coalesces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * multiple pending action events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * @see #setCoalesce
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    public boolean isCoalesce() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        return coalesce;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * Sets the string that will be delivered as the action command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * in <code>ActionEvent</code>s fired by this timer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * <code>null</code> is an acceptable value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * @param command the action command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    public void setActionCommand(String command) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        this.actionCommand = command;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * Returns the string that will be delivered as the action command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * in <code>ActionEvent</code>s fired by this timer. May be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * <code>null</code>, which is also the default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * @return the action command used in firing events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    public String getActionCommand() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        return actionCommand;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * Starts the <code>Timer</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * causing it to start sending action events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * to its listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * @see #stop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     public void start() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        timerQueue().addTimer(this, getInitialDelay());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * Returns <code>true</code> if the <code>Timer</code> is running.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * @see #start
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    public boolean isRunning() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        return timerQueue().containsTimer(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * Stops the <code>Timer</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * causing it to stop sending action events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * to its listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * @see #start
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    public void stop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        getLock().lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            cancelEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            timerQueue().removeTimer(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            getLock().unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * Restarts the <code>Timer</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * canceling any pending firings and causing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * it to fire with its initial delay.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    public void restart() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        getLock().lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            getLock().unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * Resets the internal state to indicate this Timer shouldn't notify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * any of its listeners. This does not stop a repeatable Timer from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * firing again, use <code>stop</code> for that.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    void cancelEvent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        notify.set(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    void post() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        if (notify.compareAndSet(false, true) || !coalesce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            SwingUtilities.invokeLater(doPostEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    Lock getLock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        return lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * We have to use readResolve because we can not initialize final
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * fields for deserialized object otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    private Object readResolve() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        Timer timer = new Timer(getDelay(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        timer.listenerList = listenerList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        timer.initialDelay = initialDelay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        timer.delay = delay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        timer.repeats = repeats;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        timer.coalesce = coalesce;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        timer.actionCommand = actionCommand;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        return timer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
}