View Javadoc

1   /*
2    * @(#) DSOLApplication.java Oct 24, 2003
3    * 
4    * Copyright (c) 2002-2005 Delft University of Technology Jaffalaan 5, 2628 BX
5    * Delft, the Netherlands. All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the Lesser General Public License
9    */
10  package nl.tudelft.simulation.dsol.gui;
11  
12  import java.io.File;
13  import java.io.FileInputStream;
14  import java.io.FileOutputStream;
15  import java.io.IOException;
16  import java.net.URL;
17  import java.util.Arrays;
18  import java.util.Comparator;
19  import java.util.Iterator;
20  import java.util.Properties;
21  import java.util.TreeSet;
22  
23  import nl.tudelft.simulation.dsol.experiment.Experiment;
24  import nl.tudelft.simulation.dsol.gui.applets.DSOLSecurityManager;
25  import nl.tudelft.simulation.dsol.gui.windows.DSOLFrame;
26  import nl.tudelft.simulation.dsol.simulators.Animator;
27  import nl.tudelft.simulation.event.EventProducer;
28  import nl.tudelft.simulation.language.io.URLResource;
29  import nl.tudelft.simulation.logger.Logger;
30  import nl.tudelft.simulation.logger.handlers.MemoryHandler;
31  
32  /***
33   * The DSOL Application <br>
34   * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft
35   * University of Technology </a>, the Netherlands. <br>
36   * See for project information <a
37   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
38   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
39   * General Public License (LGPL) </a>, no warranty.
40   * 
41   * @version $Revision$ $Date$
42   * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja">Peter Jacobs </a>
43   */
44  public class DSOLApplication extends EventProducer implements
45  		DSOLApplicationInterface
46  {
47  	/*** the experiment */
48  	protected Experiment experiment = null;
49  
50  	/*** the properties */
51  	protected Properties properties = new Properties();
52  
53  	/*** the recents */
54  	protected TreeSet recents = new TreeSet(new RecentComparator());
55  
56  	/***
57  	 * constructs a new DSOLApplicationInterface
58  	 * 
59  	 * @param navigation an URL to an xml-based navigation structure.
60  	 */
61  	public DSOLApplication(final URL navigation)
62  	{
63  		super();
64  		System.setSecurityManager(new DSOLSecurityManager());
65  		// Runtime.runFinalizersOnExit(true);
66  		Logger.setDefaultHandler(MemoryHandler.class);
67  		try
68  		{
69  			this.readProperties(this.properties);
70  			new DSOLFrame(this, navigation);
71  		} catch (Exception exception)
72  		{
73  			Logger.warning(this, "DSOLApplicationInterface", exception);
74  		}
75  	}
76  
77  	/***
78  	 * @see java.lang.Object#finalize()
79  	 */
80  	protected void finalize() throws Throwable
81  	{
82  		this.writeProperties(this.properties);
83  		super.finalize();
84  	}
85  
86  	/***
87  	 * returns the experiment
88  	 * 
89  	 * @return Experiment
90  	 */
91  	public Experiment getExperiment()
92  	{
93  		return this.experiment;
94  	}
95  
96  	/***
97  	 * sets the experiment
98  	 * 
99  	 * @param experiment the experiment
100 	 */
101 	public void setExperiment(final Experiment experiment)
102 	{
103 		this.experiment = experiment;
104 		if (this.experiment != null && this.experiment.getUrl() != null)
105 		{
106 			this.recents.add(new Recent(experiment.getUrl().toString(), System
107 					.currentTimeMillis()));
108 			Object[] elements = this.recents.toArray();
109 			this.recents.clear();
110 			this.recents.addAll(Arrays.asList(elements));
111 			try
112 			{
113 				this.writeProperties(this.properties);
114 
115 				this.experiment.setSimulator(new Animator());
116 				this.experiment.start();
117 				this.experiment.getSimulator().stop();
118 			} catch (Exception exception)
119 			{
120 				Logger.warning(this, "setExperiment", exception);
121 			}
122 		}
123 		this.fireEvent(EXPERIMENT_CHANGED_EVENT, experiment);
124 	}
125 
126 	/***
127 	 * returns the properties
128 	 * 
129 	 * @return Properties
130 	 */
131 	public Properties getProperties()
132 	{
133 		return this.properties;
134 	}
135 
136 	/***
137 	 * executes the dsol control panel
138 	 * 
139 	 * @param args the command-line arguments
140 	 */
141 	public static void main(final String[] args)
142 	{
143 		URL navigation = null;
144 		if (args.length != 0)
145 		{
146 			navigation = URLResource.getResource(args[0]);
147 		} else if (System.getProperty("dsol.navigation") != null)
148 		{
149 			navigation = URLResource.getResource(System
150 					.getProperty("dsol.navigation"));
151 		}
152 		if (navigation == null)
153 		{
154 			navigation = URLResource.getResource("/navigation.xml");
155 		}
156 		new DSOLApplication(navigation);
157 	}
158 
159 	/***
160 	 * reads the properties
161 	 * 
162 	 * @param properties the properties to read
163 	 * @throws IOException on failure
164 	 */
165 	private void readProperties(final Properties properties) throws IOException
166 	{
167 		properties.load(URLResource.getResourceAsStream("/dsol.properties"));
168 		String tempPath = System.getProperty("java.io.tmpdir");
169 		String fileName = this.getClass().getName() + "_dsol.properties";
170 		File file = new File(tempPath, fileName);
171 		if (file.exists() && !("true".equals(properties.get("edited"))))
172 		{
173 			FileInputStream stream = new FileInputStream(file);
174 			properties.load(stream);
175 			stream.close();
176 		}
177 		int i = 0;
178 		String value = null;
179 		while ((value = properties.getProperty("recent[" + i + "]")) != null)
180 		{
181 			String[] elements = value.split("!#!");
182 			this.recents.add(new Recent(elements[0], Long
183 					.parseLong(elements[1])));
184 			i++;
185 		}
186 	}
187 
188 	/***
189 	 * reads the properties
190 	 * 
191 	 * @param properties the properties to read
192 	 * @throws IOException on failure
193 	 */
194 	private void writeProperties(final Properties properties)
195 			throws IOException
196 	{
197 		int loop = 0;
198 		for (Iterator i = this.recents.iterator(); i.hasNext();)
199 		{
200 			Recent recent = (Recent) i.next();
201 			if (loop <= 5)
202 			{
203 				this.properties.put("recent[" + loop + "]", recent.getLabel()
204 						+ "!#!" + recent.getExecutionTime());
205 			}
206 			loop++;
207 		}
208 		String tempPath = System.getProperty("java.io.tmpdir");
209 		String fileName = this.getClass().getName() + "_dsol.properties";
210 		File file = new File(tempPath, fileName);
211 		if (!file.exists())
212 		{
213 			file.createNewFile();
214 		}
215 		FileOutputStream outputStream = new FileOutputStream(file);
216 		this.properties.store(outputStream, "");
217 		outputStream.close();
218 	}
219 
220 	/***
221 	 * the Recent Comparator
222 	 */
223 	private static class RecentComparator implements Comparator
224 	{
225 		/***
226 		 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
227 		 */
228 		public int compare(final Object o1, final Object o2)
229 		{
230 			Recent r1 = (Recent) o1;
231 			Recent r2 = (Recent) o2;
232 			if (r1.getLabel().equals(r2.getLabel()))
233 			{
234 				long executionTime = Math.max(r1.getExecutionTime(), r2
235 						.getExecutionTime());
236 				r1.executionTime = executionTime;
237 				r2.executionTime = executionTime;
238 				return 0;
239 			}
240 			return new Long(r2.getExecutionTime()).compareTo(new Long(r1
241 					.getExecutionTime()));
242 		}
243 	}
244 
245 	/***
246 	 * A RecentItem
247 	 */
248 	private static class Recent
249 	{
250 		/*** the label to use */
251 		private String label = null;
252 
253 		/*** the executionTime */
254 		protected long executionTime = 0L;
255 
256 		/***
257 		 * constructs a new Recent
258 		 * 
259 		 * @param label the label of the experimen
260 		 * @param executionTime the executionTime of the experiment
261 		 */
262 		public Recent(final String label, final long executionTime)
263 		{
264 			this.executionTime = executionTime;
265 			this.label = label;
266 		}
267 
268 		/***
269 		 * @return Returns the executionTime.
270 		 */
271 		public long getExecutionTime()
272 		{
273 			return this.executionTime;
274 		}
275 
276 		/***
277 		 * @return Returns the label.
278 		 */
279 		public String getLabel()
280 		{
281 			return this.label;
282 		}
283 	}
284 }