1 package org.opentrafficsim.demo;
2
3 import java.awt.Dimension;
4 import java.awt.event.ActionEvent;
5 import java.rmi.RemoteException;
6 import java.util.ArrayList;
7 import java.util.List;
8
9 import javax.naming.NamingException;
10 import javax.swing.Box;
11 import javax.swing.ButtonGroup;
12 import javax.swing.JLabel;
13 import javax.swing.JOptionPane;
14 import javax.swing.JPanel;
15 import javax.swing.JRadioButton;
16
17 import org.djunits.unit.UNITS;
18 import org.djunits.value.vdouble.scalar.Duration;
19 import org.djunits.value.vdouble.scalar.Time;
20 import org.opentrafficsim.core.dsol.OTSAnimator;
21 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
22 import org.opentrafficsim.core.gtu.GTUDirectionality;
23 import org.opentrafficsim.core.network.NetworkException;
24 import org.opentrafficsim.core.network.OTSNetwork;
25 import org.opentrafficsim.draw.core.OTSDrawingException;
26 import org.opentrafficsim.draw.graphs.AbstractPlot;
27 import org.opentrafficsim.draw.graphs.GraphPath;
28 import org.opentrafficsim.draw.graphs.TrajectoryPlot;
29 import org.opentrafficsim.draw.graphs.road.GraphLaneUtil;
30 import org.opentrafficsim.kpi.sampling.KpiLaneDirection;
31 import org.opentrafficsim.road.network.lane.LaneDirection;
32 import org.opentrafficsim.road.network.sampling.RoadSampler;
33 import org.opentrafficsim.swing.gui.OTSAnimationPanel;
34 import org.opentrafficsim.swing.gui.OTSSimulationApplication;
35
36 import nl.tudelft.simulation.dsol.SimRuntimeException;
37 import nl.tudelft.simulation.dsol.model.inputparameters.InputParameter;
38 import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterMap;
39 import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterSelectionMap;
40 import nl.tudelft.simulation.dsol.swing.gui.TablePanel;
41 import nl.tudelft.simulation.dsol.swing.gui.inputparameters.AbstractInputField;
42 import nl.tudelft.simulation.dsol.swing.gui.inputparameters.InputField;
43 import nl.tudelft.simulation.dsol.swing.gui.inputparameters.TabbedParameterDialog;
44
45
46
47
48
49
50
51
52
53
54
55 public class NetworksSwing extends OTSSimulationApplication<NetworksModel> implements UNITS
56 {
57
58 private static final long serialVersionUID = 1L;
59
60
61
62
63
64
65
66
67 public NetworksSwing(final String title, final OTSAnimationPanel panel, final NetworksModel model)
68 throws OTSDrawingException
69 {
70 super(model, panel);
71 OTSNetwork network = model.getNetwork();
72 System.out.println(network.getLinkMap());
73 }
74
75
76 @Override
77 protected void addTabs()
78 {
79 addStatisticsTabs(getModel().getSimulator());
80 }
81
82
83
84
85
86 public static void main(final String[] args)
87 {
88 demo(true);
89 }
90
91
92
93
94
95 public static void demo(final boolean exitOnClose)
96 {
97 try
98 {
99 OTSAnimator simulator = new OTSAnimator();
100 final NetworksModel otsModel = new NetworksModel(simulator);
101 if (NetworksParameterDialog.process(otsModel.getInputParameterMap()))
102 {
103 simulator.initialize(Time.ZERO, Duration.ZERO, Duration.createSI(3600.0), otsModel);
104 OTSAnimationPanel animationPanel = new OTSAnimationPanel(otsModel.getNetwork().getExtent(),
105 new Dimension(800, 600), simulator, otsModel, DEFAULT_COLORER, otsModel.getNetwork());
106 NetworksSwing app = new NetworksSwing("Networks", animationPanel, otsModel);
107 app.setExitOnClose(exitOnClose);
108 }
109 else
110 {
111 if (exitOnClose)
112 {
113 System.exit(0);
114 }
115 }
116 }
117 catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException exception)
118 {
119 exception.printStackTrace();
120 }
121 }
122
123
124
125
126
127 protected final void addStatisticsTabs(final OTSSimulatorInterface simulator)
128 {
129 int graphCount = getModel().pathCount();
130 int columns = 1;
131 int rows = 0 == columns ? 0 : (int) Math.ceil(graphCount * 1.0 / columns);
132 TablePanel charts = new TablePanel(columns, rows);
133 RoadSampler sampler = new RoadSampler(simulator);
134 Duration updateInterval = Duration.createSI(10.0);
135 for (int graphIndex = 0; graphIndex < graphCount; graphIndex++)
136 {
137 List<LaneDirection> start = new ArrayList<>();
138 start.add(new LaneDirection(getModel().getPath(graphIndex).get(0), GTUDirectionality.DIR_PLUS));
139 GraphPath<KpiLaneDirection> path;
140 try
141 {
142 path = GraphLaneUtil.createPath("name", start.get(0));
143 }
144 catch (NetworkException exception)
145 {
146 throw new RuntimeException(exception);
147 }
148 AbstractPlot plot =
149 new TrajectoryPlot("Trajectories on lane " + (graphIndex + 1), updateInterval, simulator, sampler, path);
150 charts.setCell(plot.getContentPane(), graphIndex % columns, graphIndex / columns);
151 }
152
153 getAnimationPanel().getTabbedPane().addTab(getAnimationPanel().getTabbedPane().getTabCount(), "statistics ", charts);
154 }
155
156
157 private static class NetworksParameterDialog extends TabbedParameterDialog
158 {
159
160 private static final long serialVersionUID = 1L;
161
162
163
164
165 NetworksParameterDialog(final InputParameterMap inputParameterMap)
166 {
167 super(inputParameterMap);
168 }
169
170
171 @SuppressWarnings({ "rawtypes", "unchecked" })
172 @Override
173 public void addParameterField(final JPanel panel, final InputParameter<?, ?> parameter)
174 {
175 if (parameter instanceof InputParameterSelectionMap<?, ?>)
176 {
177 this.fields.add(new InputFieldSelectionMapRadio(panel, (InputParameterSelectionMap<?, ?>) parameter));
178 }
179 else
180 {
181 super.addParameterField(panel, parameter);
182 }
183 }
184
185
186 @Override
187 public void actionPerformed(final ActionEvent e)
188 {
189 boolean ok = true;
190 try
191 {
192 for (InputField field : this.fields)
193 {
194 if (field instanceof InputFieldSelectionMapRadio<?, ?>)
195 {
196 InputFieldSelectionMapRadio<?, ?> f = (InputFieldSelectionMapRadio<?, ?>) field;
197 f.getParameter().setObjectValue(f.getValue());
198 }
199 }
200 }
201 catch (Exception exception)
202 {
203 JOptionPane.showMessageDialog(null, exception.getMessage(), "Data Entry Error", JOptionPane.ERROR_MESSAGE);
204 ok = false;
205 }
206 if (ok)
207 {
208 super.actionPerformed(e);
209 }
210 }
211
212
213
214
215
216
217 public static boolean process(final InputParameterMap inputParameterMap)
218 {
219 NetworksParameterDialog dialog = new NetworksParameterDialog(inputParameterMap);
220 return !dialog.stopped;
221 }
222 }
223
224
225
226
227
228
229 public static class InputFieldSelectionMapRadio<K, T> extends AbstractInputField
230 {
231
232 private List<JRadioButton> buttons = new ArrayList<>();
233
234
235 private List<T> values = new ArrayList<>();
236
237
238
239
240
241
242 public InputFieldSelectionMapRadio(final JPanel panel, final InputParameterSelectionMap<K, T> parameter)
243 {
244 super(parameter);
245 Box box = Box.createVerticalBox();
246 box.add(new JLabel(" "));
247 box.add(new JLabel(parameter.getShortName()));
248 ButtonGroup group = new ButtonGroup();
249 for (K option : parameter.getOptions().keySet())
250 {
251 String item = option.toString();
252 T value = parameter.getOptions().get(option);
253 JRadioButton button = new JRadioButton(item);
254 button.setActionCommand(item);
255 if (value.equals(parameter.getDefaultValue()))
256 {
257 button.setSelected(true);
258 }
259 group.add(button);
260 box.add(button);
261 this.buttons.add(button);
262 this.values.add(value);
263 }
264 panel.add(box);
265 }
266
267
268 @SuppressWarnings("unchecked")
269 @Override
270 public InputParameterSelectionMap<K, T> getParameter()
271 {
272 return (InputParameterSelectionMap<K, T>) super.getParameter();
273 }
274
275
276 public T getValue()
277 {
278 for (JRadioButton button : this.buttons)
279 {
280 if (button.isSelected())
281 {
282 System.out.println("SELECTED: " + this.values.get(this.buttons.indexOf(button)));
283 return this.values.get(this.buttons.indexOf(button));
284 }
285 }
286 return this.values.get(0);
287 }
288 }
289 }