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-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, @version $Revision: 1401 $, 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 AbstractWrappableAnimation implements WrappableAnimation, 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      /** save the startTime for restarting the simulation. */
54      private Time.Abs savedStartTime;
55  
56      /** save the startTime for restarting the simulation. */
57      private Time.Rel savedWarmupPeriod;
58  
59      /** save the runLength for restarting the simulation. */
60      private Time.Rel savedRunLength;
61  
62      /** {@inheritDoc} */
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; // Happens when the user cancels the file open dialog in the OpenStreetMap demo.
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      * @return the JPanel with the charts; the result will be put in the statistics tab. May return null; this causes no
114      *         statistics tab to be created.
115      */
116     protected abstract JPanel makeCharts();
117 
118     /**
119      * @param colorer the GTU colorer to use.
120      * @return the demo model. Don't forget to keep a local copy.
121      */
122     protected abstract OTSModelInterface makeModel(GTUColorer colorer);
123 
124     /**
125      * @return the initial rectangle for the animation.
126      */
127     protected abstract Rectangle2D.Double makeAnimationRectangle();
128 
129     /** {@inheritDoc} */
130     @Override
131     public final ArrayList<AbstractProperty<?>> getProperties()
132     {
133         return new ArrayList<AbstractProperty<?>>(this.properties);
134     }
135 
136     /** {@inheritDoc} */
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     /** {@inheritDoc} */
146     @Override
147     public final ArrayList<AbstractProperty<?>> getUserModifiedProperties()
148     {
149         return this.savedUserModifiedProperties;
150     }
151 
152     /** {@inheritDoc} */
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 }