package org.lucci.madhoc.gui.runtime;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.lucci.madhoc.gui.SimulationRuntime;
public class PlayPauseComponent extends RuntimeComponentElement
{
private JButton startButton = new JButton();
private JButton pauseButton = new JButton();
private JTextField textField = new JTextField("1", 9);
private JComboBox combo = new JComboBox();
public PlayPauseComponent()
{
startButton.setIcon(new ImageIcon(getClass().getResource("play.gif")));
startButton.setToolTipText("<html>(Re)<b>starts</b> the simulation and stop after <i>n</i> iterations.<br>The number <i>n</i> of iterations is given by the text field below.");
pauseButton.setIcon(new ImageIcon(getClass().getResource("pause.gif")));
pauseButton.setToolTipText("<html><b>Waits</b> until the end of the current iteration then <b>stops</b> the simulation.<br>Press <i>Start</i> if you want to start the iteration again.");
textField.setHorizontalAlignment(JTextField.CENTER);
textField.setToolTipText("<html><b>Specifies</b> the number of iterations executed by pressing on the <i>start</i> button.");
combo.addItem("iteration(s)");
combo.addItem("second(s)");
JLabel label = new JLabel("Go for: ");
label.setHorizontalAlignment(JLabel.RIGHT);
ActionListener actionHandler = new ActionHandler();
startButton.addActionListener(actionHandler);
pauseButton.addActionListener(actionHandler);
textField.addActionListener(actionHandler);
JPanel centerPanel = new JPanel(new GridBagLayout());
{
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.anchor = GridBagConstraints.EAST;
constraints.insets = new Insets(5, 0, 5, 5);
centerPanel.add(pauseButton, constraints);
}
{
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 1;
constraints.gridy = 0;
constraints.anchor = GridBagConstraints.WEST;
constraints.insets = new Insets(5, 5, 5, 0);
centerPanel.add(startButton, constraints);
}
JPanel southPanel = new JPanel();
southPanel.setLayout(new BoxLayout(southPanel, BoxLayout.X_AXIS));
southPanel.add(label);
southPanel.add(textField);
southPanel.add(combo);
setLayout(new BorderLayout());
add(centerPanel, BorderLayout.CENTER);
add(southPanel, BorderLayout.SOUTH);
}
public class ActionHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();
try
{
Integer.parseInt(textField.getText());
}
catch (NumberFormatException ex)
{
textField.setText("1");
}
double n = Integer.parseInt(textField.getText());
double numberOfIterations = 1;
String unit = (String) combo.getSelectedItem();
if (unit.startsWith("iteration"))
{
numberOfIterations = n;
}
else if (unit.startsWith("second"))
{
numberOfIterations = (int) (n / getRuntimeComponent().getRuntime().getSimulation().getResolution());
}
else
{
throw new IllegalStateException();
}
if (source == startButton || source == textField)
{
getRuntimeComponent().getRuntime().setNumberOfStepToExecute((int) numberOfIterations);
getRuntimeComponent().getRuntime().setRequiredState(SimulationRuntime.STATE_RUNNING);
}
else if (source == pauseButton)
{
getRuntimeComponent().getRuntime().setRequiredState(SimulationRuntime.STATE_SLEEPING);
}
}
}
public void stateChanged()
{
if (getRuntimeComponent().getRuntime().getState() == SimulationRuntime.STATE_RUNNING)
{
startButton.setEnabled(false);
pauseButton.setEnabled(true);
textField.setEditable(false);
}
else if (getRuntimeComponent().getRuntime().getState() == SimulationRuntime.STATE_SLEEPING)
{
startButton.setEnabled(true);
pauseButton.setEnabled(false);
textField.setEditable(true);
}
else if (getRuntimeComponent().getRuntime().getState() == SimulationRuntime.STATE_COMPLETED)
{
startButton.setEnabled(false);
pauseButton.setEnabled(false);
textField.setEditable(false);
}
else
{
throw new IllegalStateException("unknow runtime state");
}
}
public void iterationPerformed()
{
}
}