
import java.awt.*;

//------------------------------------------------------------------------------
// DistributionDialog.java:
//		Creates a dialog box for displaying extra component details
//------------------------------------------------------------------------------

public class DistributionDialog extends Dialog
{
	private SetupButton parent;
	private dataCompMember data;
	
	private Button ok_button;
	private Button cancel_button;
	private Button help_button;
	
	private Choice x2;
	private TextField likely;
	private TextField low;
	private TextField high;
	private TextField sd;

	// Constructors
	//--------------------------------------------------------------------------
	// From a field
	public DistributionDialog (SetupButton f,dataCompMember dc,boolean modal)
	{
	    super(new Frame(),"Distribution Details",modal);

		parent = f;
		data = dc;
		buildDialog("Input distribution","","");
	}

	private void buildDialog(String title, String s1, String s2)
	{
		//add("North",new TextField(""));
		
		this.setFont(new Font("Helvetica",Font.PLAIN,12));

		// Fields
		String blank = "    ";
		likely = new TextField(blank);
		low = new TextField(blank);
		high = new TextField(blank);
		sd = new TextField(blank);
		
		// Buttons
		Panel buttonPanel = new Panel();
		ok_button = new Button("OK");
		cancel_button = new Button("Cancel");
		help_button = new Button("Help");
		buttonPanel.add(ok_button);
		buttonPanel.add(cancel_button);
		//buttonPanel.add(help_button);
		add("South",buttonPanel);

	    if(System.getProperty("java.vendor").compareTo("Netscape Communications Corporation") == 0 && System.getProperty("os.name").compareTo("SunOS") == 0)
            resize(370,320);
     	else
    	    resize(370,270);

		x2 = new Choice();
		x2.addItem("Fixed Value");
		x2.addItem("Triangular");
		x2.addItem("Normal");
		x2.addItem("Uniform");
		x2.addItem("Lognormal");
		
		PanelFormat pf = new PanelFormat();
		Panel body_panel = new Panel();
		//body_panel.setBackground(Color.green);
		pf.initializePanel(body_panel);
		if(data != null && data.choicevalue() != null)
			pf.Words(body_panel,16,"BOLD",data.choicevalue());
		else
			pf.Words(body_panel,16,"BOLD",title);
		pf.LabelChoice(body_panel,new Label("Distribution type"),x2);
		pf.LabelField(body_panel,new Label("Most likely value"),likely);
		pf.LabelField(body_panel,new Label("Low value"),low);
		pf.LabelField(body_panel,new Label("High value"),high);
		pf.LabelField(body_panel,new Label("Standard deviation"),sd);
		pf.Space(body_panel);
		
		add("Center",body_panel);
	}

    public boolean handleEvent(Event e) {
        if (e.id == Event.WINDOW_DESTROY && e.target == this) {
            dispose();
    		return true;
        }
        return super.handleEvent(e);
    }

	// Catch events in the dialog box
	//--------------------------------------------------------------------------
	public boolean action(Event event,Object obj)
	{
		Object target = event.target;

		if(target instanceof Button)
		{
			Button button = (Button)target;
			if(target == ok_button)
			{
			        saveData();
				    dispose();
			}
			if(target == cancel_button)
			{
				dispose();
			}
			if(target == help_button)
			{
			}
			return true;
		}

		return false;
	}

	public void popup()
	{
		loadData();
		show();
	}

	// Load data into all the fields in this dialog
	public void loadData()
	{
		System.out.println("in load");
		if(data == null) {
			System.out.println("data is null");
			return;
		}
		
	        // Load all values
			Double z;
            x2.select(data.dist());
			z = new Double(data.doublevalue());
			likely.setText(z.toString());
			likely.setEditable(false);
			z = new Double(data.doublevalue_low());
			low.setText(z.toString());
			z = new Double(data.doublevalue_high());
			high.setText(z.toString());
			z = new Double(data.doublevalue_sd());
			sd.setText(z.toString());
	}

	// Save data in all the fields in this dialog
	public void saveData()
	{
	        // Save all values
			Double z;
            data.setdistvalue(x2.getSelectedIndex());
					z = new Double(low.getText());
					data.setlowvalue(z.doubleValue());
					z = new Double(high.getText());
					data.sethighvalue(z.doubleValue());
					z = new Double(sd.getText());
					data.setsdvalue(z.doubleValue());
	} 

}