package sim.lib.outputs;

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

import sim.util.SimSeparator;

public class PinProperties extends Container implements ActionListener {

	private TextField editBus = new TextField(10);
	private TextField editName = new TextField(10);
	private Label name = new Label("Name");
	private Label bus = new Label("Bus Size");
	private int old;

	public PinProperties(int initbus, String initlabel) {
		super();
		this.setLayout(new GridBagLayout());


		this.editName.setText(initlabel);
		this.editBus.setText(Integer.toString(initbus));

		this.editName.addActionListener(this);
		this.editBus.addActionListener(this);

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

		Panel p = new Panel(new BorderLayout());
		p.add(this.name, 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.editName, c);
		big.add(p, BorderLayout.CENTER);

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

		this.add(big, c);

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

		p = new Panel(new BorderLayout());
		p.add(this.bus, 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 = 2;
		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, 0, 0);
		c.weightx = 1;
		c.fill = GridBagConstraints.HORIZONTAL;

		this.add(big, c);

	}

	public void addNotify() {
		super.addNotify();
		this.setSize(290, 7 * this.name.getPreferredSize().height);
	}

	public void actionPerformed(ActionEvent e) {
		int newBus;

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

			if (newBus < 2) {
				this.editBus.setText(Integer.toString(this.old));
			} else {
				this.old = newBus;
			}
		} catch (NumberFormatException nfe) {
			this.editBus.setText(Integer.toString(this.old));
		}

	}

	public int getBusSize() {
		int newBus;

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

			if (newBus >= 2) {
				this.old = newBus;
			}
		} catch (NumberFormatException nfe) {
		}

		return this.old;
	}

	public int getPinBusSize() {
		int newBus;

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

			if (newBus >= 1) {
				this.old = newBus;
			}
		} catch (NumberFormatException nfe) {
		}

		return this.old;
	}

	public String getLabel() {

		return this.editName.getText();
	}

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

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

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