View Javadoc

1   /*
2    * @(#) UpdateTimer.java Apr 16, 2004 Copyright (c) 2002-2005 Delft University
3    * of Technology Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights
4    * reserved. This software is proprietary information of Delft University of
5    * Technology The code is published under the Lesser General Public License
6    */
7   package nl.tudelft.simulation.introspection.gui;
8   
9   import java.awt.Component;
10  import java.lang.ref.WeakReference;
11  import java.util.ArrayList;
12  import java.util.Arrays;
13  import java.util.List;
14  import java.util.Timer;
15  import java.util.TimerTask;
16  
17  /***
18   * provides a timed update mechanism for components
19   * <p>
20   * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft
21   * University of Technology </a>, the Netherlands. <br>
22   * See for project information <a
23   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
24   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
25   * General Public License (LGPL) </a>, no warranty.
26   * 
27   * @author <a href="http://www.peter-jacobs.com/index.htm">Peter Jacobs </a>
28   * @version 1.2 Apr 16, 2004
29   * @since 1.5
30   */
31  public class UpdateTimer extends TimerTask
32  {
33      /*** the tables to update */
34      @SuppressWarnings("unchecked")
35      private WeakReference<Component>[] components = new WeakReference[0];
36  
37      /*** the timer */
38      private Timer timer = null;
39  
40      /*** the period for this timer */
41      private long period = 300L;
42  
43      /***
44       * constructs a new UpdateTimer
45       * 
46       * @param period the period in milliseconds
47       */
48      public UpdateTimer(final long period)
49      {
50          super();
51          this.period = period;
52      }
53  
54      /***
55       * adds a component to the list.
56       * 
57       * @param component the component
58       */
59      @SuppressWarnings("unchecked")
60      public synchronized void add(final Component component)
61      {
62          List<WeakReference<Component>> arrayList = new ArrayList<WeakReference<Component>>(
63                  Arrays.asList(this.components));
64          arrayList.add(new WeakReference<Component>(component));
65          this.components = arrayList
66                  .toArray(new WeakReference[arrayList.size()]);
67          // The first table added
68          if (this.timer == null)
69          {
70              this.timer = new Timer(true);
71              this.timer.scheduleAtFixedRate(this, 0L, this.period);
72          }
73      }
74  
75      /***
76       * removes a component from a list
77       * 
78       * @param component the component
79       */
80      public synchronized void remove(final Component component)
81      {
82          for (int i = (this.components.length - 1); i > -1; i--)
83          {
84              if (this.components[i].get().equals(component))
85              {
86                  this.remove(this.components[i]);
87              }
88          }
89      }
90  
91      /***
92       * removes a reference from a list
93       * 
94       * @param reference the reference
95       */
96      @SuppressWarnings("unchecked")
97      private synchronized void remove(final WeakReference reference)
98      {
99          List<WeakReference<Component>> arrayList = new ArrayList<WeakReference<Component>>(
100                 Arrays.asList(this.components));
101         arrayList.remove(reference);
102         this.components = arrayList
103                 .toArray(new WeakReference[arrayList.size()]);
104         if (this.components.length == 0)
105         {
106             // The last component is removed. Let's cancel the timer
107             this.timer.cancel();
108         }
109     }
110 
111     /***
112      * @see java.lang.Runnable#run()
113      */
114     @Override
115     public void run()
116     {
117         for (int i = (this.components.length - 1); i > -1; i--)
118         {
119             Component component = this.components[i].get();
120             if (component != null)
121             {
122                 component.repaint();
123             } else
124             {
125                 this.remove(this.components[i]);
126             }
127         }
128     }
129 }