View Javadoc
1   package nl.tudelft.simulation.dsol.swing.introspection.gui;
2   
3   import java.awt.Component;
4   import java.lang.ref.WeakReference;
5   import java.util.ArrayList;
6   import java.util.Arrays;
7   import java.util.List;
8   import java.util.Timer;
9   import java.util.TimerTask;
10  
11  /**
12   * provides a timed update mechanism for components. <br>
13   * copyright (c) 2002-2021 <a href="https://simulation.tudelft.nl">Delft University of Technology</a>. <br>
14   * BSD-style license. See <a href="https://https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank"> DSOL License</a>. <br>
15   * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs</a>.
16   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>.
17   * @author Niels Lang.
18   * @since 1.5
19   */
20  public class UpdateTimer extends TimerTask
21  {
22      /** the tables to update. */
23      @SuppressWarnings("unchecked")
24      private WeakReference<Component>[] components = new WeakReference[0];
25  
26      /** the timer. */
27      private Timer timer = null;
28  
29      /** the period for this timer. */
30      private long period = 300L;
31  
32      /**
33       * constructs a new UpdateTimer.
34       * @param period long; the period in milliseconds
35       */
36      public UpdateTimer(final long period)
37      {
38          super();
39          this.period = period;
40      }
41  
42      /**
43       * adds a component to the list.
44       * @param component Component; the component
45       */
46      @SuppressWarnings("unchecked")
47      public synchronized void add(final Component component)
48      {
49          List<WeakReference<Component>> arrayList = new ArrayList<WeakReference<Component>>(Arrays.asList(this.components));
50          arrayList.add(new WeakReference<Component>(component));
51          this.components = arrayList.toArray(new WeakReference[arrayList.size()]);
52          // The first table added
53          if (this.timer == null)
54          {
55              this.timer = new Timer(true);
56              this.timer.scheduleAtFixedRate(this, 0L, this.period);
57          }
58      }
59  
60      /**
61       * removes a component from a list.
62       * @param component Component; the component
63       */
64      public synchronized void remove(final Component component)
65      {
66          for (int i = (this.components.length - 1); i > -1; i--)
67          {
68              if (this.components[i].get().equals(component))
69              {
70                  this.remove(this.components[i]);
71              }
72          }
73      }
74  
75      /**
76       * removes a reference from a list.
77       * @param reference WeakReference&lt;Component&gt;; the reference
78       */
79      @SuppressWarnings("unchecked")
80      private synchronized void remove(final WeakReference<Component> reference)
81      {
82          List<WeakReference<Component>> arrayList = new ArrayList<WeakReference<Component>>(Arrays.asList(this.components));
83          arrayList.remove(reference);
84          this.components = arrayList.toArray(new WeakReference[arrayList.size()]);
85          if (this.components.length == 0)
86          {
87              // The last component is removed. Let's cancel the timer
88              this.timer.cancel();
89          }
90      }
91  
92      /** {@inheritDoc} */
93      @Override
94      public void run()
95      {
96          for (int i = (this.components.length - 1); i > -1; i--)
97          {
98              Component component = this.components[i].get();
99              if (component != null)
100             {
101                 component.repaint();
102             }
103             else
104             {
105                 this.remove(this.components[i]);
106             }
107         }
108     }
109 }