1 package org.opentrafficsim.simulationengine;
2
3 import java.awt.Dimension;
4 import java.awt.Frame;
5 import java.awt.Rectangle;
6 import java.awt.geom.Rectangle2D;
7 import java.rmi.RemoteException;
8 import java.util.ArrayList;
9
10 import javax.naming.NamingException;
11 import javax.swing.JPanel;
12 import javax.swing.WindowConstants;
13
14 import nl.tudelft.simulation.dsol.SimRuntimeException;
15
16 import org.opentrafficsim.core.OTS_SCALAR;
17 import org.opentrafficsim.core.dsol.OTSModelInterface;
18 import org.opentrafficsim.core.gtu.animation.DefaultSwitchableGTUColorer;
19 import org.opentrafficsim.core.gtu.animation.GTUColorer;
20 import org.opentrafficsim.core.network.NetworkException;
21 import org.opentrafficsim.gui.OTSAnimationPanel;
22 import org.opentrafficsim.gui.SimulatorFrame;
23 import org.opentrafficsim.simulationengine.properties.AbstractProperty;
24
25
26
27
28
29
30
31
32
33
34
35 public abstract class AbstractWrappableAnimation implements WrappableAnimation, OTS_SCALAR
36 {
37
38 @SuppressWarnings("checkstyle:visibilitymodifier")
39 protected ArrayList<AbstractProperty<?>> properties = new ArrayList<AbstractProperty<?>>();
40
41
42 @SuppressWarnings("checkstyle:visibilitymodifier")
43 protected ArrayList<AbstractProperty<?>> savedUserModifiedProperties;
44
45
46 @SuppressWarnings("checkstyle:visibilitymodifier")
47 protected boolean exitOnClose;
48
49
50 @SuppressWarnings("checkstyle:visibilitymodifier")
51 protected OTSAnimationPanel panel;
52
53
54 private Time.Abs savedStartTime;
55
56
57 private Time.Rel savedWarmupPeriod;
58
59
60 private Time.Rel savedRunLength;
61
62
63 @Override
64 public final SimpleAnimator buildAnimator(final Time.Abs startTime, final Time.Rel warmupPeriod,
65 final Time.Rel runLength, final ArrayList<AbstractProperty<?>> userModifiedProperties, final Rectangle rect,
66 final boolean eoc) throws SimRuntimeException, NamingException
67 {
68 this.savedUserModifiedProperties = userModifiedProperties;
69 this.exitOnClose = eoc;
70
71 this.savedStartTime = startTime;
72 this.savedWarmupPeriod = warmupPeriod;
73 this.savedRunLength = runLength;
74
75 GTUColorer colorer = new DefaultSwitchableGTUColorer();
76 OTSModelInterface model = makeModel(colorer);
77
78 if (null == model)
79 {
80 return null;
81 }
82
83 final SimpleAnimator simulator = new SimpleAnimator(startTime, warmupPeriod, runLength, model);
84 try
85 {
86 this.panel = new OTSAnimationPanel(makeAnimationRectangle(), new Dimension(1024, 768), simulator, this, colorer);
87 }
88 catch (RemoteException exception)
89 {
90 throw new SimRuntimeException(exception);
91 }
92 JPanel charts = makeCharts();
93 if (null != charts)
94 {
95 this.panel.getTabbedPane().addTab("statistics", charts);
96 }
97
98 SimulatorFrame frame = new SimulatorFrame(shortName(), this.panel);
99 if (rect != null)
100 {
101 frame.setBounds(rect);
102 }
103 else
104 {
105 frame.setExtendedState(Frame.MAXIMIZED_BOTH);
106 }
107
108 frame.setDefaultCloseOperation(this.exitOnClose ? WindowConstants.EXIT_ON_CLOSE : WindowConstants.DISPOSE_ON_CLOSE);
109 return simulator;
110 }
111
112
113
114
115
116 protected abstract JPanel makeCharts();
117
118
119
120
121
122 protected abstract OTSModelInterface makeModel(GTUColorer colorer);
123
124
125
126
127 protected abstract Rectangle2D.Double makeAnimationRectangle();
128
129
130 @Override
131 public final ArrayList<AbstractProperty<?>> getProperties()
132 {
133 return new ArrayList<AbstractProperty<?>>(this.properties);
134 }
135
136
137 @Override
138 public final SimpleSimulatorInterface rebuildSimulator(final Rectangle rect) throws SimRuntimeException,
139 NetworkException, NamingException
140 {
141 return buildAnimator(this.savedStartTime, this.savedWarmupPeriod, this.savedRunLength,
142 this.savedUserModifiedProperties, rect, this.exitOnClose);
143 }
144
145
146 @Override
147 public final ArrayList<AbstractProperty<?>> getUserModifiedProperties()
148 {
149 return this.savedUserModifiedProperties;
150 }
151
152
153 @Override
154 @SuppressWarnings("checkstyle:designforextension")
155 public void stopTimersThreads()
156 {
157 if (this.panel != null && this.panel.getStatusBar() != null)
158 {
159 this.panel.getStatusBar().cancelTimer();
160 }
161 this.panel = null;
162 }
163 }