1
2
3
4
5
6
7
8
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.util.Arrays;
17 import java.util.Comparator;
18 import java.util.Iterator;
19 import java.util.Properties;
20 import java.util.TreeSet;
21
22 import nl.tudelft.simulation.dsol.experiment.Experiment;
23 import nl.tudelft.simulation.dsol.gui.windows.DSOLFrame;
24 import nl.tudelft.simulation.dsol.simulators.Animator;
25 import nl.tudelft.simulation.event.EventProducer;
26 import nl.tudelft.simulation.language.io.URLResource;
27 import nl.tudelft.simulation.logger.Logger;
28 import nl.tudelft.simulation.logger.handlers.MemoryHandler;
29
30 /***
31 * The DSOL Application <br>
32 * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
33 * University of Technology </a>, the Netherlands. <br>
34 * See for project information <a
35 * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
36 * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
37 * License (GPL) </a>, no warranty <br>
38 *
39 * @version 1.0 18.10.2003 <br>
40 * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
41 * Jacobs </a>
42 */
43 public class DSOLApplication extends EventProducer implements
44 DSOLApplicationInterface
45 {
46 /***
47 * the experiment
48 *
49 * @uml.property name="experiment"
50 */
51 protected Experiment experiment = null;
52
53 /***
54 * the properties
55 *
56 * @uml.property name="properties"
57 */
58 protected Properties properties = new Properties();
59
60
61 /*** the recents */
62 protected TreeSet recents = new TreeSet(new RecentComparator());
63
64 /***
65 * constructs a new DSOLApplicationInterface
66 */
67 public DSOLApplication()
68 {
69 super();
70 Runtime.runFinalizersOnExit(true);
71 Logger.setDefaultHandler(MemoryHandler.class);
72 try
73 {
74 this.readProperties(this.properties);
75 new DSOLFrame(this);
76 } catch (Exception exception)
77 {
78 Logger.warning(this, "DSOLApplicationInterface", exception);
79 }
80 }
81
82 /***
83 * @see java.lang.Object#finalize()
84 */
85 protected void finalize() throws Throwable
86 {
87 this.writeProperties(this.properties);
88 super.finalize();
89 }
90
91 /***
92 * returns the experiment
93 *
94 * @return Experiment
95 *
96 * @uml.property name="experiment"
97 */
98 public Experiment getExperiment()
99 {
100 return this.experiment;
101 }
102
103 /***
104 * sets the experiment
105 *
106 * @param experiment the experiment
107 *
108 * @uml.property name="experiment"
109 */
110 public void setExperiment(final Experiment experiment)
111 {
112 this.experiment = experiment;
113 if (this.experiment != null && this.experiment.getUrl() != null)
114 {
115 this.recents.add(new Recent(experiment.getUrl().toString(), System
116 .currentTimeMillis()));
117 Object[] elements = this.recents.toArray();
118 this.recents.clear();
119 this.recents.addAll(Arrays.asList(elements));
120 try
121 {
122 this.writeProperties(this.properties);
123
124 this.experiment.setSimulator(new Animator());
125 this.experiment.start();
126 this.experiment.getSimulator().stop();
127 } catch (Exception e)
128 {
129 e.printStackTrace();
130 }
131 }
132 this.fireEvent(EXPERIMENT_CHANGED_EVENT, experiment);
133 }
134
135 /***
136 * returns the properties
137 *
138 * @return Properties
139 *
140 * @uml.property name="properties"
141 */
142 public Properties getProperties()
143 {
144 return this.properties;
145 }
146
147 /***
148 * executes the dsol control panel
149 *
150 * @param args the command-line arguments
151 */
152 public static void main(final String[] args)
153 {
154 new DSOLApplication();
155 }
156
157 /***
158 * reads the properties
159 *
160 * @param properties the properties to read
161 * @throws IOException on failure
162 */
163 private void readProperties(final Properties properties) throws IOException
164 {
165 String tempPath = System.getProperty("java.io.tmpdir");
166 String fileName = this.getClass().getName() + "_dsol.properties";
167 File file = new File(tempPath, fileName);
168 if (file.exists())
169 {
170 FileInputStream stream = new FileInputStream(file);
171 properties.load(stream);
172 stream.close();
173 } else
174 {
175 properties
176 .load(URLResource.getResourceAsStream("/dsol.properties"));
177 }
178 int i = 0;
179 String value = null;
180 while ((value = properties.getProperty("recent[" + i + "]")) != null)
181 {
182 String[] elements = value.split("!#!");
183 this.recents.add(new Recent(elements[0], Long
184 .parseLong(elements[1])));
185 i++;
186 }
187 }
188
189 /***
190 * reads the properties
191 *
192 * @param properties the properties to read
193 * @throws IOException on failure
194 */
195 private void writeProperties(final Properties properties) 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 }