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