View Javadoc

1   /*
2    * @(#) GamingPersistent.java Oct 25, 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  package org.gscg.common.gui.statistics.components;
14  
15  import java.rmi.RemoteException;
16  
17  import nl.tudelft.simulation.event.Event;
18  import nl.tudelft.simulation.event.EventInterface;
19  import nl.tudelft.simulation.event.EventListenerInterface;
20  import nl.tudelft.simulation.event.EventType;
21  import nl.tudelft.simulation.event.remote.RemoteEventListener;
22  import nl.tudelft.simulation.logger.Logger;
23  
24  import org.gscg.common.gui.ActorApplicationInterface;
25  import org.gscg.common.gui.ClientInterface;
26  import org.gscg.common.gui.exceptions.ReceivedUnknownEventException;
27  import org.gscg.common.interactionlayer.dataobjects.GamingPersistentData;
28  
29  /***
30   * A GamingPersistent is used to enable the setting of values upon an update of
31   * the user interface.
32   * 
33   * <p>
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 <br>
48   */
49  public class GamingPersistent implements StatisticsInterface,
50  		EventListenerInterface
51  {
52  	/*** the serial version uid */
53  	private static final long serialVersionUID = 11L;
54  
55  	/*** the remote event listener */
56  	private RemoteEventListener remoteEventListener = null;
57  
58  	/*** the event type of interest for this persistent */
59  	private EventType eventType = null;
60  
61  	/*** the application */
62  	private ActorApplicationInterface application = null;
63  
64  	/*** the customized persistent */
65  	private CustomizedPersistent persistent = null;
66  
67  	/*** indicates how many times this chart is opened */
68  	private int opened = 0;
69  
70  	/*** the update event type */
71  	private EventType updateEventType = null;
72  
73  	/*** the owner */
74  	private EventListenerInterface owner = null;
75  
76  	/***
77  	 * constructs a new GamingPersistent
78  	 * 
79  	 * @param application the application
80  	 * @param description the description
81  	 * @param eventType the eventtype of interest
82  	 * @param updateEventType the update event type
83  	 * @param owner the owner
84  	 */
85  	public GamingPersistent(final ActorApplicationInterface application,
86  			final String description, final EventType eventType,
87  			final EventType updateEventType, final EventListenerInterface owner)
88  
89  	{
90  		super();
91  		this.owner = owner;
92  		this.updateEventType = updateEventType;
93  		this.application = application;
94  		this.eventType = eventType;
95  		this.remoteEventListener = new RemoteEventListener(this);
96  		this.persistent = new CustomizedPersistent(description);
97  
98  		// in case the chart is opened and the client closes the application
99  		// we would like to unsuscribe from the server-side event this chart has
100 		// subscribed to
101 		try
102 		{
103 			this.application.getClientSideInteractionLayerInterface()
104 					.addListener(this,
105 							ClientInterface.UNSUBSCRIBE_FROM_SERVER_EVENT);
106 		} catch (RemoteException remoteException)
107 		{
108 			Logger.severe(this, "<init>", remoteException);
109 		}
110 	}
111 
112 
113 	/***
114 	 * @see nl.tudelft.simulation.event.EventListenerInterface#notify(nl.tudelft.simulation.event.EventInterface)
115 	 */
116 	public void notify(final EventInterface event)
117 	{
118 		if (event.getType().equals(this.eventType))
119 		{
120 			GamingPersistentData data = (GamingPersistentData) event
121 					.getContent();
122 			this.persistent.setValues(data);
123 
124 			try
125 			{
126 				// send an event to update the graphical user interface
127 				this.owner.notify(new Event(this.updateEventType, this, null));
128 			} catch (RemoteException remoteException)
129 			{
130 				Logger.severe(this, "notify", remoteException);
131 			}
132 			return;
133 		}
134 		if (event.getType().equals(
135 				ClientInterface.UNSUBSCRIBE_FROM_SERVER_EVENT))
136 		{
137 			this.setStatus(false);
138 			return;
139 		}
140 		new ReceivedUnknownEventException(this, "notify", event.getType());
141 	}
142 
143 	/***
144 	 * @see org.gscg.common.gui.statistics.components.StatisticsInterface#setStatus(boolean)
145 	 */
146 	public void setStatus(final boolean status)
147 	{
148 		if (status)
149 		{
150 			if (this.opened == 0)
151 			{
152 				try
153 				{
154 					this.application.getGlobalInteractionLayerInterface()
155 							.addListener(this.remoteEventListener,
156 									this.eventType, false);
157 					this.application.getGlobalInteractionLayerInterface()
158 							.getCache(null, this.eventType, false);
159 					System.out
160 							.println("GamingPersistent added listener for event type: "
161 									+ this.eventType);
162 				} catch (RemoteException remoteException)
163 				{
164 					Logger.severe(this, "setStatus", remoteException);
165 				}
166 			}
167 			this.opened++;
168 		} else
169 		{
170 			if (this.opened == 1)
171 			{
172 				try
173 				{
174 					this.application.getGlobalInteractionLayerInterface()
175 							.removeListener(this.remoteEventListener,
176 									this.eventType);
177 					System.out
178 							.println("GamingPersistent removed listener for event type: "
179 									+ this.eventType);
180 
181 				} catch (RemoteException remoteException)
182 				{
183 					Logger.severe(this, "setStatus", remoteException);
184 				}
185 			}
186 			this.opened--;
187 		}
188 	}
189 
190 
191 	/***
192 	 * @return returns the customized persistent
193 	 */
194 	public CustomizedPersistent getPersistent()
195 	{
196 		return this.persistent;
197 	}
198 
199 	/***
200 	 * A GamingPersistent is used to enable the setting of values upon an
201 	 * initialization of the user interface.
202 	 * 
203 	 * <p>
204 	 * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628
205 	 * BX Delft, the Netherlands. All rights reserved.
206 	 * 
207 	 * See for project information <a href="http://www.simulation.tudelft.nl/">
208 	 * www.simulation.tudelft.nl </a>.
209 	 * 
210 	 * The source code and binary code of this software is proprietary
211 	 * information of Delft University of Technology.
212 	 * 
213 	 * @author <a
214 	 *         href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
215 	 *         van Houten </a>
216 	 * @version $Revision: 1.1 $ $Date: 2005/06/16 12:33:54 $
217 	 * @since 1.0.0
218 	 */
219 	private class CustomizedPersistent extends
220 			nl.tudelft.simulation.jstats.statistics.Persistent
221 	{
222 		/*** the serial version uid */
223 		private static final long serialVersionUID = 11L;
224 
225 		/***
226 		 * constructs a new GamingPersistent
227 		 * 
228 		 * @param description the description
229 		 */
230 		public CustomizedPersistent(final String description)
231 
232 		{
233 			super(description);
234 			super.initialize();
235 		}
236 
237 		/***
238 		 * Method setValues sets the values of the gaming persistent.
239 		 * 
240 		 * @param data the gaming persistent data contains the data
241 		 */
242 		public void setValues(final GamingPersistentData data)
243 		{
244 			super.min = data.getMin();
245 			super.max = data.getMax();
246 			super.sampleMean = data.getSampleMean();
247 			super.sum = data.getSum();
248 			super.varianceSum = data.getVarianceSum();
249 			super.n = data.getN();
250 		}
251 	}
252 }