View Javadoc
1   package org.opentrafficsim.swing.gui;
2   
3   import java.awt.Frame;
4   import java.awt.event.WindowAdapter;
5   import java.awt.event.WindowEvent;
6   import java.awt.geom.Rectangle2D;
7   
8   import javax.swing.JPanel;
9   import javax.swing.WindowConstants;
10  
11  import org.opentrafficsim.animation.gtu.colorer.DefaultSwitchableGtuColorer;
12  import org.opentrafficsim.animation.gtu.colorer.GtuColorer;
13  import org.opentrafficsim.core.dsol.OtsModelInterface;
14  
15  /**
16   * Wrap a DSOL simulation model, or any (descendant of a) JPanel in a JFrame (wrap it in a window). The window will be
17   * maximized.
18   * <p>
19   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
20   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
21   * </p>
22   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
23   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
24   * @param <T> model type
25   */
26  public class OtsSwingApplication<T extends OtsModelInterface> extends AppearanceApplication
27  {
28      /** */
29      private static final long serialVersionUID = 20141216L;
30  
31      /** Single instance of default colorer, reachable from various places. */
32      public static final GtuColorer DEFAULT_COLORER = new DefaultSwitchableGtuColorer();
33  
34      /** the model. */
35      private final T model;
36  
37      /** whether the application has been closed or not. */
38      @SuppressWarnings("checkstyle:visibilitymodifier")
39      protected boolean closed = false;
40  
41      /**
42       * Wrap an OtsModel in a JFrame. Uses a default GTU colorer.
43       * @param model T; the model that will be shown in the JFrame
44       * @param panel JPanel; this should be the JPanel of the simulation
45       */
46      public OtsSwingApplication(final T model, final JPanel panel)
47      {
48          super(panel);
49          this.model = model;
50          setTitle("OTS | The Open Traffic Simulator | " + model.getDescription());
51          pack();
52          setExtendedState(Frame.MAXIMIZED_BOTH);
53          setVisible(true);
54  
55          setExitOnClose(true);
56          addWindowListener(new WindowAdapter()
57          {
58              @Override
59              public void windowClosing(final WindowEvent windowEvent)
60              {
61                  OtsSwingApplication.this.closed = true;
62                  super.windowClosing(windowEvent);
63              }
64          });
65      }
66  
67      /**
68       * Return the initial 'home' extent for the animation. The 'Home' button returns to this extent. Override this method when a
69       * smaller or larger part of the infra should be shown. In the default setting, all currently visible objects are shown.
70       * @return the initial and 'home' rectangle for the animation.
71       */
72      @SuppressWarnings("checkstyle:designforextension")
73      protected Rectangle2D makeAnimationRectangle()
74      {
75          return this.model.getNetwork().getExtent();
76      }
77  
78      /**
79       * @param exitOnClose boolean; set exitOnClose
80       */
81      public final void setExitOnClose(final boolean exitOnClose)
82      {
83          if (exitOnClose)
84          {
85              setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
86          }
87          else
88          {
89              setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
90          }
91      }
92  
93      /**
94       * @return closed
95       */
96      public final boolean isClosed()
97      {
98          return this.closed;
99      }
100 
101     /**
102      * @return model
103      */
104     public final T getModel()
105     {
106         return this.model;
107     }
108 
109 }