View Javadoc

1   /*
2    * StatisticsXYChart.java Created @ Jul 2, 2004
3    * 
4    * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
5    * Delft, the Netherlands. All rights reserved.
6    * 
7    * See for project information <a href="http://www.simulation.tudelft.nl/">
8    * www.simulation.tudelft.nl </a>.
9    * 
10   * The source code and binary code of this software is proprietary information
11   * of Delft University of Technology.
12   */
13  
14  package org.gscg.common.gui.statistics.components;
15  
16  import java.rmi.RemoteException;
17  import java.util.ArrayList;
18  import java.util.Map;
19  
20  import nl.tudelft.simulation.event.EventInterface;
21  import nl.tudelft.simulation.event.EventListenerInterface;
22  import nl.tudelft.simulation.event.EventType;
23  import nl.tudelft.simulation.event.remote.RemoteEventListener;
24  import nl.tudelft.simulation.jstats.Swingable;
25  import nl.tudelft.simulation.jstats.charts.xy.XYChart;
26  import nl.tudelft.simulation.logger.Logger;
27  
28  import org.gscg.common.gui.ActorApplicationInterface;
29  import org.gscg.common.gui.ClientInterface;
30  import org.jfree.chart.ChartPanel;
31  import org.jfree.chart.JFreeChart;
32  
33  /***
34   * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
35   * Delft, the Netherlands. All rights reserved.
36   * 
37   * See for project information <a href="http://www.simulation.tudelft.nl/">
38   * www.simulation.tudelft.nl </a>.
39   * 
40   * The source code and binary code of this software is proprietary information
41   * of Delft University of Technology.
42   * 
43   * @author <a
44   *         href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
45   *         van Houten </a>
46   * @version $Revision: 1.1 $ $Date: 2005/06/16 12:33:54 $
47   * @since 1.0.0
48   */
49  public abstract class StatisticsXYChart implements StatisticsChartInterface,
50  		EventListenerInterface
51  {
52  	/*** the actor panel */
53  	protected ActorApplicationInterface actorPanel = null;
54  
55  	/*** the xy chart */
56  	protected XYChart xyChart = null;
57  
58  	/*** the cache */
59  	protected ArrayList cache = new ArrayList();
60  
61  	/*** the event type used to update the xy chart */
62  	protected EventType eventType = null;
63  
64  	/*** the name of the chart */
65  	private String chartName = null;
66  
67  	/*** the horizontal size of the chart */
68  	private double runLength = Double.NaN;
69  
70  	/*** the range label */
71  	private String rangeLabel = null;
72  
73  	/*** the domain label */
74  	private String domainLabel = null;
75  
76  	/*** the remote event listener */
77  	private RemoteEventListener remoteEventListener = null;
78  
79  	/*** the series */
80  	protected Map series = null;
81  
82  	/*** indicates how many times this chart is opened */
83  	private int opened = 0;
84  
85  	/***
86  	 * the AreaStatistics show the statistics per area which are used in the are
87  	 * infromation frame
88  	 * 
89  	 * @param actorPanel the actor panel
90  	 * @param eventType the event type is used to update the xy chart
91  	 * @param chartName the name of the chart
92  	 * @param domainLabel the label of the domain
93  	 * @param rangeLabel the label of the range
94  	 * @param runLength the run length
95  	 */
96  	public StatisticsXYChart(final ActorApplicationInterface actorPanel,
97  			final EventType eventType, final String chartName,
98  			final String domainLabel, final String rangeLabel,
99  			final double runLength)
100 	{
101 		super();
102 		this.actorPanel = actorPanel;
103 		this.eventType = eventType;
104 		this.domainLabel = domainLabel;
105 		this.rangeLabel = rangeLabel;
106 		this.chartName = chartName;
107 		this.runLength = runLength;
108 		this.createXYChart();
109 		this.remoteEventListener = new RemoteEventListener(this);
110 
111 		// in case the chart is opened and the client closes the application
112 		// we would like to unsuscribe from the server-side event this chart has
113 		// subscribed to
114 		try
115 		{
116 			this.actorPanel.getClientSideInteractionLayerInterface()
117 					.addListener(this,
118 							ClientInterface.UNSUBSCRIBE_FROM_SERVER_EVENT);
119 		} catch (RemoteException remoteException)
120 		{
121 			Logger.severe(this, "<init>", remoteException);
122 		}
123 	}
124 
125 	/***
126 	 * create the statistics
127 	 */
128 	public void createXYChart()
129 	{
130 		this.xyChart = new XYChart(this.chartName, new double[]{0,
131 				this.runLength}, new double[]{-2, 30});
132 		this.xyChart.getChart().setBackgroundPaint(
133 				this.actorPanel.getBusinessPanelBackGroundColor());
134 		this.xyChart.getChart().getXYPlot().setBackgroundPaint(
135 				this.actorPanel.getCellBackGroundColor());
136 		this.xyChart.getChart().getXYPlot().getDomainAxis().setAutoRange(false);
137 		this.xyChart.getChart().getXYPlot().getRangeAxis().setAutoRange(true);
138 		this.xyChart.getChart().getXYPlot().getDomainAxis().setLabel(
139 				this.domainLabel);
140 		this.xyChart.getChart().getXYPlot().getRangeAxis().setLabel(
141 				this.rangeLabel);
142 
143 		// turn autorange on
144 		((ChartPanel) this.xyChart.getSwingPanel()).autoRangeBoth();
145 	}
146 
147 	/***
148 	 * @return Returns the xyChart.
149 	 */
150 	public XYChart getXyChart()
151 	{
152 		return this.xyChart;
153 	}
154 
155 	/***
156 	 * @see org.gscg.common.gui.statistics.components.StatisticsChartInterface#getChartTitle()
157 	 */
158 	public String getChartTitle()
159 	{
160 		return this.xyChart.getChart().getTitle().getText();
161 	}
162 
163 	/***
164 	 * @see org.gscg.common.gui.statistics.components.StatisticsChartInterface#getSwingable()
165 	 */
166 	public Swingable getSwingable()
167 	{
168 		return this.xyChart;
169 	}
170 
171 	/***
172 	 * @see java.lang.Object#toString()
173 	 */
174 	public String toString()
175 	{
176 		return this.xyChart.getChart().getTitle().getText();
177 	}
178 
179 	/***
180 	 * @see nl.tudelft.simulation.event.EventListenerInterface#notify(nl.tudelft.simulation.event.EventInterface)
181 	 */
182 	public abstract void notify(final EventInterface event)
183 			throws RemoteException;
184 
185 	/***
186 	 * @see org.gscg.common.gui.statistics.components.StatisticsChartInterface#getJFreeChart()
187 	 */
188 	public JFreeChart getJFreeChart()
189 	{
190 		return null;
191 	}
192 
193 	/***
194 	 * @see org.gscg.common.gui.statistics.components.StatisticsInterface#setStatus(boolean)
195 	 */
196 	public void setStatus(final boolean status)
197 	{
198 		if (status)
199 		{
200 			if (this.opened == 0)
201 			{
202 				try
203 				{
204 					this.actorPanel.getGlobalInteractionLayerInterface()
205 							.addListener(this.remoteEventListener,
206 									this.eventType, false);
207 					this.actorPanel.getGlobalInteractionLayerInterface()
208 							.getCache(null, this.eventType, false);
209 				} catch (RemoteException remoteException)
210 				{
211 					Logger.severe(this, "setStatus", remoteException);
212 				}
213 			}
214 			this.opened++;
215 		} else
216 		{
217 			if (this.opened == 1)
218 			{
219 				try
220 				{
221 					this.actorPanel.getGlobalInteractionLayerInterface()
222 							.removeListener(this.remoteEventListener,
223 									this.eventType);
224 				} catch (RemoteException remoteException)
225 				{
226 					Logger.severe(this, "setStatus", remoteException);
227 				}
228 			}
229 			this.opened--;
230 		}
231 	}
232 }