//
// Caitlin Howell
// CS577, Homework 4
// LCS Dynamic Programming Algorithm Implemenation
// Due December 9, 1997
//
// File  LCString.java
//
// Class LCString (which is an applet)

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

// Main Class for applet LCString

public class LCString extends Applet
{


// The following labels and textfields display information on a web page
	private Label lbl1; 
	private Label lbl2;
	private Label lbl3;
	private TextField tf1, tf2, result;
	private TextArea resTA;

// We initialize the Maximum String Length to 100, but it will be set
// to whatever is passed in from the web page

	private int MaxStringLength = 100;

// LCSResult is an object that holds the LCS table and returns the
// results of the LCS computations

	private LCSTable LCSResult;

	public LCString()
	{
		setBackground(Color.white);
		lbl1 = new Label("String 1: ");
		lbl2 = new Label("String 2: ");
		lbl3 = new Label("The Longest Common String: ");
	}

	public String getAppletInfo()
	{
		return "Name: LCString\r\n" +
		       "Author: Caitlin Thomas Howell\r\n" +
		       "Created with Microsoft Visual J++ Version 1.1, as well as the Sun JDK";
	}

	public void init()
	{

// Set the maximum length of the strings we find the LCS of on the web page
		MaxStringLength = Integer.valueOf(getParameter("MAXLENGTH")).intValue();

// Allocate lots of web page space for the table!

		resize(600, 500);

// We need a fixed-width font to display the table on the web page

                Font TAfont = new Font("Courier",Font.PLAIN,10);
			
// Set up the graphical interface on the web page

		tf1 = new TextField(MaxStringLength);
		tf2 = new TextField(MaxStringLength);
		result = new TextField(MaxStringLength + 10);
		result.setForeground(Color.red);
		result.setEditable(false);
		resTA = new TextArea("", MaxStringLength/2,2*MaxStringLength);
		resTA.setEditable(false);
		resTA.setForeground(Color.blue);
		resTA.setFont(TAfont);
		add("West",lbl1);
		add("East",tf1);
		add("West",lbl2);
		add("East",tf2);
		add("West", lbl3);
		add("East",result);
		add("South", resTA);

// Force a page redraw (this method has been depecated but I like it.)

		show();
	}

// Every time two non-null strings are entered in the 
// and the user presses enter, this event handler is triggered
// the event handler calls the LCSTable class, which does the LCS 
// computations

	public boolean handleEvent(Event event)
	{
		if ((event.target == tf1) || (event.target == tf2))
		{
			if ((tf1.getText() != "") && (tf2.getText() != ""))
			{
				if (event.id == Event.ACTION_EVENT)
				{
					System.out.println(tf1.getText());
					System.out.println(tf2.getText());
		  			LCSResult = new LCSTable(tf1.getText(), tf2.getText());
					LCSResult.init();
					result.setText(LCSResult.lcs() + " (length " + LCSResult.lcsLength() + ")");
					resTA.setText(LCSResult.printTbl());
					show();
					return true;
				}
			}
		}
		return false;
	}
	
	public void destroy()
	{
	}

	public void paint(Graphics g)
	{
	}

	public void start()
	{
	}
	
	public void stop()
	{
	}


}
