View Javadoc

1   /*
2    * @(#) DemandChangedStatisticsXYChart.java Jan 2, 2005
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  package org.gscg.common.gui.statistics.components;
14  
15  import java.rmi.RemoteException;
16  import java.util.Iterator;
17  import java.util.Map;
18  import java.util.Set;
19  
20  import nl.tudelft.simulation.event.EventInterface;
21  import nl.tudelft.simulation.event.EventType;
22  import nl.tudelft.simulation.jstats.charts.xy.XYDataset;
23  import nl.tudelft.simulation.jstats.charts.xy.XYSeries;
24  
25  import org.gscg.common.gui.ActorApplicationInterface;
26  import org.gscg.common.gui.ClientInterface;
27  import org.gscg.common.gui.exceptions.ReceivedUnknownEventException;
28  import org.gscg.common.gui.statistics.data.DemandStatisticsChangedData;
29  
30  /***
31   * An implementation of the XYChartStatistics suitable for demand statistics.
32   * <p>
33   * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
34   * Delft, the Netherlands. All rights reserved.
35   * 
36   * See for project information <a href="http://www.simulation.tudelft.nl/">
37   * www.simulation.tudelft.nl </a>.
38   * 
39   * The source code and binary code of this software is proprietary information
40   * of Delft University of Technology.
41   * 
42   * @author <a
43   *         href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
44   *         van Houten </a>
45   * @version $Revision: 1.1 $ $Date: 2005/06/16 12:33:54 $
46   * @since 1.0.0 <br>
47   */
48  public class DemandChangedStatisticsXYChart extends StatisticsXYChart
49  {
50  	/***
51  	 * constructs a new DemandChangedStatisticsXYChart
52  	 * 
53  	 * @param application the application
54  	 * @param eventType the event type is used to update the xy chart
55  	 * @param chartName the name of the chart
56  	 * @param domainLabel the label of the domain
57  	 * @param rangeLabel the label of the range
58  	 * @param runLength the run length
59  	 */
60  	public DemandChangedStatisticsXYChart(
61  			final ActorApplicationInterface application,
62  			final EventType eventType, final String chartName,
63  			final String domainLabel, final String rangeLabel,
64  			final double runLength)
65  	{
66  		super(application, eventType, chartName, domainLabel, rangeLabel,
67  				runLength);
68  	}
69  
70  	/***
71  	 * @see org.gscg.common.gui.statistics.components.StatisticsXYChart#notify(nl.tudelft.simulation.event.EventInterface)
72  	 */
73  	public void notify(final EventInterface event) throws RemoteException
74  	{
75  		if (event.getType().equals(this.eventType))
76  		{
77  			if (event.getContent() instanceof DemandStatisticsChangedData)
78  			{
79  				DemandStatisticsChangedData data = (DemandStatisticsChangedData) event
80  						.getContent();
81  				if (this.series != null)
82  				{
83  					XYSeries series = (XYSeries) this.series.get(data
84  							.getProductName());
85  					if (series == null)
86  					{
87  						boolean serieFound = false;
88  						for (Iterator it = this.series.keySet().iterator(); it
89  								.hasNext();)
90  						{
91  							Object key = it.next();
92  							Set set = (Set) this.series.get(key);
93  
94  							for (Iterator setIterator = set.iterator(); setIterator
95  									.hasNext();)
96  							{
97  								XYSeries serie = (XYSeries) setIterator.next();
98  
99  								if (serie.getSeriesName().equalsIgnoreCase(
100 										data.getProductName()))
101 								{
102 									serie.notify(data.getTimedEvent());
103 									serieFound = true;
104 								}
105 							}
106 						}
107 						if (!serieFound)
108 						{
109 							// we didn't find a serie for the product name
110 							// let's get a general update; this will give us all
111 							// the required data
112 							this.actorPanel
113 									.getGlobalInteractionLayerInterface()
114 									.getCache(null, this.eventType, false);
115 							return;
116 						}
117 					} else
118 					{
119 						series.notify(data.getTimedEvent());
120 					}
121 					this.xyChart.getSwingPanel().repaint();
122 				} else
123 				// a new series has been added
124 				{
125 					XYDataset set = (XYDataset) this.xyChart.getChart()
126 							.getXYPlot().getDataset();
127 					XYSeries serie = new XYSeries(
128 							"Demand for " + data.getProductName() + " of "
129 									+ "new actor name..",
130 							nl.tudelft.simulation.jstats.charts.xy.XYChart.XLINEAR_YLINEAR,
131 							super.actorPanel
132 									.getGlobalInteractionLayerInterface()
133 									.getTotalNumberOfDays());
134 					set.addSeries(serie);
135 					this.xyChart.getChart().getXYPlot().setDataset(set);
136 				}
137 			} else
138 			// set the chart
139 			{
140 				XYDataset set = new XYDataset();
141 				this.series = (Map) event.getContent();
142 				for (Iterator it = this.series.keySet().iterator(); it
143 						.hasNext();)
144 				{
145 					Object key = it.next();
146 					try
147 					{
148 						set.addSeries((XYSeries) this.series.get(key));
149 					} catch (ClassCastException classCastException)
150 					{
151 						Set setXYSeries = (Set) this.series.get(key);
152 						for (Iterator setXYSeriesIterator = setXYSeries
153 								.iterator(); setXYSeriesIterator.hasNext();)
154 						{
155 							set
156 									.addSeries((XYSeries) setXYSeriesIterator
157 											.next());
158 						}
159 					}
160 				}
161 				this.xyChart.getChart().getXYPlot().setDataset(set);
162 			}
163 			return;
164 		}
165 		if (event.getType().equals(
166 				ClientInterface.UNSUBSCRIBE_FROM_SERVER_EVENT))
167 		{
168 			super.setStatus(false);
169 			return;
170 		}
171 		new ReceivedUnknownEventException(this, "notify", event.getType());
172 	}
173 }