View Javadoc
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.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.draw.core.OTSDrawingException;
25  import org.opentrafficsim.draw.graphs.GraphPath;
26  import org.opentrafficsim.draw.graphs.TrajectoryPlot;
27  import org.opentrafficsim.draw.graphs.road.GraphLaneUtil;
28  import org.opentrafficsim.kpi.sampling.KpiLaneDirection;
29  import org.opentrafficsim.road.network.OTSRoadNetwork;
30  import org.opentrafficsim.road.network.lane.LaneDirection;
31  import org.opentrafficsim.road.network.sampling.RoadSampler;
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   * Simplest contour plots demonstration.
48   * <p>
49   * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
50   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
51   * <p>
52   * $LastChangedDate: 2019-01-06 01:35:05 +0100 (Sun, 06 Jan 2019) $, @version $Revision: 4831 $, by $Author: averbraeck $,
53   * initial version 12 nov. 2014 <br>
54   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
55   */
56  public class NetworksSwing extends OTSSimulationApplication<NetworksModel> implements UNITS
57  {
58      /** */
59      private static final long serialVersionUID = 1L;
60  
61      /**
62       * Create a Networks Swing application.
63       * @param title String; the title of the Frame
64       * @param panel OTSAnimationPanel; the tabbed panel to display
65       * @param model NetworksModel; the model
66       * @throws OTSDrawingException on animation error
67       */
68      public NetworksSwing(final String title, final OTSAnimationPanel panel, final NetworksModel model)
69              throws OTSDrawingException
70      {
71          super(model, panel);
72          OTSRoadNetwork network = model.getNetwork();
73          System.out.println(network.getLinkMap());
74      }
75  
76      /** {@inheritDoc} */
77      @Override
78      protected void addTabs()
79      {
80          addStatisticsTabs(getModel().getSimulator());
81      }
82  
83      /**
84       * Main program.
85       * @param args String[]; the command line arguments (not used)
86       */
87      public static void main(final String[] args)
88      {
89          demo(true);
90      }
91  
92      /**
93       * Start the demo.
94       * @param exitOnClose boolean; when running stand-alone: true; when running as part of a demo: false
95       */
96      public static void demo(final boolean exitOnClose)
97      {
98          try
99          {
100             OTSAnimator simulator = new OTSAnimator("NetworksSwing");
101             final NetworksModelml#NetworksModel">NetworksModel otsModel = new NetworksModel(simulator);
102             if (NetworksParameterDialog.process(otsModel.getInputParameterMap()))
103             {
104                 simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(3600.0), otsModel);
105                 OTSAnimationPanel animationPanel = new OTSAnimationPanel(otsModel.getNetwork().getExtent(), new Dimension(800,
106                     600), simulator, otsModel, DEFAULT_COLORER, otsModel.getNetwork());
107                 NetworksSwingng.html#NetworksSwing">NetworksSwing app = new NetworksSwing("Networks", animationPanel, otsModel);
108                 app.setExitOnClose(exitOnClose);
109             }
110             else
111             {
112                 if (exitOnClose)
113                 {
114                     System.exit(0);
115                 }
116             }
117         }
118         catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException | DSOLException exception)
119         {
120             exception.printStackTrace();
121         }
122     }
123 
124     /**
125      * Add the statistics tabs.
126      * @param simulator OTSSimulatorInterface; the simulator on which sampling can be scheduled
127      */
128     protected final void addStatisticsTabs(final OTSSimulatorInterface simulator)
129     {
130         int graphCount = getModel().pathCount();
131         int columns = 1;
132         int rows = 0 == columns ? 0 : (int) Math.ceil(graphCount * 1.0 / columns);
133         TablePanel charts = new TablePanel(columns, rows);
134         RoadSampler sampler = new RoadSampler(getModel().getNetwork());
135         Duration updateInterval = Duration.instantiateSI(10.0);
136         for (int graphIndex = 0; graphIndex < graphCount; graphIndex++)
137         {
138             List<LaneDirection> start = new ArrayList<>();
139             start.add(new LaneDirection(getModel().getPath(graphIndex).get(0), GTUDirectionality.DIR_PLUS));
140             GraphPath<KpiLaneDirection> path;
141             try
142             {
143                 path = GraphLaneUtil.createPath("name", start.get(0));
144             }
145             catch (NetworkException exception)
146             {
147                 throw new RuntimeException(exception);
148             }
149             GraphPath.initRecording(sampler, path);
150             SwingPlot plot = new SwingPlot(new TrajectoryPlot("Trajectories on lane " + (graphIndex + 1), updateInterval,
151                 simulator, sampler.getSamplerData(), path));
152             charts.setCell(plot.getContentPane(), graphIndex % columns, graphIndex / columns);
153         }
154 
155         getAnimationPanel().getTabbedPane().addTab(getAnimationPanel().getTabbedPane().getTabCount(), "statistics ", charts);
156     }
157 
158     /** A parameter dialog with a radio button for the network choice tab. */
159     private static class NetworksParameterDialog extends TabbedParameterDialog
160     {
161         /** */
162         private static final long serialVersionUID = 1L;
163 
164         /**
165          * @param inputParameterMap InputParameterMap; the parameter map to display
166          */
167         NetworksParameterDialog(final InputParameterMap inputParameterMap)
168         {
169             super(inputParameterMap);
170         }
171 
172         /** {@inheritDoc} */
173         @SuppressWarnings({"rawtypes", "unchecked"})
174         @Override
175         public void addParameterField(final JPanel panel, final InputParameter<?, ?> parameter)
176         {
177             if (parameter instanceof InputParameterSelectionMap<?, ?>)
178             {
179                 this.fields.add(new InputFieldSelectionMapRadio(panel, (InputParameterSelectionMap<?, ?>) parameter));
180             }
181             else
182             {
183                 super.addParameterField(panel, parameter);
184             }
185         }
186 
187         /** {@inheritDoc} */
188         @Override
189         public void actionPerformed(final ActionEvent e)
190         {
191             boolean ok = true;
192             try
193             {
194                 for (InputField field : this.fields)
195                 {
196                     if (field instanceof InputFieldSelectionMapRadio<?, ?>)
197                     {
198                         InputFieldSelectionMapRadio<?, ?> f = (InputFieldSelectionMapRadio<?, ?>) field;
199                         f.getParameter().setObjectValue(f.getValue());
200                     }
201                 }
202             }
203             catch (Exception exception)
204             {
205                 JOptionPane.showMessageDialog(null, exception.getMessage(), "Data Entry Error", JOptionPane.ERROR_MESSAGE);
206                 ok = false;
207             }
208             if (ok)
209             {
210                 super.actionPerformed(e);
211             }
212         }
213 
214         /**
215          * Construct a tabbed parameter dialog that is not a part of a higher dialog.
216          * @param inputParameterMap InputParameterMap; the parameter map to use
217          * @return whether the data was entered correctly or not
218          */
219         public static boolean process(final InputParameterMap inputParameterMap)
220         {
221             NetworksParameterDialog dialog = new NetworksParameterDialog(inputParameterMap);
222             return !dialog.stopped;
223         }
224     }
225 
226     /**
227      * Radio button selection map.
228      * @param <K> key of the selection map
229      * @param <T> return type of the selection map
230      */
231     public static class InputFieldSelectionMapRadio<K, T> extends AbstractInputField
232     {
233         /** combo box for the user interface. */
234         private List<JRadioButton> buttons = new ArrayList<>();
235 
236         /** combo box for the user interface. */
237         private List<T> values = new ArrayList<>();
238 
239         /**
240          * Create a string field on the screen.
241          * @param panel JPanel; panel to add the field to
242          * @param parameter InputParameterSelectionMap&lt;K,T&gt;; the parameter
243          */
244         public InputFieldSelectionMapRadio(final JPanel panel, final InputParameterSelectionMap<K, T> parameter)
245         {
246             super(parameter);
247             Box box = Box.createVerticalBox();
248             box.add(new JLabel("  "));
249             box.add(new JLabel(parameter.getShortName()));
250             ButtonGroup group = new ButtonGroup();
251             for (K option : parameter.getOptions().keySet())
252             {
253                 String item = option.toString();
254                 T value = parameter.getOptions().get(option);
255                 JRadioButton button = new JRadioButton(item);
256                 button.setActionCommand(item);
257                 if (value.equals(parameter.getDefaultValue()))
258                 {
259                     button.setSelected(true);
260                 }
261                 group.add(button);
262                 box.add(button);
263                 this.buttons.add(button);
264                 this.values.add(value);
265             }
266             panel.add(box);
267         }
268 
269         /** {@inheritDoc} */
270         @SuppressWarnings("unchecked")
271         @Override
272         public InputParameterSelectionMap<K, T> getParameter()
273         {
274             return (InputParameterSelectionMap<K, T>) super.getParameter();
275         }
276 
277         /** @return the mapped value of the field in the gui, selected by the key's toString() value. */
278         public T getValue()
279         {
280             for (JRadioButton button : this.buttons)
281             {
282                 if (button.isSelected())
283                 {
284                     System.out.println("SELECTED: " + this.values.get(this.buttons.indexOf(button)));
285                     return this.values.get(this.buttons.indexOf(button));
286                 }
287             }
288             return this.values.get(0);
289         }
290     }
291 }