/*
	Copyright (c) 1997 by Columbia University. All rights reserved.
 */

import java.awt.*;
import tjt.util.*;
import java.io.*;
import java.net.*;
import symantec.itools.awt.BorderPanel;

/** 
 * Dialog window used to display and to update options associated with the 
 * continuous or automated ping functionality. 
 * @version 1.0 31 Aug 1998
 * @author Terrence Truta
 */
public class AutoDlg extends Dialog
{
	public AutoDlg(Frame parent, boolean modal)
	{
		super(parent, modal);

		// 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(459,442);
		setBackground(new Color(12632256));
		borderPanel2 = new symantec.itools.awt.BorderPanel();
		borderPanel2.setLayout(null);
		borderPanel2.setBounds(24,168,420,220);
		add(borderPanel2);
		label5 = new java.awt.Label("Display a message if");
		label5.setBounds(10,4,300,29);
		borderPanel2.add(label5);
		chkUnreachable = new java.awt.Checkbox("Host becomes unreachable");
		chkUnreachable.setBounds(10,32,232,25);
		borderPanel2.add(chkUnreachable);
		chkUnreachable.setEnabled(false);
		chkDelayExceed = new java.awt.Checkbox("Delay exceeds value:");
		chkDelayExceed.setBounds(10,66,160,25);
		borderPanel2.add(chkDelayExceed);
		chkDelayExceed.setEnabled(false);
		txtDelayValue = new java.awt.TextField();
		txtDelayValue.setBounds(178,60,64,32);
		borderPanel2.add(txtDelayValue);
		txtDelayValue.setEnabled(false);
		label6 = new java.awt.Label("ms");
		label6.setBounds(262,64,36,29);
		borderPanel2.add(label6);
		chkLossExceed = new java.awt.Checkbox("Loss exceeds value:");
		chkLossExceed.setBounds(10,100,160,25);
		borderPanel2.add(chkLossExceed);
		chkLossExceed.setEnabled(false);
		txtLossValue = new java.awt.TextField();
		txtLossValue.setBounds(178,100,64,32);
		borderPanel2.add(txtLossValue);
		txtLossValue.setEnabled(false);
		chkSendMail = new java.awt.Checkbox("Send email (Unix only) to:");
		chkSendMail.setBounds(10,156,192,25);
		borderPanel2.add(chkSendMail);
		chkSendMail.setEnabled(false);
		txtEmail = new java.awt.TextField();
		txtEmail.setBounds(210,152,172,32);
		borderPanel2.add(txtEmail);
		txtEmail.setEnabled(false);
		chkAlerts = new java.awt.Checkbox("Alerts");
		chkAlerts.setBounds(32,144,169,23);
		add(chkAlerts);
		chkDisplayData = new java.awt.Checkbox("Display summary data when finished");
		chkDisplayData.setBounds(32,108,256,23);
		add(chkDisplayData);
		chkDisplayData.setState(true);
		label1 = new java.awt.Label("Monitor for ");
		label1.setBounds(32,28,92,25);
		add(label1);
		label2 = new java.awt.Label("hours");
		label2.setBounds(216,28,52,25);
		add(label2);
		label3 = new java.awt.Label("Run tests every");
		label3.setBounds(32,68,116,25);
		add(label3);
		label4 = new java.awt.Label("minutes");
		label4.setBounds(240,68,52,25);
		add(label4);
		btnOK = new java.awt.Button();
		btnOK.setLabel("OK");
		btnOK.setBounds(236,400,88,30);
		btnOK.setBackground(new Color(12632256));
		add(btnOK);
		btnCancel = new java.awt.Button();
		btnCancel.setLabel("Cancel");
		btnCancel.setBounds(336,400,88,30);
		btnCancel.setBackground(new Color(12632256));
		add(btnCancel);
		txtHours = new java.awt.TextField();
		txtHours.setText("24");
		txtHours.setBounds(148,24,58,31);
		add(txtHours);
		txtMinutes = new java.awt.TextField();
		txtMinutes.setText("30");
		txtMinutes.setBounds(172,64,58,31);
		add(txtMinutes);
		setTitle("Automation");
		//}}

		//{{REGISTER_LISTENERS
		SymWindow aSymWindow = new SymWindow();
		this.addWindowListener(aSymWindow);
		SymAction lSymAction = new SymAction();
		btnCancel.addActionListener(lSymAction);
		btnOK.addActionListener(lSymAction);
		SymItem lSymItem = new SymItem();
		chkAlerts.addItemListener(lSymItem);
		//}}
		
		gui = parent;
	}
	
	/** Returns the editted options encapsulated in a AutoOptions Object.
	  */
	public AutoOptions getOptions() {
	  return ao;   
	}    
		
		
	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;
	}

    // Used for addNotify check.
	boolean fComponentsAdjusted = false;
    
    public void displayErrMsg(String msg) {
		MsgDialog ed = new MsgDialog(gui, "Error", msg, 
												"", true);
			ed.show();
			return;
	}

	public AutoDlg(Frame parent, String title, boolean modal)
	{
		this(parent, modal);
		setTitle(title);
	}

    /**
     * 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)
		{
			Rectangle bounds = getParent().getBounds();
			Rectangle abounds = getBounds();
	
			setLocation(bounds.x + (bounds.width - abounds.width)/ 2,
				 bounds.y + (bounds.height - abounds.height)/2);
		}
		super.setVisible(b);
	}

	//{{DECLARE_CONTROLS
	symantec.itools.awt.BorderPanel borderPanel2;
	java.awt.Label label5;
	java.awt.Checkbox chkUnreachable;
	java.awt.Checkbox chkDelayExceed;
	java.awt.TextField txtDelayValue;
	java.awt.Label label6;
	java.awt.Checkbox chkLossExceed;
	java.awt.TextField txtLossValue;
	java.awt.Checkbox chkSendMail;
	java.awt.TextField txtEmail;
	java.awt.Checkbox chkAlerts;
	java.awt.Checkbox chkDisplayData;
	java.awt.Label label1;
	java.awt.Label label2;
	java.awt.Label label3;
	java.awt.Label label4;
	java.awt.Button btnOK;
	java.awt.Button btnCancel;
	java.awt.TextField txtHours;
	java.awt.TextField txtMinutes;
	//}}
	
	Frame gui;
    AutoOptions ao; //utility class to store user choices
                    //prevents us from having 13 'get' access functions
	class SymWindow extends java.awt.event.WindowAdapter
	{
		public void windowClosing(java.awt.event.WindowEvent event)
		{
			Object object = event.getSource();
			if (object == AutoDlg.this)
				Dialog1_WindowClosing(event);
		}
	}
	
	void Dialog1_WindowClosing(java.awt.event.WindowEvent event)
	{
		setVisible(false);
	}


	
	class SymAction implements java.awt.event.ActionListener
	{
		public void actionPerformed(java.awt.event.ActionEvent event)
		{
			Object object = event.getSource();
			if (object == btnCancel)
				btnCancel_ActionPerformed(event);
			else if (object == btnOK)
				btnOK_ActionPerformed(event);
		}
	}

	void btnCancel_ActionPerformed(java.awt.event.ActionEvent event)
	{
		// to do: code goes here.
			 
		//{{CONNECTION
		// Hide the Dialog
		setVisible(false);
		//}}
	}
    
	void btnOK_ActionPerformed(java.awt.event.ActionEvent event)
	{
        //create a new AutoOptions object to store user choices in
        //this utility class saves us from having 13 'get' access functions.
        ao = new AutoOptions();

		// validate the inputs
		try {
		    
		    ao.monitorHours = (Float.valueOf(txtHours.getText())).floatValue();
		    if (ao.monitorHours <= 0) throw new Exception();
		    ao.testMinutes = Integer.parseInt(txtMinutes.getText());
			if (ao.testMinutes <= 0) throw new Exception();
			if ((ao.alerts = chkAlerts.getState())) {
    			if ((ao.delay = chkDelayExceed.getState())) {
        			ao.delayValue = Integer.parseInt(txtDelayValue.getText());
        			if (ao.delayValue <= 0) throw new Exception();
        		}
        		if ((ao.loss = chkLossExceed.getState())) {
        			ao.lossValue = Integer.parseInt(txtLossValue.getText());
        			if (ao.lossValue <= 0) throw new Exception();
    			}
    		}
			
		} catch (Exception e) {
		    displayErrMsg("Number values need to be positive");
		    return;
		}
		
		if (chkAlerts.getState()) {
		     //I couldn't get the sound to work right so
		     //it will be included in the next version.
		     ao.playSound = false;
		    /*if ((ao.playSound = chkPlaySound.getState())) {
						try {
        		  URL url = new URL(txtSoundFile.getText());
						} catch (MalformedURLException e) {  
        		    displayErrMsg("Sound file not found.");
        		    return;
        	   }
        	    ao.soundFile = txtSoundFile.getText();
    	    }*/
    	    
    	    if ((ao.sendEmail = chkSendMail.getState())) {
        	    if ((txtEmail.getText()).indexOf("@") == -1) {
        	        displayErrMsg("Email address isn't valid");
        	        return;
        	    }
        	    ao.emailAddress = txtEmail.getText();
        	}
    	}
    	
    	ao.displaySummary = chkDisplayData.getState();
        ao.unreachable = chkUnreachable.getState();
        ao.displayMsg = true; //chkMsgBox.getState();
		

		//{{CONNECTION
		// Hide the Dialog
		setVisible(false);
		//}}
	}

	class SymItem implements java.awt.event.ItemListener
	{
		public void itemStateChanged(java.awt.event.ItemEvent event)
		{
			Object object = event.getSource();
			if (object == chkAlerts)
				chkAlerts_ItemStateChanged(event);
		}
	}

	void chkAlerts_ItemStateChanged(java.awt.event.ItemEvent event)
	{
		// to do: code goes here.
			 
		//{{CONNECTION
		// Enable the Checkbox on condition... Is Checkbox On?
		chkLossExceed.setEnabled(chkAlerts.getState());
		chkUnreachable.setEnabled(chkAlerts.getState());
		chkDelayExceed.setEnabled(chkAlerts.getState());
		txtDelayValue.setEnabled(chkAlerts.getState());
		txtLossValue.setEnabled(chkAlerts.getState());
		//chkMsgBox.setEnabled(chkAlerts.getState());
		//chkPlaySound.setEnabled(chkAlerts.getState());
		chkSendMail.setEnabled(chkAlerts.getState());
		//txtSoundFile.setEnabled(chkAlerts.getState());
		txtEmail.setEnabled(chkAlerts.getState());
		
		
		//}}
		//enable email things only if we are in Unix
		
		if (System.getProperty("os.name").indexOf("Windows") != -1) {
		    txtEmail.setEnabled(false);
		    chkSendMail.setEnabled(false);
		}
	}


}
