// CommandShell.java
//
// This program is created by modifying an example from the book 
// _Java in a Nutshell_ by David Flanagan. You may study, use, modify, 
// and distribute this example for any purpose. This program is 
// provided WITHOUT WARRANTY either expressed or implied.
//
// Copyright Henrik Björkman 1999
//
// This applet will connect to a port on a server and open
// a window with one field for input from keyboard and one
// field for output received from the server.
//
// History:
// Created by Henrik Björkman 1999-09-08

import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;


class CommandShell extends Frame
{
  public static final int DEFAULT_PORT = 5000;

  DataInputStream in;
  PrintStream out;
  TextField inputfield;
  TextArea outputarea;

  static int exit_count=1; // Do system exit if this reaches zero.
  int event_count=0; // Just for debugging


  // Just for debugging.
  static void debug(String s)
  {
    System.out.println("CommandShell: " + s);   
  }


  public CommandShell()
  {
    //try 
    {
            

      inputfield = new TextField();
      outputarea = new TextArea();
      outputarea.setEditable(false);
      this.setLayout(new BorderLayout());
      this.add("South", inputfield);
      this.add("Center", outputarea);


      this.pack();
      this.show();

    }      
    /*catch (IOException e) 
    {
      System.err.println(e); 
    }
    finally 
    {
      debug("finally");
    }*/

  }



  // This will handle all events.
  public boolean handleEvent(Event e) 
  {
    //debug("handleEvent "+(event_count++));

    switch(e.id)
    {
      case Event.ACTION_EVENT:
      {
        if (e.target == inputfield) 
        {
          debug("inputfield");
          String str=inputfield.getText();
          debug("str "+str);
          //out.println(str);
          new CommandShellChild(str,outputarea);
          inputfield.setText("");
          return true;
        }
        break;
      }
      case Event.WINDOW_DESTROY:
      {
        debug("WINDOW_DESTROY");
        this.hide();
        this.dispose();
        if (--exit_count==0) System.exit(0);
        return true;
      }
      default:
      {
        //debug("Other event");
        break;
      }
    }
    return false;
  }

  public void term()
  {
    debug("term");

    /*listener.stop();*/
    inputfield.hide();
    outputarea.hide();
  }



  public void finalize()
  {
    debug("finalize");
  }

  public static void usage() 
  {
    System.out.println("Usage: java CommandShell");
    System.exit(0);
  }    


  public static void main(String[] args) 
  {        
    new CommandShell();
  }

} 




