// TextCode.java, version 1.00, file created, June 8, 2007.
//
// Applet for converting ASCII text to space delimited decimal code.
//
// This file last updated June 8, 2007, by Rick Wagner.
// Copyright 2004-2007 by Rick Wagner, all rights reserved.
//
// Use of this source code is authorized for educational purposes only. No use without
// proper attribution to Rick Wagner (http://iris.usc.edu/home/iris/rwagner/
// e-mail: Richard.J.Wagner@gmail.com).
//
// The prefix naming convention used here is a modified Hungarian notation.
// "s" is string, "sf" is single precision floating point, "i" is integer, "b" is boolean,
// and "d" is dimension.

import java.applet.*;
import java.awt.*;

public class TextCode extends Applet
{
  // Applet instance variables:
  private final String sVerNum = "1.00";                    // Only constructors can run here ("" is a constructor).
  private final String sCompiledDate = "June 8, 2007";      // Compiled date.

  private Dimension dApplet = null;                         // The applet panel size (set in calling html).
  private Image imOffScreen = null;                         // Offscreen image for double buffering.
  private Graphics grOffScreen = null;                      // Offscreen graphics for double buffering.

  private Button btnComputeCode = null;

  private TextArea taInputText = null;
  //private TextArea taPlainText = null;
  private TextArea taCodeText = null;

  private Label lblOne = null;                              // First label.
  private Label lblTwo = null;                              // Second label.
  //private Label lblThree = null;                            // Third label.
  private Label lblMessage = null;                          // Message label.

  // To allow browsers to get information about the applet (not yet implemented in Netscape nor in MSIE):
  public String getAppletInfo()
  {
    return "Text to code conversion applet, version " + sVerNum +
           ", by Rick Wagner, copyright 2007,\nall rights reserved.\n\n" +
           "Compiled " + sCompiledDate + ". Source code use authorized for\n" +
           "educational purposes only. No use without attribution.\n";
  }

  // Initialize the applet
  public void init()
  {
    int i = 0;
    GridBagLayout gbl = null;                               // GridBagLayout is used for the applet GUI layout.
    GridBagConstraints gbc = null;

    this.setBackground(Color.lightGray);
    dApplet = this.size();

    gbl = new GridBagLayout();
    this.setLayout(gbl);

    gbc = new GridBagConstraints();
    gbc.insets = new Insets(5, 10, 5, 10);                  // top, left, bottom, right.
    gbc.weightx = 0.0;
    gbc.weighty = 0.0;
    gbc.gridx = 0;


    // First Label
    lblOne = new Label("Enter Text for Conversion", Label.CENTER);
    gbc.gridy = 0;
    gbl.setConstraints(lblOne, gbc);
    this.add(lblOne);

    // Add the ASCII text box:
    taInputText = new TextArea("", 6, 80);                  // 6 rows and 80 columns.
    taInputText.setEditable(true);
    gbc.gridy = 1;
    gbl.setConstraints(taInputText, gbc);
    this.add(taInputText);

    // Message Label
    lblMessage = new Label("            Welcome to the text to code conversion applet." +
                           "Type or paste text into the upper text box.            ", Label.CENTER);
    gbc.gridy = 2;
    gbl.setConstraints(lblMessage, gbc);
    this.add(lblMessage);

    // Add the Compute Key button:
    btnComputeCode = new Button("Compute Code");               // The compute button.
    btnComputeCode.setForeground(Color.black);
    btnComputeCode.setBackground(Color.lightGray);
    gbc.gridy = 3;
    gbl.setConstraints(btnComputeCode, gbc);
    this.add(btnComputeCode);

    // Second Label
    lblTwo = new Label("Space Delimited ASCII Code", Label.CENTER);
    gbc.gridy = 4;
    gbl.setConstraints(lblTwo, gbc);
    this.add(lblTwo);

    // Add the key text box:
    taCodeText = new TextArea("", 6, 80);                     // 6 rows and 80 columns.
    taCodeText.setEditable(true);
    gbc.gridy = 5;
    gbl.setConstraints(taCodeText, gbc);
    this.add(taCodeText);

  } // End of init()

  // Execute this code after initialization
  public void start()
  {
    System.out.println("\n" + this.getAppletInfo());         // Identify self to the Java console-aware user
    taInputText.requestFocus();
  }                                                          // End of start()

  // Implements double buffering 
  public void update(Graphics g)
  {
    if (imOffScreen == null)
    {
      // Make sure the offscreen and graphics exist:
      imOffScreen = this.createImage(dApplet.width, dApplet.height);
      grOffScreen = imOffScreen.getGraphics();
      grOffScreen.clearRect(0, 0, dApplet.width, dApplet.height);
    }
    this.paint(grOffScreen);
    g.drawImage(imOffScreen, 0, 0, null);
  }

  // The applet frame painting function
  public void paint(Graphics g)
  {
    // Code for displaying images or drawing in the applet frame (called by the OS).
    g.clearRect(0, 0, dApplet.width, dApplet.height);        // Needed for double buffering.
    this.setBackground(Color.lightGray);                     // Ditto.
  
    drawFrame(g);                                            // Draw the frame abound the applet.
  }                                                          // End of paint()

  private void drawFrame(Graphics g)
  {
    // Draw a recessed frame around the applet border. Designed for gray-on-gray browser background.
    g.setColor(Color.black);
    g.drawLine(0, 0, dApplet.width - 1, 0);
    g.drawLine(0, 0, 0, dApplet.height - 1);
    g.setColor(Color.white);
    g.drawLine(0, dApplet.height - 1, dApplet.width - 1, dApplet.height - 1);
    g.drawLine(dApplet.width - 1, 1, dApplet.width - 1, dApplet.height - 1);
  }

  public boolean action(Event e, Object o)
  {
    if (e.target == btnComputeCode)
    {
      btnComputeCode.disable();
      if (taInputText.getText().length() == 0)
      {
        lblMessage.setText("Input text is empty.");
        taCodeText.setText("");
        taInputText.requestFocus();
      }
      else
      {
        // We can do the code computation.
        lblMessage.setText("Input text is not empty.");
        computeCode();
      }
      btnComputeCode.enable();
    }
    return true;                                              // Absorb the event;
  }                                                           // End of action().

  private void computeCode()
  {
    int i = 0;
    int iInputLength = 0;
    int iTemp = 0;
    String sInputText = null;
    String sCodeText = null;
    StringBuffer sbTemp = null;

    sInputText = taInputText.getText();
    iInputLength = sInputText.length();

    // Begin the code computation.
    sbTemp = new StringBuffer();
    lblMessage.setText("Writing input text as space delimited ASCII in decimal format.");

    for (i = 0; i < iInputLength; i++)
    {
      iTemp = sInputText.charAt(i);

      if (i == iInputLength - 1)
      {
        sbTemp.append(iTemp);
      }
      else
      {
        sbTemp.append(iTemp + " ");
      }
    }
    taCodeText.setText(sbTemp.toString());
  }                                                                       // End of computeCode().

} // End of Applet class CryptKey
