package sim.lib.others;

import java.awt.*;
import java.awt.event.*;

import sim.util.SimSeparator;

public class TextBoxProperties extends Container implements ActionListener, FocusListener {

	private TextField editBus = new TextField(5);
	private TextField editDelay = new TextField(10);
	private double oldDelay;
	private int oldBus;
	private Label inputBus = new Label("Input Bus");
	private Label simulation = new Label("Proporgation Delay");

	public TextBoxProperties(int bus, double delay) {
		super();
		this.setLayout(new GridBagLayout());

		this.oldBus = bus;
		this.editBus.addActionListener(this);
		this.editBus.addFocusListener(this);
		this.editBus.setText(Integer.toString(bus));

		this.oldDelay = delay;
		this.editDelay.addActionListener(this);
		this.editDelay.addFocusListener(this);
		this.editDelay.setText(Double.toString(delay));

		// pins
		Panel big = new Panel(new BorderLayout(0, 15));

		Panel p = new Panel(new BorderLayout());
		p.add(this.inputBus, BorderLayout.WEST);
		p.add(new SimSeparator(), BorderLayout.CENTER);
		big.add(p, BorderLayout.NORTH);

		p = new Panel(new GridBagLayout());

		GridBagConstraints c = new GridBagConstraints();

		c.gridy = 0;
		c.gridwidth = 1;
		c.gridheight = 1;
		c.anchor = GridBagConstraints.WEST;

		c.weighty = 0;

		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 1;
		c.gridx = 0;

		p.add(this.editBus, c);
		big.add(p, BorderLayout.CENTER);

		c.gridx = 0;
		c.insets = new Insets(0, 0, 15, 0);
		c.weightx = 1;
		c.fill = GridBagConstraints.HORIZONTAL;

		this.add(big, c);

		// simulation
		big = new Panel(new BorderLayout(0, 15));

		p = new Panel(new BorderLayout());
		p.add(this.simulation, BorderLayout.WEST);
		p.add(new SimSeparator(), BorderLayout.CENTER);
		big.add(p, BorderLayout.NORTH);

		p = new Panel(new GridBagLayout());
		c = new GridBagConstraints();

		c.gridy = 1;
		c.gridwidth = 1;
		c.gridheight = 1;
		c.anchor = GridBagConstraints.WEST;

		c.weighty = 0;

		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 1;
		c.gridx = 0;

		p.add(this.editDelay, c);
		big.add(p, BorderLayout.CENTER);

		c.gridx = 0;
		c.insets = new Insets(0, 0, 15, 0);
		c.weightx = 1;
		c.fill = GridBagConstraints.HORIZONTAL;

		this.add(big, c);
	}

	public void addNotify() {
		super.addNotify();
		this.setSize(290, this.editDelay.getPreferredSize().height * 2 + this.editBus.getPreferredSize().height * 2 + 45);
	}

	public void actionPerformed(ActionEvent e) {
		TextField source = (TextField) e.getSource();

		if (source == this.editDelay) {
			this.getDelay();
		} else if (source == this.editBus) {
			this.getBusSize();
		}
	}

	public void focusGained(FocusEvent e) {
	}

	public void focusLost(FocusEvent e) {
		TextField source = (TextField) e.getSource();

		if (source == this.editDelay) {
			this.getDelay();
		} else if (source == this.editBus) {
			this.getBusSize();
		}

		source.setText(source.getText());
	}

	public int getBusSize() {
		int newBus;

		try {
			newBus = Integer.valueOf(this.editBus.getText()).intValue();

			if (newBus > 0) {
				this.oldBus = newBus;
			} else {
				this.editBus.setText(Integer.toString(this.oldBus));
			}
		} catch (NumberFormatException nfe) {
			this.editBus.setText(Integer.toString(this.oldBus));
		}

		return this.oldBus;
	}

	public double getDelay() {
		double newDelay;

		try {
			newDelay = Double.valueOf(this.editDelay.getText()).doubleValue();

			if (newDelay >= 0) {
				this.oldDelay = newDelay;
			} else {
				this.editDelay.setText(Double.toString(this.oldBus));
			}
		} catch (NumberFormatException nfe) {
			this.editDelay.setText(Double.toString(this.oldBus));
		}

		return this.oldDelay;
	}

	public Dimension getPreferredSize() {
		return this.getSize();
	}

	public Dimension getMinimumSize() {
		return this.getSize();
	}

	public Dimension getMaximumSize() {
		return this.getSize();
	}
}
