View Javadoc

1   /*
2    * CurrentRowOrColumnNumber.java Created @ Jun 16, 2004
3    * 
4    * 
5    * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
6    * Delft, the Netherlands. All rights reserved.
7    * 
8    * See for project information <a href="http://www.simulation.tudelft.nl/">
9    * www.simulation.tudelft.nl </a>.
10   * 
11   * The source code and binary code of this software is proprietary information
12   * of Delft University of Technology.
13   */
14  
15  package org.gscg.common.interactionlayer.timecontrol;
16  
17  import java.io.Serializable;
18  import java.rmi.RemoteException;
19  import java.util.Calendar;
20  
21  import nl.tudelft.simulation.event.Event;
22  import nl.tudelft.simulation.event.EventListenerInterface;
23  import nl.tudelft.simulation.event.EventType;
24  import nl.tudelft.simulation.logger.Logger;
25  
26  import org.gscg.common.interactionlayer.AnnounceInterface;
27  import org.gscg.singleuser.interactionlayer.SingleUserInteractionLayerInterface;
28  import org.gscg.singleuser.interactionlayer.dataobjects.CurrentRowOrColumnData;
29  
30  /***
31   * The CurrentRowOrColumnNumber calculates the row or column which should be
32   * selected according to the time in the simulation.
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.2 $ $Date: 2005/08/09 15:43:42 $
47   * @since 1.0.0
48   */
49  public class CurrentRowOrColumnNumber implements AnnounceInterface,
50  		Serializable
51  {
52  	/*** the serial version uid */
53  	private static final long serialVersionUID = 11L;
54  
55  	/*** the events to subscribe the owner to */
56  	private EventType[] eventsToSubscribeOwnerTo = {
57  			GlobalRowOrColumnNumber.UPDATE_CURRENT_DAY,
58  			GlobalRowOrColumnNumber.UPDATE_CURRENT_WEEK,
59  			GlobalRowOrColumnNumber.UPDATE_CURRENT_MONTH,
60  			GlobalRowOrColumnNumber.UPDATE_CURRENT_DATE,
61  			GlobalRowOrColumnNumber.TOTAL_NUMBER_OF_DAYS};
62  
63  	/*** the owner */
64  	private EventListenerInterface owner = null;
65  
66  	/***
67  	 * constructs a new CurrentRowOrColumnNumber
68  	 * 
69  	 * @param owner the owner
70  	 */
71  	public CurrentRowOrColumnNumber(final EventListenerInterface owner)
72  	{
73  		super();
74  		this.owner = owner;
75  
76  		try
77  		{
78  			// subscribe the owner to events fired from this object
79  			for (int i = 0; i < this.eventsToSubscribeOwnerTo.length; i++)
80  			{
81  				GlobalRowOrColumnNumber.getGlobalRowOrColumnNumber()
82  						.getCustomEventProducer().addListener(this.owner,
83  								this.eventsToSubscribeOwnerTo[i]);
84  				if (SingleUserInteractionLayerInterface.class
85  						.isAssignableFrom(owner.getClass()))
86  				{
87  					((SingleUserInteractionLayerInterface) this.owner)
88  							.addEventType(this.eventsToSubscribeOwnerTo[i]);
89  				}
90  			}
91  			if (SingleUserInteractionLayerInterface.class
92  					.isAssignableFrom(owner.getClass()))
93  			{
94  				// add the announce event
95  				((SingleUserInteractionLayerInterface) this.owner)
96  						.addEventTypeToAnnounceList(
97  								GlobalRowOrColumnNumber.UPDATE_CURRENT_DATES,
98  								this);
99  				((SingleUserInteractionLayerInterface) this.owner)
100 						.addEventTypeToAnnounceList(
101 								GlobalRowOrColumnNumber.TOTAL_NUMBER_OF_DAYS,
102 								this);
103 			}
104 		} catch (RemoteException remoteException)
105 		{
106 			Logger.severe(this, "<init>", remoteException);
107 		}
108 	}
109 
110 	/***
111 	 * @see org.gscg.common.interactionlayer.AnnounceInterface#announce(nl.tudelft.simulation.event.EventType,
112 	 *      boolean)
113 	 */
114 	public void announce(final EventType eventType, final boolean announce)
115 	{
116 		if (announce)
117 		{
118 			try
119 			{
120 				((SingleUserInteractionLayerInterface) this.owner)
121 						.notifyAnnounced(new Event(
122 								GlobalRowOrColumnNumber.UPDATE_CURRENT_DAY,
123 								this, new Integer(GlobalRowOrColumnNumber
124 										.getCurrentNumberDay())));
125 				((SingleUserInteractionLayerInterface) this.owner)
126 						.notifyAnnounced(new Event(
127 								GlobalRowOrColumnNumber.UPDATE_CURRENT_WEEK,
128 								this, new Integer(GlobalRowOrColumnNumber
129 										.getCurrentNumberWeek())));
130 				((SingleUserInteractionLayerInterface) this.owner)
131 						.notifyAnnounced(new Event(
132 								GlobalRowOrColumnNumber.UPDATE_CURRENT_MONTH,
133 								this, new Integer(GlobalRowOrColumnNumber
134 										.getCurrentNumberMonth())));
135 				((SingleUserInteractionLayerInterface) this.owner)
136 						.notifyAnnounced(new Event(
137 								GlobalRowOrColumnNumber.UPDATE_CURRENT_DATE,
138 								this, new CurrentRowOrColumnData(
139 										GlobalRowOrColumnNumber
140 												.getProgressCalendar().get(
141 														Calendar.DAY_OF_MONTH),
142 										GlobalRowOrColumnNumber
143 												.getProgressCalendar().get(
144 														Calendar.MONTH),
145 										GlobalRowOrColumnNumber
146 												.getProgressCalendar().get(
147 														Calendar.YEAR))));
148 				// sent the total number of days, e.g. for the statistics
149 				((SingleUserInteractionLayerInterface) this.owner)
150 						.notifyAnnounced(new Event(
151 								GlobalRowOrColumnNumber.TOTAL_NUMBER_OF_DAYS,
152 								this, new Integer(GlobalRowOrColumnNumber
153 										.getNumberOfDays())));
154 			} catch (RemoteException remoteException)
155 			{
156 				Logger.severe(this, "announce", remoteException);
157 			}
158 		}
159 	}
160 }