import java.awt.*;
/* Chesapeake TTCP
 * Copyright 1997, Chesapeake Computer Consultants, Inc.
 */

// import java.awt.*;
/*
    This class is a basic extension of the Frame class.  It can be used
    by an applet or application.  To use it, create a reference to the class,
    then instantiate an object of the class, and call the show() method.


    example:

    ttcpControls thettcpControls;
    thettcpControls = new ttcpControls();
    thettcpControls.show();

    You can add controls or menus to ttcpControls with Cafe Studio.
 */


import java.awt.Frame;
import java.awt.Panel;
import java.awt.CheckboxGroup;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.List;
import java.awt.Label;
import java.awt.Event;
import java.awt.Color;
import java.awt.TextField;
import java.awt.TextArea;
import java.awt.Font;
/* */
import java.net.InetAddress;
import java.net.UnknownHostException;

public class ttcpControls extends Frame {

    // Variables
    TransThread Trans;
    ParamsClass Params;
    int tmpNumBuf = 0, tmpBufSize = 0, tmpPort = 0;
    String tmpHost = "";

    // Constructor (Builds window)
    public ttcpControls(ParamsClass p) {

        super("Chesapeake TTCP v1.0");
        setBackground(Color.lightGray);
        Params = p;
        //{{INIT_CONTROLS
        setLayout(null);
        resize(511,361);
        panel1=new Panel();
        panel1.setLayout(null);
        add(panel1);
        panel1.reshape(14,46,483,58);
        groupTransRec= new CheckboxGroup();
        ButtonStart=new Button("Start");
        panel1.add(ButtonStart);
        ButtonStart.reshape(21,19,91,26);
        ButtonStop=new Button("Stop");
        panel1.add(ButtonStop);
        ButtonStop.reshape(147,19,91,26);
        ButtonQuit=new Button("Quit");
        panel1.add(ButtonQuit);
        ButtonQuit.reshape(273,19,91,26);
        label1=new Label("Results:");
        add(label1);
        label1.reshape(28,208,70,13);
        CheckTrans=new Checkbox("Transmit",groupTransRec, false);
        add(CheckTrans);
        CheckTrans.reshape(154,111,105,19);
        CheckRec=new Checkbox("Receive",groupTransRec, true);
        add(CheckRec);
        CheckRec.reshape(266,111,105,19);
        LabelHost=new Label("Destination:");
        add(LabelHost);
        LabelHost.reshape(21,143,84,13);
        EditHost=new TextField(13);
        EditHost.disable();
        add(EditHost);
        EditHost.reshape(126,143,112,20);
        LabelBufSize=new Label("Buffersize:");
        add(LabelBufSize);
        LabelBufSize.reshape(266,143,84,13);
        EditBufSize=new TextField(13);
        add(EditBufSize);
        EditBufSize.reshape(371,143,112,20);
        LabelNumBuf=new Label("Num Buffers:");
        add(LabelNumBuf);
        LabelNumBuf.reshape(21,176,84,13);
        EditNumBuf=new TextField(13);
        add(EditNumBuf);
        EditNumBuf.reshape(126,176,112,19);
        LabelPort=new Label("Port:");
        add(LabelPort);
        LabelPort.reshape(266,176,84,13);
        EditPort=new TextField(13);
        add(EditPort);
        EditPort.reshape(371,176,112,19);
        Status=new TextArea(6,57);
        add(Status);
        Status.reshape(21,228,476,104);
        Title=new Label("Chesapeake TTCP v1.0");
        Title.setFont(new Font("Dialog",Font.BOLD|Font.ITALIC,16));
        add(Title);
        Title.reshape(14,26,168,20);
        LabelCopyright=new Label("Copyright 1997, Chesapeake Computer Consultants, Inc. (www.ccci.com)");
        LabelCopyright.setFont(new Font("Dialog",Font.PLAIN,10));
        add(LabelCopyright);
        LabelCopyright.reshape(21,338,371,20);
        LabelKnowledge=new Label("Where Knowledge Works!");
        LabelKnowledge.setFont(new Font("Dialog",Font.BOLD,12));
        add(LabelKnowledge);
        LabelKnowledge.reshape(336,26,161,20);
        //}}

        //{{INIT_MENUS
        //}}

        // Initialize text fields from info in Trans
        tmpHost = Params.getHost();
        tmpBufSize = Params.getBufSize();
        tmpNumBuf = Params.getNumBuf();
        tmpPort = Params.getPort();

        EditHost.setText(tmpHost);
        EditBufSize.setText(Integer.toString(tmpBufSize));
        EditNumBuf.setText(Integer.toString(tmpNumBuf));
        EditPort.setText(Integer.toString(tmpPort));
    }

    public synchronized void show() {
    	move(50, 50);
    	super.show();
    }

    public boolean handleEvent(Event event) {
    	if (event.id == Event.ACTION_EVENT && event.target == CheckRec) {
    	    	ioReceive();
    	    	return true;
    	}
    	else
    	if (event.id == Event.ACTION_EVENT && event.target == CheckTrans) {
    	    	ioTransmit();
    	    	return true;
    	}
    	else
/*    	if (event.id == Event.ACTION_EVENT && event.target == CheckUDP) {
    	    	protocolUDP();
    	    	return true;
    	}
    	else
    	if (event.id == Event.ACTION_EVENT && event.target == CheckTCP) {
    	    	protocolTCP();
    	    	return true;
    	}
    	else  /* */
    	if (event.id == Event.ACTION_EVENT && event.target == ButtonStart) {
    	    	clickedButtonStart();
    	    	return true;
    	}
    	else
    	if (event.id == Event.ACTION_EVENT && event.target == ButtonStop) {
    	    	clickedButtonStop();
    	    	return true;
    	}
    	else
    	if (event.id == Event.ACTION_EVENT && event.target == ButtonQuit) {
    	    	clickedButtonQuit();
    	    	return true;
    	}
    	else
    	if (event.id == Event.WINDOW_DESTROY) {
//    	    hide();
            System.exit(0);
    	    return true;
    	}
    	return super.handleEvent(event);
    }

    //{{DECLARE_MENUS
    //}}

    //{{DECLARE_CONTROLS
    Panel panel1;
    CheckboxGroup groupTransRec;
    Button ButtonStart;
    Button ButtonStop;
    Button ButtonQuit;
    Label label1;
    Checkbox CheckTrans;
    Checkbox CheckRec;
    Label LabelHost;
    TextField EditHost;
    Label LabelBufSize;
    TextField EditBufSize;
    Label LabelNumBuf;
    TextField EditNumBuf;
    Label LabelPort;
    TextField EditPort;
    TextArea Status;
    Label Title;
    Label LabelCopyright;
    Label LabelKnowledge;
    //}}

// Button functions
    public void clickedButtonStart() {
        InetAddress tmpIPAddr;
        boolean error = false;

//        System.err.println("Start");
        // Tell TransThread to go.
        Status.setText("");
        try {
            tmpNumBuf = Integer.parseInt(EditNumBuf.getText());
        } catch (NumberFormatException e) {
             Status.appendText("% NumBuffers contains invalid input\n");
             error = true;
        }
        try {
            tmpBufSize = Integer.parseInt(EditBufSize.getText());
        } catch (NumberFormatException e) {
             Status.appendText("% Buffer Size contains invalid input\n");
             error = true;
        }
        try {
            tmpPort = Integer.parseInt(EditPort.getText());
        } catch (NumberFormatException e) {
             Status.appendText("% Port contains invalid input\n");
             error = true;
        }
        try {
            tmpHost = EditHost.getText();
            tmpIPAddr = InetAddress.getByName(tmpHost);
        } catch (UnknownHostException e) {
             Status.appendText("% Destination is not a valid host name or address\n");
             error = true;
        }

        if (error) {
            Status.appendText("% Invalid input detected, please correct and restart.\n");
        } else {
            if (Trans != null) {
                if (Trans.isAlive()) {
                    Trans.stop();
                }
            }
            // All input is valid, create a new thread and start.
            Params.setHost(tmpHost);
            Params.setPort(tmpPort);
            Params.setBufSize(tmpBufSize);
            Params.setNumBuf(tmpNumBuf);
            Trans = new TransThread(Params, this);
            Trans.start();
            ButtonState(Params.RUN);
        }
    }
    public void clickedButtonStop() {
        System.err.println("Stop");
        if (Trans != null) {
            if (Trans.isAlive()) {
                Trans.stop();
                Trans = null;
            }
        }
        ButtonState(Params.STOP);
        Status.appendText("Transfer interrupted.\n");
    }
    public void transferComplete() {
        ButtonState(Params.STOP);
        Status.appendText(Params.getStats());
    }
    public void clickedButtonQuit() {
        System.exit(0);
    }
    public void protocolTCP() {
        System.err.println("TCP");
        Params.setProtocol(Params.TCP);
    }
    public void protocolUDP() {
        System.err.println("UDP");
        Params.setProtocol(Params.UDP);
    }
    public void ioTransmit() {
        System.err.println("Transmit");
        Params.setTR(Params.TRANSMIT);
        enableTR(Params.TRANSMIT);
    }
    public void ioReceive() {
        System.err.println("Receive");
        Params.setTR(Params.RECEIVE);
        enableTR(Params.RECEIVE);
    }

    void enableTR(boolean tr) {
        // RECEIVE == true and TRANSMIT == false
        // Set the input fields so one or the other is enabled
        // at a time.
        LabelHost.enable(!tr);
        EditHost.enable(!tr);
    }

    void ButtonState(boolean s) {
        // boolean s: RUN == true and STOP == false
        // Quit button is always enabled.
        // The Stop button should be enabled while running.
        //
        // Buttons and check buttons.
        ButtonStart.enable(!s); // Start
        ButtonStop.enable(s);  // Stop
//        CheckTCP.enable(!s);  // TCP
//        CheckUDP.enable(!s);  // UDP
        // Edit boxes and labels
        LabelBufSize.enable(!s);
        LabelNumBuf.enable(!s);
        LabelPort.enable(!s);
        EditBufSize.enable(!s);
        EditNumBuf.enable(!s);
        EditPort.enable(!s);
        CheckRec.enable(!s);
        CheckTrans.enable(!s);
        // Host field is always disabled during the run, but
        // is only re-enabled if in TRANSMIT mode.
        if (s) { // RUN state
            LabelHost.disable();
            EditHost.disable();
        } else { // STOP state, check TRANSMIT mode
            LabelHost.enable(!Params.getTR());
            EditHost.enable(!Params.getTR());
        }
    }

}

