View Javadoc
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   * <p>
27   * Copyright (c) 2013-2015 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
28   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
29   * <p>
30   * $LastChangedDate: 2015-09-03 13:38:01 +0200 (Thu, 03 Sep 2015) $, @version $Revision: 1378 $, by $Author: averbraeck $,
31   * initial version Jun 18, 2015 <br>
32   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
33   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
34   */
35  public abstract class AbstractWrappableSimulation implements WrappableSimulation, OTS_SCALAR
36  {
37      /** The properties exhibited by this simulation. */
38      @SuppressWarnings("checkstyle:visibilitymodifier")
39      protected ArrayList<AbstractProperty<?>> properties = new ArrayList<AbstractProperty<?>>();
40  
41      /** The properties after (possible) editing by the user. */
42      @SuppressWarnings("checkstyle:visibilitymodifier")
43      protected ArrayList<AbstractProperty<?>> savedUserModifiedProperties;
44  
45      /** Use EXIT_ON_CLOSE when true, DISPOSE_ON_CLOSE when false on closing of the window. */
46      @SuppressWarnings("checkstyle:visibilitymodifier")
47      protected boolean exitOnClose;
48  
49      /** the tabbed panel so other tabs can be added by the classes that extend this class. */
50      @SuppressWarnings("checkstyle:visibilitymodifier")
51      protected OTSAnimationPanel panel;
52  
53      /** {@inheritDoc} */
54      @Override
55      public final SimpleAnimator buildSimulator(final ArrayList<AbstractProperty<?>> userModifiedProperties,
56          final Rectangle rect, final boolean eoc) throws RemoteException, SimRuntimeException, NamingException
57      {
58          this.savedUserModifiedProperties = userModifiedProperties;
59          this.exitOnClose = eoc;
60  
61          GTUColorer colorer = new DefaultSwitchableGTUColorer();
62          OTSModelInterface model = makeModel(colorer);
63  
64          if (null == model)
65          {
66              return null; // Happens when the user cancels the file open dialog in the OpenStreetMap demo.
67          }
68  
69          final SimpleAnimator simulator =
70              new SimpleAnimator(new Time.Abs(0.0, SECOND), new Time.Rel(0.0, SECOND), new Time.Rel(3600.0, SECOND), model);
71          this.panel = new OTSAnimationPanel(makeAnimationRectangle(), new Dimension(1024, 768), simulator, this, colorer);
72          JPanel charts = makeCharts();
73          if (null != charts)
74          {
75              this.panel.getTabbedPane().addTab("statistics", charts);
76          }
77  
78          SimulatorFrame frame = new SimulatorFrame(shortName(), this.panel);
79          if (rect != null)
80          {
81              frame.setBounds(rect);
82          }
83          else
84          {
85              frame.setExtendedState(Frame.MAXIMIZED_BOTH);
86          }
87  
88          frame.setDefaultCloseOperation(this.exitOnClose ? WindowConstants.EXIT_ON_CLOSE : WindowConstants.DISPOSE_ON_CLOSE);
89          return simulator;
90      }
91  
92      /**
93       * @return the JPanel with the charts; the result will be put in the statistics tab. May return null; this causes no
94       *         statistics tab to be created.
95       */
96      protected abstract JPanel makeCharts();
97  
98      /**
99       * @param colorer the GTU colorer to use.
100      * @return the demo model. Don't forget to keep a local copy.
101      */
102     protected abstract OTSModelInterface makeModel(GTUColorer colorer);
103 
104     /**
105      * @return the initial rectangle for the animation.
106      */
107     protected abstract Rectangle2D.Double makeAnimationRectangle();
108 
109     /** {@inheritDoc} */
110     @Override
111     public final ArrayList<AbstractProperty<?>> getProperties()
112     {
113         return new ArrayList<AbstractProperty<?>>(this.properties);
114     }
115 
116     /** {@inheritDoc} */
117     @Override
118     public final SimpleSimulation rebuildSimulator(final Rectangle rect) throws SimRuntimeException, RemoteException,
119         NetworkException, NamingException
120     {
121         return buildSimulator(this.savedUserModifiedProperties, rect, this.exitOnClose);
122     }
123 
124     /** {@inheritDoc} */
125     @Override
126     public final ArrayList<AbstractProperty<?>> getUserModifiedProperties()
127     {
128         return this.savedUserModifiedProperties;
129     }
130 
131     /** {@inheritDoc} */
132     @Override
133     @SuppressWarnings("checkstyle:designforextension")
134     public void stopTimersThreads()
135     {
136         if (this.panel != null && this.panel.getStatusBar() != null)
137         {
138             this.panel.getStatusBar().cancelTimer();
139         }
140         this.panel = null;
141     }
142 }