View Javadoc

    1   /*
    2    * @(#) Statusbar.java Oct 13, 2003
    3    *
    4    * Copyright (c) 2002-2005 Delft University of Technology Jaffalaan 5, 2628 BX
    5    * Delft, the Netherlands. All rights reserved.
    6    *
    7    * This software is proprietary information of Delft University of Technology
    8    * The code is published under the Lesser General Public License
    9    */
    10  package nl.tudelft.simulation.dsol.gui.panels;
    11
    12  import java.awt.BorderLayout;
    13  import java.awt.Color;
    14  import java.awt.Dimension;
    15  import java.rmi.RemoteException;
    16  import java.text.NumberFormat;
    17
    18  import javax.swing.BorderFactory;
    19  import javax.swing.BoxLayout;
    20  import javax.swing.JButton;
    21  import javax.swing.JPanel;
    22  import javax.swing.JTextField;
    23  import javax.swing.UIManager;
    24
    25  import nl.tudelft.simulation.dsol.experiment.Experiment;
    26  import nl.tudelft.simulation.dsol.gui.DSOLApplicationInterface;
    27  import nl.tudelft.simulation.dsol.gui.actions.FastForwardAction;
    28  import nl.tudelft.simulation.dsol.gui.actions.ResetAction;
    29  import nl.tudelft.simulation.dsol.gui.actions.StartAction;
    30  import nl.tudelft.simulation.dsol.gui.actions.StepAction;
    31  import nl.tudelft.simulation.dsol.gui.actions.StopAction;
    32  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
    33  import nl.tudelft.simulation.event.Event;
    34  import nl.tudelft.simulation.event.EventInterface;
    35  import nl.tudelft.simulation.event.EventListenerInterface;
    36  import nl.tudelft.simulation.logger.Logger;
    37
    38  /***
    39   * The StatusBar <br>
    40   * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft
    41   * University of Technology </a>, the Netherlands. <br>
    42   * See for project information <a
    43   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
    44   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
    45   * General Public License (LGPL) </a>, no warranty.
    46   *
    47   * @version $Revision: 1.4 $ $Date: 2005/01/13 16:28:54 $
    48   * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja">Peter Jacobs </a>
    49   */
    50  public class Statusbar extends JPanel implements EventListenerInterface
    51  {
    52
    53  	/*** the timeField of the statusBar */
    54  	private JTextField timeField = new JTextField();
    55
    56  	/*** the replicationField of the statusBar */
    57  	private JTextField replicationField = new JTextField();
    58
    59  	/*** the replicationField of the statusBar */
    60  	private JTextField treatmentField = new JTextField();
    61
    62  	/*** the parent DSOL Panel */
    63  	private DSOLApplicationInterface application = null;
    64
    65  	/*** the units */
    66  	private String units = null;
    67
    68  	/*** the simulatorTime */
    69  	private double simulatorTime = Double.NaN;
    70
    71  	/***
    72  	 * constructs a new Statusbar
    73  	 *
    74  	 * @param application the application
    75  	 */
    76  	public Statusbar(final DSOLApplicationInterface application)
    77  	{
    78  		super();
    79  		this.application = application;
    80  		this.initialize();
    81  		try
    82  		{
    83  			this.application.addListener(this,
    84  					DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT);
    85  			this.notify(new Event(
    86  					DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT,
    87  					this.application, this.application.getExperiment()));
    88  			if (this.application.getExperiment() != null
    89  					&& this.application.getExperiment().getSimulator() != null)
    90  			{
    91  				if (this.application.getExperiment().getSimulator()
    92  						.getReplication() != null)
    93  				{
    94  					this.notify(new Event(
    95  							SimulatorInterface.START_REPLICATION_EVENT,
    96  							this.application.getExperiment().getSimulator(),
    97  							this.application.getExperiment().getSimulator()
    98  									.getReplication()));
    99  				}
    100 			}
    101 		} catch (Exception exception)
    102 		{
    103 			Logger.warning(this, "Statusbar", exception);
    104 		}
    105 	}
    106
    107 	/***
    108 	 * initializes the statusbar
    109 	 */
    110 	private void initialize()
    111 	{
    112 		Color backgroundColor = UIManager.getColor("MenuBar.background");
    113 		this.setBackground(backgroundColor);
    114 		this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
    115 		JPanel controlPanel = new JPanel();
    116 		controlPanel.setBackground(backgroundColor);
    117 		controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.X_AXIS));
    118 		StepAction stepAction = new StepAction(this.application);
    119 		JButton stepButton = new JButton(stepAction);
    120 		stepButton.setText("");
    121 		stepButton.setBackground(backgroundColor);
    122 		stepButton.setToolTipText("steps the simulator");
    123 		stepButton.setBorder(BorderFactory.createEmptyBorder());
    124 		controlPanel.add(stepButton);
    125 		StartAction startAction = new StartAction(this.application);
    126 		JButton startButton = new JButton(startAction);
    127 		startButton.setText("");
    128 		startButton.setBackground(backgroundColor);
    129 		startButton.setToolTipText("starts the simulator");
    130 		startButton.setBorder(BorderFactory.createEmptyBorder());
    131 		controlPanel.add(startButton);
    132 		StopAction stopAction = new StopAction(this.application);
    133 		JButton stopButton = new JButton(stopAction);
    134 		stopButton.setText("");
    135 		stopButton.setBackground(backgroundColor);
    136 		stopButton.setToolTipText("stops the simulator");
    137 		stopButton.setBorder(BorderFactory.createEmptyBorder());
    138 		controlPanel.add(stopButton);
    139 		FastForwardAction fastForwardAction = new FastForwardAction(
    140 				this.application);
    141 		JButton fastForwardButton = new JButton(fastForwardAction);
    142 		fastForwardButton.setText("");
    143 		fastForwardButton.setBackground(backgroundColor);
    144 		fastForwardButton.setToolTipText("fast forwards the simulator");
    145 		fastForwardButton.setBorder(BorderFactory.createEmptyBorder());
    146 		controlPanel.add(fastForwardButton);
    147 		ResetAction resetAction = new ResetAction(this.application);
    148 		JButton resetButton = new JButton(resetAction);
    149 		resetButton.setText("");
    150 		resetButton.setBackground(backgroundColor);
    151 		resetButton.setToolTipText("resets the experiment");
    152 		resetButton.setBorder(BorderFactory.createEmptyBorder());
    153 		controlPanel.add(resetButton);
    154 		JPanel statusPanel = new JPanel();
    155 		statusPanel.setBackground(backgroundColor);
    156 		statusPanel.setBorder(BorderFactory.createEmptyBorder());
    157 		statusPanel.setLayout(new BorderLayout());
    158 		this.timeField.setBackground(Color.WHITE);
    159 		this.timeField.setPreferredSize(new Dimension(200, 12));
    160 		this.timeField.setEditable(false);
    161 		this.timeField.setText("Time:");
    162 		this.timeField.setToolTipText("The simulator time");
    163 		statusPanel.add(this.timeField, BorderLayout.WEST);
    164 		JPanel eastSide = new JPanel();
    165 		eastSide.setBackground(backgroundColor);
    166 		eastSide.setBorder(BorderFactory.createEmptyBorder());
    167 		eastSide.setLayout(new BorderLayout());
    168 		this.replicationField.setBackground(Color.WHITE);
    169 		this.replicationField.setEditable(false);
    170 		this.replicationField.setText("Rep(-/-)");
    171 		this.replicationField
    172 				.setToolTipText("The currently running replication");
    173 		this.treatmentField.setBackground(Color.WHITE);
    174 		this.treatmentField.setEditable(false);
    175 		this.treatmentField.setText("Treat(-/-)");
    176 		this.treatmentField.setToolTipText("The currently running treatment");
    177 		eastSide.add(this.replicationField, BorderLayout.WEST);
    178 		eastSide.add(this.treatmentField, BorderLayout.EAST);
    179 		statusPanel.add(eastSide, BorderLayout.EAST);
    180 		this.add(controlPanel);
    181 		this.add(statusPanel);
    182 		new TimeUpdater(this).start();
    183 	}
    184
    185 	/***
    186 	 * gets simulatorTime
    187 	 *
    188 	 * @return Returns the simulatorTime.
    189 	 */
    190 	protected double getSimulatorTime()
    191 	{
    192 		return this.simulatorTime;
    193 	}
    194
    195 	/***
    196 	 * gets timeField
    197 	 *
    198 	 * @return Returns the timeField.
    199 	 */
    200 	protected JTextField getTimeField()
    201 	{
    202 		return this.timeField;
    203 	}
    204
    205 	/***
    206 	 * gets units
    207 	 *
    208 	 * @return Returns the units.
    209 	 */
    210 	protected String getUnits()
    211 	{
    212 		return this.units;
    213 	}
    214
    215 	/***
    216 	 * @see nl.tudelft.simulation.event.EventListenerInterface
    217 	 *      #notify(nl.tudelft.simulation.event.EventInterface)
    218 	 */
    219 	public void notify(final EventInterface event) throws RemoteException
    220 	{
    221 		if (event.getSource() instanceof DSOLApplicationInterface)
    222 		{
    223 			if (event.getType().equals(
    224 					DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT))
    225 			{
    226 				if (event.getContent() != null)
    227 				{
    228 					Experiment experiment = (Experiment) event.getContent();
    229 					experiment.addListener(this,
    230 							Experiment.SIMULATOR_CHANGED_EVENT);
    231 					this.notify(new Event(Experiment.SIMULATOR_CHANGED_EVENT,
    232 							experiment, experiment.getSimulator()));
    233 				}
    234 				return;
    235 			}
    236 		} else if (event.getSource() instanceof Experiment)
    237 		{
    238 			if (event.getType().equals(Experiment.SIMULATOR_CHANGED_EVENT))
    239 			{
    240 				this.simulatorTime = Double.NaN;
    241 				if (event.getContent() != null)
    242 				{
    243 					SimulatorInterface simulator = (SimulatorInterface) event
    244 							.getContent();
    245 					simulator.addListener(this,
    246 							SimulatorInterface.START_REPLICATION_EVENT);
    247 					simulator.addListener(this,
    248 							SimulatorInterface.TIME_CHANGED_EVENT);
    249 					this.treatmentField.setText("Treat(-/-)");
    250 					this.replicationField.setText("Rep(-/-)");
    251 					this.timeField.setText("Time:");
    252 					if (simulator.getReplication() != null)
    253 					{
    254 						this.notify(new Event(
    255 								SimulatorInterface.START_REPLICATION_EVENT,
    256 								simulator, simulator.getReplication()));
    257 					}
    258 					this.validate();
    259 					this.repaint();
    260 				}
    261 				return;
    262 			}
    263 		}
    264 		if (event.getSource() instanceof SimulatorInterface)
    265 		{
    266 			if (event.getType().equals(
    267 					SimulatorInterface.START_REPLICATION_EVENT))
    268 			{
    269 				this.units = this.application.getExperiment().getSimulator()
    270 						.getReplication().getRunControl().getTreatment()
    271 						.getTimeUnit().toString();
    272 				int rep = ((SimulatorInterface) event.getSource())
    273 						.getReplication().getNumber() + 1;
    274 				int nrrep = ((SimulatorInterface) event.getSource())
    275 						.getReplication().getRunControl().getReplications().length;
    276 				int treat = ((SimulatorInterface) event.getSource())
    277 						.getReplication().getRunControl().getTreatment()
    278 						.getNumber() + 1;
    279 				int nrtreat = ((SimulatorInterface) event.getSource())
    280 						.getReplication().getRunControl().getTreatment()
    281 						.getExperiment().getTreatments().length;
    282 				this.replicationField.setText("Rep(" + rep + "/" + nrrep + ")");
    283 				this.treatmentField.setText("Treat(" + treat + "/" + nrtreat
    284 						+ ")");
    285 				this.validate();
    286 				this.repaint();
    287 				return;
    288 			}
    289 			if (event.getType().equals(SimulatorInterface.TIME_CHANGED_EVENT))
    290 			{
    291 				this.simulatorTime = ((Double) event.getContent())
    292 						.doubleValue();
    293 			}
    294 		}
    295 	}
    296
    297 	/***
    298 	 * The TimeUpdater
    299 	 */
    300 	private class TimeUpdater extends Thread
    301 	{
    302
    303 		/*** formatter */
    304 		private NumberFormat formatter = NumberFormat.getInstance();
    305
    306 		/*** the last length of the string */
    307 		private int maxLength = 0;
    308
    309 		/*** the source */
    310 		private Statusbar source = null;
    311
    312 		/***
    313 		 * constructs a new
    314 		 *
    315 		 * @param source the statusbar
    316 		 */
    317 		public TimeUpdater(final Statusbar source)
    318 		{
    319 			super();
    320 			this.source = source;
    321 		}
    322
    323 		/***
    324 		 * @see java.lang.Runnable#run()
    325 		 */
    326 		public void run()
    327 		{
    328 			while (true)
    329 			{
    330 				try
    331 				{
    332 					String formattedTime = "Time:"
    333 							+ this.formatter.format(this.source
    334 									.getSimulatorTime()) + " "
    335 							+ this.source.getUnits();
    336 					this.source.getTimeField().setText(formattedTime);
    337 					if (formattedTime.length() > this.maxLength)
    338 					{
    339 						this.maxLength = formattedTime.length();
    340 						this.source.validate();
    341 					}
    342 					Thread.sleep(250);
    343 				} catch (Throwable throwable)
    344 				{
    345 					throwable = null;
    346 				}
    347 			}
    348 		}
    349 	}
    350 }