/* File: RunDialog.java
 * Author: Jason Gookins
 * Description: This class creates the popup dialog for inputting the number of runs to perform on a network.
 */

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class RunDialog extends JDialog implements ActionListener
{
	/************************
	 ** Instance Variables **
	 ************************/

	private JTextField numberOfRunsTextField;
	private JCheckBox minimizeCheckBox;
	private JButton okayButton;
	private JButton cancelButton;
	private int numberOfRuns;
	private boolean minimizeChange;



	/**********************
	 ** Main Constructor **
	 **********************/

	RunDialog(JFrame parent)
	{
		super(parent, "Run Network", true);

		this.numberOfRuns = 0;
		this.minimizeChange = false;

		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		setPreferredSize(new Dimension(240, 150));
		setResizable(false);

		JPanel dialogPanel  = new JPanel(null);

		JLabel numberOfRunsLabel = new JLabel("Number of runs:");
		numberOfRunsLabel.setBounds(10, 10, 120, 30);
		dialogPanel.add(numberOfRunsLabel);
		numberOfRunsTextField = new JTextField("100");
		numberOfRunsTextField.setBounds(125, 10,  100, 30);
		dialogPanel.add(numberOfRunsTextField);

		minimizeCheckBox = new JCheckBox("Minimize Change");
		minimizeCheckBox.setBounds(45, 45, 150, 30);
		dialogPanel.add(minimizeCheckBox);

		okayButton = new JButton("Okay");
		okayButton.setBounds(35, 90, 75, 25);
		okayButton.addActionListener(this);
		dialogPanel.add(okayButton);

		cancelButton = new JButton("Cancel");
		cancelButton.setBounds(130, 90, 75, 25);
		cancelButton.addActionListener(this);
		dialogPanel.add(cancelButton);

		setContentPane(dialogPanel);

		pack();
		numberOfRunsTextField.requestFocusInWindow();
		numberOfRunsTextField.selectAll();
		setLocationRelativeTo(parent);
		setVisible(true);
	}



	/***************
	 ** Accessors **
	 ***************/

	public int getNumberOfRuns()
	{
		return this.numberOfRuns;
	}

	public boolean getMinimizeChange()
	{
		return this.minimizeChange;
	}



	/*******************
	 ** Action Events **
	 *******************/

	public void actionPerformed(ActionEvent evt)
	{
		if (evt.getSource().equals(okayButton))
		{
			String runsText = numberOfRunsTextField.getText();

			if (runsText != null)
			{
				numberOfRuns = Integer.parseInt(runsText);

				if (minimizeCheckBox.isSelected())
				{
					minimizeChange = true;
				}

				dispose();
			}
			else
			{
				numberOfRunsTextField.requestFocusInWindow();
				numberOfRunsTextField.selectAll();
				JOptionPane.showMessageDialog(this, "Number of runs cannot be blank.", "Run Error", JOptionPane.ERROR_MESSAGE);
			}
		}
		else if (evt.getSource().equals(cancelButton))
		{
			dispose();
		}
	}
}
