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