[kaffe] CVS kaffe (robilad): Resynced with GNU Classpath: swing fixlet

Kaffe CVS cvs-commits at kaffe.org
Wed Mar 2 14:39:28 PST 2005


PatchSet 5614 
Date: 2005/03/02 22:27:38
Author: robilad
Branch: HEAD
Tag: (none) 
Log:
Resynced with GNU Classpath: swing fixlet

2005-03-02  Dalibor Topic  <robilad at kaffe.org>

        Resynced with GNU Classpath.

        2005-02-26  Audrius Meskauskas  <audriusa at bluewin.ch>

        * javax/swing/Timer.java: documenting and some
          formatting.

Members: 
	ChangeLog:1.3659->1.3660 
	libraries/javalib/javax/swing/Timer.java:1.8->1.9 

Index: kaffe/ChangeLog
diff -u kaffe/ChangeLog:1.3659 kaffe/ChangeLog:1.3660
--- kaffe/ChangeLog:1.3659	Wed Mar  2 22:26:39 2005
+++ kaffe/ChangeLog	Wed Mar  2 22:27:38 2005
@@ -4,6 +4,15 @@
 
 	2005-02-26  Audrius Meskauskas  <audriusa at bluewin.ch>
 
+        * javax/swing/Timer.java: documenting and some
+          formatting.
+
+2005-03-02  Dalibor Topic  <robilad at kaffe.org>
+
+	Resynced with GNU Classpath.
+
+	2005-02-26  Audrius Meskauskas  <audriusa at bluewin.ch>
+
         * javax/swing/JTextArea.java (replaceRange):
         Fixing doc.remove(start, length) misinterpretation.
 
Index: kaffe/libraries/javalib/javax/swing/Timer.java
diff -u kaffe/libraries/javalib/javax/swing/Timer.java:1.8 kaffe/libraries/javalib/javax/swing/Timer.java:1.9
--- kaffe/libraries/javalib/javax/swing/Timer.java:1.8	Wed Mar  2 10:30:01 2005
+++ kaffe/libraries/javalib/javax/swing/Timer.java	Wed Mar  2 22:27:45 2005
@@ -1,5 +1,5 @@
 /* Timer.java --
-   Copyright (C) 2002, 2004  Free Software Foundation, Inc.
+   Copyright (C) 2002, 2004, 2005  Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -46,33 +46,70 @@
 import javax.swing.event.EventListenerList;
 
 /**
- * DOCUMENT ME!
+ * Fires one or more action events after the specified delay.
+ * @author Ronald Veldema
+ * @author Audrius Meskauskas (audriusa at Bionformatics.org) - bug fixes
+ * and documentation comments
  */
-public class Timer implements Serializable
+public class Timer
+  implements Serializable
 {
-  /** DOCUMENT ME! */
-  private static final long serialVersionUID = -1116180831621385484L;
-
-  /** DOCUMENT ME! */
-  protected EventListenerList listenerList = new EventListenerList();
-
-  // This object manages a "queue" of virtual actionEvents, maintained as a
-  // simple long counter. When the timer expires, a new event is queued,
-  // and a dispatcher object is pushed into the system event queue. When
-  // the system thread runs the dispatcher, it will fire as many
-  // ActionEvents as have been queued, unless the timer is set to
-  // coalescing mode, in which case it will fire only one ActionEvent.
+  /**
+   * The timer thread
+   */
+  private class Waker
+    extends Thread
+  {
+    /**
+     * Fires events, pausing for required intervals.
+     */
+    public void run()
+    {
+      running = true;
+      try
+        {
+          sleep(initialDelay);
 
-  /** DOCUMENT ME! */
-  private long queue;
+          queueEvent();
 
-  /** DOCUMENT ME! */
-  private Object queueLock = new Object();
+          while (running)
+            {
+              try
+                {
+                  sleep(delay);
+                }
+              catch (InterruptedException e)
+                {
+                  return;
+                }
+              queueEvent();
+
+              if (logTimers)
+                System.out.println("javax.swing.Timer -> clocktick");
+
+              if ( ! repeats)
+                break;
+            }
+          running = false;
+        }
+      catch (Exception e)
+        {
+          // The timer is no longer running.
+          running = false;
+        }
+    }
+  }
 
-  /** DOCUMENT ME! */
-  private Waker waker;
+  /**
+   * Use serialVersionUID for interoperability.
+   */
+  private static final long serialVersionUID = -1116180831621385484L;
 
-  private Runnable drainer = new Runnable() 
+  /**
+   * The encloding class, used with {@link SwingUtilities#invokeLater}
+   * to invoke the {@link #drainEvents()}.
+   */
+  private Runnable drainer = new Runnable()
     {
       public void run()
       {
@@ -81,105 +118,70 @@
     };
 
   /**
-   * DOCUMENT ME!
+   * If <code>true</code>, the timer prints a message to
+   * {@link System#out} when firing each event.
    */
-  private void queueEvent()
-  {
-    synchronized (queueLock)
-      {
-	queue++;
-	if (queue == 1)
-	  SwingUtilities.invokeLater(drainer);
-      }
-  }
+  static boolean logTimers;
 
   /**
-   * DOCUMENT ME!
+   * A field to store all listeners who are listening to this timer.
    */
-  private void drainEvents()
-  {
-    synchronized (queueLock)
-      {
-	if (isCoalesce())
-	  {
-	    if (queue > 0)
-	      fireActionPerformed();
-	  }
-	else
-	  {
-	    while (queue > 0)
-	      {
-		fireActionPerformed();
-		queue--;
-	      }
-	  }
-	queue = 0;
-      }
-  }
-
-  static boolean logTimers;
+  protected EventListenerList listenerList = new EventListenerList();
 
-  /** DOCUMENT ME! */
+  /**
+   * <code>true</code> if the timer coalesces events.
+   */
   boolean coalesce = true;
 
-  /** DOCUMENT ME! */
+  /**
+   * <code>true</code> if the timer is firing repetetive events.
+   */
   boolean repeats = true;
 
-  /** DOCUMENT ME! */
+  /**
+   * <code>true</code> if the timer is currently active, firing events
+   * as scheduled.
+   */
   boolean running;
 
-  /** DOCUMENT ME! */
-  int ticks;
-
-  /** DOCUMENT ME! */
+  /**
+   * The delay between subsequent repetetive events.
+   */
   int delay;
 
-  /** DOCUMENT ME! */
+  /**
+   * The initial delay before the first event.
+   */
   int initialDelay;
 
   /**
-   * DOCUMENT ME!
+   * The number of events that have been already fired by this timer.
+   * This is used as a numeric identifier for the next event that would
+   * be fired.
    */
-  private class Waker extends Thread
-  {
-    /**
-     * DOCUMENT ME!
-     */
-    public void run()
-    {
-      running = true;
-      try
-        {
-	  sleep(initialDelay);
-	  
-	  queueEvent();
-
-	  while (running)
-	    {
-	      try
-	        {
-		  sleep(delay);
-	        }
-	      catch (InterruptedException e)
-	        {
-		  return;
-	        }
-	      queueEvent();
-
-	      if (logTimers)
-		System.out.println("javax.swing.Timer -> clocktick");
-
-	      if (! repeats)
-		break;
-	    }
-	  running = false;
-        }
-      catch (Exception e)
-        {
-//	  System.out.println("swing.Timer::" + e);
-        }
-    }
-  }
+  int ticks;
+
+  /**
+   * Stores the thread that posts events to the queue at required time
+   * intervals.
+   */
+  private Waker waker;
+
+  /**
+   * This object manages a "queue" of virtual actionEvents, maintained as a
+   * simple long counter. When the timer expires, a new event is queued,
+   * and a dispatcher object is pushed into the system event queue. When
+   * the system thread runs the dispatcher, it will fire as many
+   * ActionEvents as have been queued, unless the timer is set to
+   * coalescing mode, in which case it will fire only one ActionEvent.
+   */
+  private long queue;
+
+  /**
+   * <code>synchronized(queueLock)</code> replaces
+   * <code>synchronized(queue)</code> that is not supported by this language.
+   */
+  private Object queueLock = new Object();
 
   /**
    * Creates a new Timer object.
@@ -191,59 +193,61 @@
   public Timer(int d, ActionListener listener)
   {
     delay = d;
-	initialDelay = d;
+    initialDelay = d;
 
     if (listener != null)
       addActionListener(listener);
   }
 
   /**
-   * DOCUMENT ME!
+   * Get the array of action listeners.
    *
-   * @param c DOCUMENT ME!
-   */
-  public void setCoalesce(boolean c)
-  {
-    coalesce = c;
-  }
-
-  /**
-   * DOCUMENT ME!
+   * @return the array of action listeners that are listening for the events,
+   * fired by this timer
    *
-   * @return DOCUMENT ME!
+   * @since 1.4
    */
-  public boolean isCoalesce()
+  public ActionListener[] getActionListeners()
   {
-    return coalesce;
+    return (ActionListener[]) listenerList.getListeners(ActionListener.class);
   }
 
   /**
-   * DOCUMENT ME!
+   * Sets whether the Timer coalesces multiple pending event firings.
+   * If the coalescing is enabled, the multiple events that have not been
+   * fired on time are replaced by the single event. The events may not
+   * be fired on time if the application is busy.
    *
-   * @param listener DOCUMENT ME!
+   * @param c <code>true</code> (default) to enable the event coalescing,
+   * <code>false</code> otherwise
    */
-  public void addActionListener(ActionListener listener)
+  public void setCoalesce(boolean c)
   {
-    listenerList.add(ActionListener.class, listener);
+    coalesce = c;
   }
 
   /**
-   * DOCUMENT ME!
+   * Checks if the Timer coalesces multiple pending event firings.
+   * If the coalescing is enabled, the multiple events that have not been
+   * fired on time are replaced by the single event. The events may not
+   * be fired on time if the application is busy.
    *
-   * @param listener DOCUMENT ME!
+   * @return <code>true</code> if the coalescing is enabled,
+   * <code>false</code> otherwise
    */
-  public void removeActionListener(ActionListener listener)
+  public boolean isCoalesce()
   {
-    listenerList.remove(ActionListener.class, listener);
+    return coalesce;
   }
 
   /**
-   * DOCUMENT ME!
-   *
-   * @param listenerType DOCUMENT ME!
+   * Get the event listeners of the given type that are listening for the
+   * events, fired by this timer.
    *
-   * @return DOCUMENT ME!
+   * @param listenerType the listener type (for example, ActionListener.class)
    *
+   * @return the array of event listeners that are listening for the events,
+   * fired by this timer
    * @since 1.3
    */
   public EventListener[] getListeners(Class listenerType)
@@ -252,42 +256,12 @@
   }
 
   /**
-   * DOCUMENT ME!
-   *
-   * @return DOCUMENT ME!
-   *
-   * @since 1.4
-   */
-  public ActionListener[] getActionListeners()
-  {
-    return (ActionListener[]) listenerList.getListeners(ActionListener.class);
-  }
-
-  /**
-   * DOCUMENT ME!
-   *
-   * @param event DOCUMENT ME!
-   */
-  protected void fireActionPerformed(ActionEvent event)
-  {
-    ActionListener[] listeners = getActionListeners();
-
-    for (int i = 0; i < listeners.length; i++)
-      listeners[i].actionPerformed(event);
-  }
-
-  /**
-   * DOCUMENT ME!
-   */
-  void fireActionPerformed()
-  {
-    fireActionPerformed(new ActionEvent(this, ticks++, "Timer"));
-  }
-
-  /**
-   * DOCUMENT ME!
+   * Set the timer logging state. If it is set to <code>true</code>, the
+   * timer prints a message to {@link System#out} when firing each
+   * action event.
    *
-   * @param lt DOCUMENT ME!
+   * @param lt <code>true</code> if logging is enabled, <code>false</code>
+   * (default value) otherwise
    */
   public static void setLogTimers(boolean lt)
   {
@@ -295,9 +269,11 @@
   }
 
   /**
-   * DOCUMENT ME!
+   * Return the logging state.
    *
-   * @return DOCUMENT ME!
+   * @return <code>true</code> if the timer is printing a message to
+   * {@link System#out}
+   * when firing each action event
    */
   public static boolean getLogTimers()
   {
@@ -305,9 +281,11 @@
   }
 
   /**
-   * DOCUMENT ME!
+   * Set the delay between firing the subsequent events.
+   * This parameter does not change the value of the initial delay before
+   * firing the first event.
    *
-   * @param d DOCUMENT ME!
+   * @param d The time gap between the subsequent events, in milliseconds
    */
   public void setDelay(int d)
   {
@@ -315,9 +293,9 @@
   }
 
   /**
-   * DOCUMENT ME!
+   * Get the delay between firing the subsequent events.
    *
-   * @return DOCUMENT ME!
+   * @return The delay between subsequent events, in milliseconds
    */
   public int getDelay()
   {
@@ -325,9 +303,12 @@
   }
 
   /**
-   * DOCUMENT ME!
+   * Set the intial delay before firing the first event since calling
+   * the {@link #start()} method. If the initial delay has not been
+   * set, it is assumed having the same value as the delay between the
+   * subsequent events.
    *
-   * @param i DOCUMENT ME!
+   * @param i the initial delay, in milliseconds
    */
   public void setInitialDelay(int i)
   {
@@ -335,9 +316,11 @@
   }
 
   /**
-   * DOCUMENT ME!
+   * Get the intial delay before firing the first event since calling
+   * the {@link #start()} method. If the initial delay has not been
+   * set, returns the same value as {@link #getDelay()}.
    *
-   * @return DOCUMENT ME!
+   * @return the initial delay before firing the first action event.
    */
   public int getInitialDelay()
   {
@@ -345,9 +328,11 @@
   }
 
   /**
-   * DOCUMENT ME!
+   * Enable firing the repetetive events.
    *
-   * @param r DOCUMENT ME!
+   * @param r <code>true</code> (default value) to fire repetetive events.
+   * <code>false</code> to fire
+   * only one event after the initial delay
    */
   public void setRepeats(boolean r)
   {
@@ -355,9 +340,11 @@
   }
 
   /**
-   * DOCUMENT ME!
+   * Check is this timer fires repetetive events.
    *
-   * @return DOCUMENT ME!
+   * @return <code>true</code> if the timer fires repetetive events,
+   * <code>false</code> if it fires
+   * only one event after the initial delay
    */
   public boolean isRepeats()
   {
@@ -365,9 +352,11 @@
   }
 
   /**
-   * DOCUMENT ME!
+   * Get the timer state.
    *
-   * @return DOCUMENT ME!
+   * @return <code>true</code> if the timer has been started and is firing
+   * the action events as scheduled. <code>false</code>
+   * if the timer is inactive.
    */
   public boolean isRunning()
   {
@@ -375,18 +364,28 @@
   }
 
   /**
-   * DOCUMENT ME!
+   * Add the action listener
+   *
+   * @param listener the action listener to add
    */
-  public void start()
+  public void addActionListener(ActionListener listener)
   {
-    if (isRunning())
-      return;
-    waker = new Waker();
-    waker.start();
+    listenerList.add(ActionListener.class, listener);
   }
 
   /**
-   * DOCUMENT ME!
+   * Remove the action listener.
+   *
+   * @param listener the action listener to remove
+   */
+  public void removeActionListener(ActionListener listener)
+  {
+    listenerList.remove(ActionListener.class, listener);
+  }
+
+  /**
+   * Cancel all pending tasks and fire the first event after the initial
+   * delay.
    */
   public void restart()
   {
@@ -395,7 +394,18 @@
   }
 
   /**
-   * DOCUMENT ME!
+   * Start firing the action events.
+   */
+  public void start()
+  {
+    if (isRunning())
+      return;
+    waker = new Waker();
+    waker.start();
+  }
+
+  /**
+   * Stop firing the action events.
    */
   public void stop()
   {
@@ -404,7 +414,70 @@
       waker.interrupt();
     synchronized (queueLock)
       {
-	queue = 0;
+        queue = 0;
+      }
+  }
+
+  /**
+   * Fire the given action event to the action listeners.
+   *
+   * @param event the event to fire
+   */
+  protected void fireActionPerformed(ActionEvent event)
+  {
+    ActionListener[] listeners = getActionListeners();
+
+    for (int i = 0; i < listeners.length; i++)
+      listeners [ i ].actionPerformed(event);
+  }
+
+  /**
+   * Fire the action event, named "Timer" and having the numeric
+   * identifier, equal to the numer of events that have been
+   * already fired before.
+   */
+  void fireActionPerformed()
+  {
+    fireActionPerformed(new ActionEvent(this, ticks++, "Timer"));
+  }
+
+  /**
+   * Fire the queued action events.
+   * In the coalescing mode, a single event is fired as a replacement
+   * for all queued events. In non coalescing mode, a series of
+   * all queued events is fired.
+   */
+  private void drainEvents()
+  {
+    synchronized (queueLock)
+      {
+        if (isCoalesce())
+          {
+            if (queue > 0)
+              fireActionPerformed();
+          }
+        else
+          {
+            while (queue > 0)
+              {
+                fireActionPerformed();
+                queue--;
+              }
+          }
+        queue = 0;
+      }
+  }
+
+  /**
+  * Post a scheduled event to the event queue.
+  */
+  private void queueEvent()
+  {
+    synchronized (queueLock)
+      {
+        queue++;
+        if (queue == 1)
+          SwingUtilities.invokeLater(drainer);
       }
   }
 }




More information about the kaffe mailing list