package sim.lib.wires;

import java.awt.*;
import java.awt.event.*;
import sim.util.SimSeparator;

public class SplitterProperties extends Container
		implements ActionListener, FocusListener {

	private TextField editBus = new TextField(8);
	private TextField from = new TextField(8);
	private TextField to = new TextField(8);
	private Label pins = new Label("Pins");
	private int oldBus;
	private int oldTo;
	private int oldFrom;
	private boolean fromEqualTo;

	public SplitterProperties(int paramInt1, int paramInt2, int paramInt3, boolean paramBoolean) {
		setLayout(new BorderLayout(0, 15));
		this.oldBus = paramInt1;
		this.oldFrom = paramInt2;
		this.oldTo = paramInt3;
		this.fromEqualTo = paramBoolean;
		Panel localPanel1 = new Panel(new BorderLayout());
		localPanel1.add(this.pins, "West");
		localPanel1.add(new SimSeparator(), "Center");
		add(localPanel1, "North");
		localPanel1 = new Panel(new FlowLayout(2, 0, 0));
		localPanel1.add(new Label("Bus Size"));
		localPanel1.add(this.editBus);
		add(localPanel1, "Center");
		localPanel1 = new Panel(new GridBagLayout());
		Panel localPanel2 = new Panel(new FlowLayout(2, 0, 0));
		localPanel2.add(new Label("From"));
		localPanel2.add(this.from);
		GridBagConstraints localGridBagConstraints = new GridBagConstraints();
		localGridBagConstraints.anchor = 17;
		localGridBagConstraints.gridx = 0;
		localGridBagConstraints.gridy = 0;
		localGridBagConstraints.gridheight = 1;
		localGridBagConstraints.gridwidth = 1;
		localGridBagConstraints.weightx = 1.0D;
		localGridBagConstraints.weighty = 0.0D;
		localGridBagConstraints.fill = 0;
		localPanel1.add(localPanel2, localGridBagConstraints);
		localPanel2 = new Panel(new FlowLayout(0, 0, 0));
		localPanel2.add(new Label("To"));
		localPanel2.add(this.to);
		localGridBagConstraints.gridx = 1;
		localGridBagConstraints.anchor = 13;
		localPanel1.add(localPanel2, localGridBagConstraints);
		add(localPanel1, "South");
		this.editBus.addActionListener(this);
		this.from.addActionListener(this);
		this.to.addActionListener(this);
		this.editBus.addFocusListener(this);
		this.from.addFocusListener(this);
		this.to.addFocusListener(this);
		this.editBus.setText(Integer.toString(paramInt1));
		this.from.setText(Integer.toString(paramInt2));
		this.to.setText(Integer.toString(paramInt3));
	}

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

	public int getBusSize() {
		try {
			int i = Integer.valueOf(this.editBus.getText()).intValue();
			if ((i >= 2) && (i != this.oldBus)) {
				this.oldBus = i;
				this.oldFrom = 0;
				this.oldTo = (this.oldBus - 1);
				this.from.setText("0");
				this.to.setText(Integer.toString(this.oldTo));
			} else {
				this.editBus.setText(Integer.toString(this.oldBus));
			}
		} catch (NumberFormatException localNumberFormatException) {
			this.editBus.setText(Integer.toString(this.oldBus));
		}
		return this.oldBus;
	}

	public int getFrom() {
		try {
			int i = Integer.valueOf(this.from.getText()).intValue();
			if ((i >= 0) && ((i < this.oldTo) || ((i == this.oldTo) && (this.fromEqualTo)))) {
				this.oldFrom = i;
			} else {
				this.from.setText(Integer.toString(this.oldFrom));
			}
		} catch (NumberFormatException localNumberFormatException) {
			this.from.setText(Integer.toString(this.oldFrom));
		}
		return this.oldFrom;
	}

	public int getTo() {
		try {
			int i = Integer.valueOf(this.to.getText()).intValue();
			if ((i < this.oldBus) && ((i > this.oldFrom) || ((i == this.oldFrom) && (this.fromEqualTo)))) {
				this.oldTo = i;
			} else {
				this.to.setText(Integer.toString(this.oldTo));
			}
		} catch (NumberFormatException localNumberFormatException) {
			this.to.setText(Integer.toString(this.oldTo));
		}
		return this.oldTo;
	}

	public void focusLost(FocusEvent paramFocusEvent) {
		TextField localTextField = (TextField) paramFocusEvent.getSource();
		if (localTextField == this.editBus) {
			getBusSize();
			return;
		}
		if (localTextField == this.from) {
			getFrom();
			return;
		}
		if (localTextField == this.to) {
			getTo();
		}
	}

	public void actionPerformed(ActionEvent paramActionEvent) {
		TextField localTextField = (TextField) paramActionEvent.getSource();
		if (localTextField == this.editBus) {
			getBusSize();
			return;
		}
		if (localTextField == this.from) {
			getFrom();
			return;
		}
		if (localTextField == this.to) {
			getTo();
		}
	}

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

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

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

	public void focusGained(FocusEvent paramFocusEvent) {
	}
}
