View Javadoc
1   package org.opentrafficsim.gui;
2   
3   import java.awt.BorderLayout;
4   import java.awt.Insets;
5   import java.rmi.RemoteException;
6   import java.util.List;
7   
8   import javax.swing.JLabel;
9   import javax.swing.JPanel;
10  import javax.swing.JScrollPane;
11  import javax.swing.SwingConstants;
12  import javax.swing.UIManager;
13  
14  import org.opentrafficsim.base.modelproperties.CompoundProperty;
15  import org.opentrafficsim.base.modelproperties.Property;
16  import org.opentrafficsim.base.modelproperties.PropertyException;
17  import org.opentrafficsim.simulationengine.WrappableAnimation;
18  
19  import nl.tudelft.simulation.dsol.gui.swing.Console;
20  import nl.tudelft.simulation.dsol.gui.swing.StatusBar;
21  import nl.tudelft.simulation.dsol.gui.swing.TabbedContentPane;
22  import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
23  
24  /**
25   * GUI with simulator, console, control panel, status bar, etc.
26   * <p>
27   * Copyright (c) 2013-2018 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: 2018-09-19 13:55:45 +0200 (Wed, 19 Sep 2018) $, @version $Revision: 4006 $, 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 class OTSSimulationPanel extends JPanel
36  {
37      /** */
38      private static final long serialVersionUID = 20150617L;
39  
40      /** The simulator. */
41      private final DEVSSimulatorInterface.TimeDoubleUnit simulator;
42  
43      /** The console to log messages. */
44      private final Console console = new Console();
45  
46      /** The control panel to control start/stop, speed of the simulation. */
47      private final OTSControlPanel otsControlPanel;
48  
49      /** Animation, required to add properties tab. */
50      private final WrappableAnimation wrappableAnimation;
51  
52      static
53      {
54          // use narrow border for TabbedPane, which cannot be changed afterwards
55          UIManager.put("TabbedPane.contentBorderInsets", new Insets(1, 1, 1, 1));
56      }
57  
58      /** The tabbed pane that contains the different (default) screens. */
59      private final TabbedContentPane tabbedPane = new AppearanceControlTabbedContentPane(SwingConstants.BOTTOM);
60  
61      /** The status bar at the bottom to indicate wall clock time and simulation time. */
62      private final StatusBar statusBar = null;
63  
64      /**
65       * Construct a panel that looks like the DSOLPanel for quick building of OTS applications.
66       * @param simulator the simulator or animator of the model.
67       * @param wrappableAnimation the builder and rebuilder of the simulation, based on properties.
68       * @throws RemoteException when communications to a remote machine fails
69       * @throws PropertyException when one of the user modified properties has the empty string as key
70       */
71      public OTSSimulationPanel(final DEVSSimulatorInterface.TimeDoubleUnit simulator,
72              final WrappableAnimation wrappableAnimation) throws RemoteException, PropertyException
73      {
74  
75          this.simulator = simulator;
76          this.wrappableAnimation = wrappableAnimation;
77  
78          this.setLayout(new BorderLayout());
79  
80          // Let's add our simulationControl
81          this.otsControlPanel = new OTSControlPanel(simulator, wrappableAnimation);
82          this.add(this.otsControlPanel, BorderLayout.NORTH);
83  
84          // Let's display our tabbed contentPane
85          this.add(this.tabbedPane, BorderLayout.CENTER);
86  
87          // put a status bar at the bottom
88          // this.statusBar = new StatusBar(this.simulator);
89          // this.add(this.statusBar, BorderLayout.SOUTH);
90      }
91  
92      /**
93       * Adds the console tab.
94       */
95      public final void addConsoleTab()
96      {
97          // Let's add our console to our tabbed pane
98          JScrollPane cons = new JScrollPane(this.console);
99          cons.setBorder(null);
100         this.tabbedPane.addTab("console", cons);
101     }
102 
103     /**
104      * Adds the properties tab.
105      * @throws PropertyException on exception with properties
106      */
107     public final void addPropertiesTab() throws PropertyException
108     {
109         // Let's add the properties of the simulation model as a tab
110         List<Property<?>> propertyList =
111                 new CompoundProperty("", "", "", this.wrappableAnimation.getUserModifiedProperties(), true, 0)
112                         .displayOrderedValue();
113         StringBuilder html = new StringBuilder();
114         html.append("<html><table border=\"1\"><tr><th colspan=\"" + propertyList.size() + "\">Settings</th></tr><tr>");
115 
116         for (Property<?> ap : propertyList)
117         {
118             html.append("<td valign=\"top\">" + ap.htmlStateDescription() + "</td>");
119         }
120         html.append("</table></html>");
121         JLabel propertySettings = new JLabel(html.toString());
122         JScrollPane settings = new JScrollPane(propertySettings);
123         settings.setBorder(null);
124         this.tabbedPane.addTab("settings", settings);
125     }
126 
127     /**
128      * @return tabbedPane
129      */
130     public final TabbedContentPane getTabbedPane()
131     {
132         return this.tabbedPane;
133     }
134 
135     /**
136      * @return simulator.
137      */
138     public final DEVSSimulatorInterface.TimeDoubleUnit getSimulator()
139     {
140         return this.simulator;
141     }
142 
143     /**
144      * @return statusBar.
145      */
146     public final StatusBar getStatusBar()
147     {
148         return this.statusBar;
149     }
150 
151     /**
152      * Return the OTSControlPanel of this OTSSimulationPanel.
153      * @return OTSControlPanel; the OTS control panel
154      */
155     public final OTSControlPanel getOtsControlPanel()
156     {
157         return this.otsControlPanel;
158     }
159 
160     /** {@inheritDoc} */
161     @Override
162     public final String toString()
163     {
164         return "OTSSimulationPanel [simulatorTime=" + this.simulator.getSimulatorTime() + "]";
165     }
166 
167     /**
168      * TabbedContentPane which ignores appearance (it has too much colors looking ugly / becoming unreadable).
169      * <p>
170      * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
171      * <br>
172      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
173      * <p>
174      * @version $Revision: 4006 $, $LastChangedDate: 2018-09-19 13:55:45 +0200 (Wed, 19 Sep 2018) $, by $Author: averbraeck $,
175      *          initial version 6 feb. 2018 <br>
176      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
177      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
178      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
179      */
180     static class AppearanceControlTabbedContentPane extends TabbedContentPane implements AppearanceControl
181     {
182         /** */
183         private static final long serialVersionUID = 20180206L;
184 
185         /**
186          * @param tabPlacement tabPlacement
187          */
188         public AppearanceControlTabbedContentPane(int tabPlacement)
189         {
190             super(tabPlacement);
191         }
192 
193         /** {@inheritDoc} */
194         @Override
195         public String toString()
196         {
197             return "AppearanceControlTabbedContentPane []";
198         }
199     }
200 
201 }