View Javadoc

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