/*
 Copyright (c) 1997 by Columbia University. All rights reserved.
 */

import java.awt.*;
import tjt.util.MsgDialog;
import symantec.itools.awt.BorderPanel;
import symantec.itools.awt.util.ProgressBar;

/** 
 * GUI Frame that is displayed at startup. It enables the user
 * to chose between the three current modes of operations.
 * When the user clicks the 'start' button this object calls 
 * one of the execute methods in the NetTest Object. StartGUI also
 * features a progress bar which is nice since some of the operations
 * take a long time especially when operating in continuous mode.
 * @version 1.0 31 Aug 1998
 * @author Terrence Truta
 */
public class StartGUI extends Frame
{
	public StartGUI(NetTest app)
	{


		// This code is automatically generated by Visual Cafe when you add
		// components to the visual environment. It instantiates and initializes
		// the components. To modify the code, only use code syntax that matches
		// what Visual Cafe can generate, or Visual Cafe may be unable to back
		// parse your Java file into its visual environment.
		//{{INIT_CONTROLS
		setLayout(null);
		setVisible(false);
		setSize(496,403);
		setBackground(new Color(12632256));
		label2 = new java.awt.Label("Remote Host:");
		label2.setBounds(16,12,144,24);
		add(label2);
		btnStart = new java.awt.Button();
		btnStart.setLabel("Start");
		btnStart.setBounds(292,360,88,30);
		btnStart.setBackground(new Color(12632256));
		add(btnStart);
		btnClose = new java.awt.Button();
		btnClose.setLabel("Close");
		btnClose.setBounds(396,360,88,30);
		btnClose.setBackground(new Color(12632256));
		add(btnClose);
		cmbHost = new java.awt.TextField();
		cmbHost.setText("cunix.columbia.edu");
		cmbHost.setBounds(16,36,185,30);
		add(cmbHost);
		borderPanel1 = new symantec.itools.awt.BorderPanel();
		borderPanel1.setLayout(null);
		borderPanel1.setBounds(8,108,484,152);
		add(borderPanel1);
		Group1 = new CheckboxGroup();
		rdoStandard = new java.awt.Checkbox("Standard (uses your platform's traceroute program)", Group1, true);
		rdoStandard.setBounds(10,12,412,32);
		borderPanel1.add(rdoStandard);
		rdoThorough = new java.awt.Checkbox("Thorough (uses traceroute and ping)", Group1, false);
		rdoThorough.setBounds(10,48,416,36);
		borderPanel1.add(rdoThorough);
		chkAutomation = new java.awt.Checkbox("Continuous (pings host continuously)", Group1, false);
		chkAutomation.setBounds(10,88,268,36);
		borderPanel1.add(chkAutomation);
		btnAutoOptions = new java.awt.Button();
		btnAutoOptions.setLabel("Options...");
		btnAutoOptions.setBounds(290,92,88,30);
		btnAutoOptions.setBackground(new Color(12632256));
		borderPanel1.add(btnAutoOptions);
		btnAutoOptions.setEnabled(false);
		label1 = new java.awt.Label("Type of test");
		label1.setBounds(24,88,130,17);
		add(label1);
		progressBar1 = new symantec.itools.awt.util.ProgressBar();
		progressBar1.setBounds(16,300,468,41);
		add(progressBar1);
		lblStatus = new java.awt.Label("");
		lblStatus.setBounds(16,276,245,21);
		add(lblStatus);
		setTitle("NetTest");
		//}}

        this.app = app;

		//{{INIT_MENUS
		//}}

		//{{REGISTER_LISTENERS
		SymWindow aSymWindow = new SymWindow();
		this.addWindowListener(aSymWindow);
		SymItem lSymItem = new SymItem();
		chkAutomation.addItemListener(lSymItem);
		SymAction lSymAction = new SymAction();
		btnClose.addActionListener(lSymAction);
		btnStart.addActionListener(lSymAction);
		btnAutoOptions.addActionListener(lSymAction);
		//}}
	}

	
		


    /**
     * Shows or hides the component depending on the boolean flag b.
     * @param b  if true, show the component; otherwise, hide the component.
     * @see java.awt.Component#isVisible
     */
    public void setVisible(boolean b)
	{
		if(b)
		{
			setLocation(50, 50);
		}
		super.setVisible(b);
	}

	static public void main(String args[])
	{
		(new StartGUI(null)).setVisible(true);
	}

	public void addNotify()
	{
	    // Record the size of the window prior to calling parents addNotify.
	    Dimension d = getSize();

		super.addNotify();

		if (fComponentsAdjusted)
			return;

		// Adjust components according to the insets
		setSize(getInsets().left + getInsets().right + d.width, getInsets().top + getInsets().bottom + d.height);
		Component components[] = getComponents();
		for (int i = 0; i < components.length; i++)
		{
			Point p = components[i].getLocation();
			p.translate(getInsets().left, getInsets().top);
			components[i].setLocation(p);
		}
		fComponentsAdjusted = true;
	}

	/** returns the host that the user entered */
	public String getHost() {
		return cmbHost.getText();
	}

	/** Displays a message box to the user.
	 * @param msg - The message to display to the user
	 */
  public void displayErrMsg(String msg) {
		MsgDialog ed = new MsgDialog(this, "Error", msg, true);
			ed.show();
			return;
	}

	/** Increments the progrss bar
	 * @param int - Amount to increment the progress bar from its current fill position.
	 */
	public void incrementProgressBar(int value) {
			debugPrint("Incrementing Progress Bar");
			try {
				progressBar1.setValue(progressBar1.getValue() + value);
			}
			catch(Exception e) { debugPrint("Error incrementing progress bar"); }
			progressBar1.paint(progressBar1.getGraphics());
			//this.paint(this.getGraphics());
			
	}

	/** Resets the progress bar to 0 (empty)
	 */
	public void resetProgressBar() {
		try {
			progressBar1.setValue(0);
		}
		catch (Exception e) { }
	}


    // Used for addNotify check.
	boolean fComponentsAdjusted = false;
    private static final int INITIAL = 0;
    private static final int RUNNING = 1;
	//{{DECLARE_CONTROLS
	java.awt.Label label2;
	java.awt.Button btnStart;
	java.awt.Button btnClose;
	java.awt.TextField cmbHost;
	symantec.itools.awt.BorderPanel borderPanel1;
	java.awt.Checkbox rdoStandard;
	CheckboxGroup Group1;
	java.awt.Checkbox rdoThorough;
	java.awt.Checkbox chkAutomation;
	java.awt.Button btnAutoOptions;
	java.awt.Label label1;
	symantec.itools.awt.util.ProgressBar progressBar1;
	java.awt.Label lblStatus;
	//}}

    NetTest app;
		AutoOptions autoOptions;
		AutoDlg ad;
	//{{DECLARE_MENUS
	//}}

	class SymWindow extends java.awt.event.WindowAdapter
	{
		public void windowClosing(java.awt.event.WindowEvent event)
		{
			Object object = event.getSource();
			if (object == StartGUI.this)
				Frame1_WindowClosing(event);
		}
	}

	void Frame1_WindowClosing(java.awt.event.WindowEvent event)
	{
		setVisible(false);		 // hide the Frame
	}

	class SymItem implements java.awt.event.ItemListener
	{
		public void itemStateChanged(java.awt.event.ItemEvent event)
		{
			Object object = event.getSource();
			if (object == chkAutomation)
				chkAutomation_ItemStateChanged(event);
		}
	}

	void chkAutomation_ItemStateChanged(java.awt.event.ItemEvent event)
	{
		// to do: code goes here.

		//{{CONNECTION
		// Enable the Button on condition... Is Checkbox On?
		btnAutoOptions.setEnabled(chkAutomation.getState());
		//}}
		
	}

	class SymAction implements java.awt.event.ActionListener
	{
		public void actionPerformed(java.awt.event.ActionEvent event)
		{
			Object object = event.getSource();
			if (object == btnClose)
				btnClose_ActionPerformed(event);
			else if (object == btnStart)
				btnTrace_ActionPerformed(event);
			else if (object == btnAutoOptions)
				btnAutoOptions_ActionPerformed(event);
		}
	}

	void btnClose_ActionPerformed(java.awt.event.ActionEvent event)
	{
		if ((btnClose.getLabel()).compareTo("Close") == 0)
		    app.exit();


	}

	void btnTrace_ActionPerformed(java.awt.event.ActionEvent event)
	{
	    setGUIState(RUNNING);

			if (chkAutomation.getState())	{	
				app.executeAutoPing(autoOptions);
			}
			else 
			{
				if(rdoStandard.getState())
						app.executeTrace();
				else
						app.executeThoroughTrace();
			}

		setGUIState(INITIAL);
	}

	void btnAutoOptions_ActionPerformed(java.awt.event.ActionEvent event)
	{
		 if (ad == null) ad = new AutoDlg(this, "Automation Options", true);
		 ad.show();
		
			autoOptions = ad.getOptions();



	}

	private void setGUIState(int state) {
	    switch(state) {
	        case INITIAL:
	            btnStart.setEnabled(true);
	            btnStart.setEnabled(true);
	            btnClose.setLabel("Cancel");
	            chkAutomation.setEnabled(true);
	            btnAutoOptions.setEnabled(true);
	            rdoStandard.setEnabled(true);
	            rdoThorough.setEnabled(true);
	            cmbHost.setEnabled(true);
	            btnClose.setLabel("Close");
	            break;
	        case RUNNING:
	            btnStart.setEnabled(false);
	            btnClose.setLabel("Cancel");
	            chkAutomation.setEnabled(false);
	            btnAutoOptions.setEnabled(false);
	            rdoStandard.setEnabled(false);
	            rdoThorough.setEnabled(false);
	            cmbHost.setEnabled(false);
	    }
	}

private boolean debug = false;
private void debugPrint(String s) {
       if (debug) System.out.println(s);
}

}
