// Buy/Sell Dialog

package COM.xinit.demon.co.uk.3tier.tradertool;

import java.awt.*;
import database.*;

public class BuySellDialog extends Dialog {


	boolean		buy = false;
	Button		bOK = new Button("OK");
	Button		bCancel = new Button("Cancel");
	Label		l1 = new Label();
	Label		l2 = new Label();
	TextField	tf1 = new TextField("0");
	int	num = 0;


	// Tester
	public static void main(String argc[]) {


		System.out.print("The number is " + BuySellDialog.getNumber("SUN-1", true));

		System.exit(0);
		
	}

	/**
	 * Create dialog and return the number entered
	 */
	public static int getNumber(String symbol, boolean buy) {

		Frame		f = new Frame();
		int		ret = 0;

		f.pack();
//		f.show();

		BuySellDialog	d = new BuySellDialog(f, symbol, buy);

		// Wait for handleEvent to wait me up
		synchronized(d) {
			try {
				d.wait();
			}
			catch (InterruptedException e) {
			}
		}

		ret = d.num;
		d.setVisible(false);
		d.dispose();

		f.dispose();


		return ret;

	}


//	public BuySellDialog(Frame f) {
//
//		this(f, false);
//	}

	public BuySellDialog(Frame f, String symbol, boolean buy) {

		super(f);

		if (buy) {
			this.setTitle("Buy");
			l2.setText("Number to buy: ");
		}
		else {
			this.setTitle("Sell");
			l2.setText("Number to sell: ");
		}

		l1.setText(symbol);

		
		this.setLayout(new BorderLayout());

		this.add("North",l1);
		this.add("West",l2);
		this.add("East",tf1);

		Panel p = new Panel();
		p.setLayout(new FlowLayout());
		p.add(bOK);
		p.add(bCancel);
		this.add("South",p);
		
		this.pack();

//		this.setModal(true);
//		this.setResizable(false);

		this.setVisible(true);


	}



//	public boolean action(Event event, Object arg) {
//
//
//
//		return super.action(event, arg);
//
//	}


	public synchronized boolean handleEvent(Event event) {

		switch (event.id) {

			case Event.ACTION_EVENT:
				if (event.target == bCancel) {
					num = 0;
					this.setVisible(false);
					notifyAll();
				}
					

				if (event.target == bOK) {
//					System.err.println("Event arg: " + event.arg);
					num = (new Integer(tf1.getText())).intValue();
					System.err.println("TextField " + tf1.getText() + " " + num);
					this.setVisible(false);
					notifyAll();
				}

		}

		return super.handleEvent(event);

	}
}