/* fakeConsole.java   last mod 30 JAN 2012 by Ed James */

import java.awt.*;
import java.applet.*;           // to run this in Applet mode
import java.awt.event.*;        // for the window adapter
import javax.swing.JFrame;      // for JFrame, main GUI component
import javax.swing.JMenuBar;    // for the JMenuBar
import javax.swing.JMenu;       // for the JMenu
import javax.swing.JMenuItem;   // for the JMenuItem
import javax.swing.JTextArea;   // for the text display
import javax.swing.JScrollPane; // to add scrollability to the text display
import javax.swing.JTextField;  // for the text input field

// Helper/internal class for this system - highest level GUI component
class fakeConsoleFrame extends JFrame implements ActionListener {
   public static final long serialVersionUID = 496L; // Not currently used.

   JMenuBar  myJMenuBar;
   JMenu     myJMenuA;
   JMenuItem myJMenuAItem1;

   BorderLayout myBorderLayout;

   JTextArea myDisplayArea;
   int       displayCount     = 0; // need better way to clear screen. 

   JTextField myInputField;

   public fakeConsoleFrame () {
      super ();

      setTitle ("unk");
      setSize (500,300);   // first approximation for size

      setJMenuBar (buildJMenuBar());

      myDisplayArea = new JTextArea ("", 30, 100);
      myDisplayArea.setEditable (false);

      Font myFont = new Font ("Lucida Sans Typewriter", Font.PLAIN, 12);
      myDisplayArea.setFont (myFont);
      myDisplayArea.setBackground (Color.black);
      myDisplayArea.setForeground (Color.white);

      myInputField = new JTextField();
      myInputField.addActionListener(this);
      myInputField.setBackground (Color.white);
      myInputField.setForeground (Color.black);

      Container contentPane = getContentPane();
      contentPane.add (new JScrollPane(myDisplayArea), BorderLayout.CENTER);
      contentPane.add (myInputField, BorderLayout.SOUTH);

      pack ();

      setVisible (true);
      }

// These overrides are needed for the MouseListener implementation:

   JMenuBar buildJMenuBar () {
      JMenuBar  newMenuBar = new JMenuBar();
      JMenu     newMenu;
      JMenuItem newMenuItem;

      newMenuBar.add (newMenu = new JMenu ("Console"));

      newMenu.add (newMenuItem= new JMenuItem ("Test Text"));
      newMenuItem.addActionListener (this);

      newMenu.add (newMenuItem= new JMenuItem ("Clear"));
      newMenuItem.addActionListener (this);

      newMenuBar.add (newMenu = new JMenu ("Font"));

      newMenu.add (newMenuItem= new JMenuItem ("Null-1"));
      newMenuItem.addActionListener (this);

      newMenu.add (newMenuItem= new JMenuItem ("Null-2"));
      newMenuItem.addActionListener (this);

      return newMenuBar;
      }

// This override is needed for the ActionListener implementation:
   public void actionPerformed(ActionEvent AE) {
      System.out.println ("actionPerformed() started");

      String strInput;

      Object source = AE.getSource();
      if (source == myInputField) {
         strInput = myInputField.getText();
         System.out.println (strInput);
         displayString (strInput+"\n");
         myInputField.setText ("");
         return;
         }

   // NOTE: This cast might be dangerous if actions are added for
   // other than menu items:
      JMenuItem JMI = (JMenuItem)AE.getSource();

      System.out.println (JMI.getActionCommand());

      String AC = JMI.getActionCommand ();
      if      (AC == "Test Text"   ) doTestText ();
      else if (AC == "Clear"       ) doClearScreen();
      else
         displayString ("ERROR: unknown action command - " + AC + "\n");
      }

   public void displayString(String strParm) {
      myDisplayArea.append (strParm);
      displayCount++;

      if (myDisplayArea.getLineCount() > 300) {
         myDisplayArea.replaceRange ("", 0, strParm.length());
         displayCount--;
         }
      }

   void doTestText () {
      for (int i = 1 ; i <= 50 ; i++) {
         myDisplayArea.append ("Test Line Number " + i + "\n");
         }
      }

   void doClearScreen () {
      myDisplayArea.setText ("");
      displayCount = 0;
      }

   void addWindowListener () {
      try {
         addWindowListener (new WindowAdapter() {
            public void windowClosing (WindowEvent myEvent) {
               System.exit (0);
               }
            });
         }
      catch (java.security.AccessControlException ACE2) {
         System.out.println("Error 2: " + ACE2.getMessage());
         }
      }
   }


// Main class for this system:
public class fakeConsole extends Applet {
   public static final long serialVersionUID = 497L; // Not currently used.
   public static fakeConsoleFrame myFrame;

   public void init () {
      myFrame = new fakeConsoleFrame ();
      myFrame.addWindowListener ();
      }

   public static void main (String[] args) {
      myFrame = new fakeConsoleFrame ();
      myFrame.addWindowListener ();
      }
   }