// NAME.java -- Richard W. DeVaul -- DATE
//
// Solution to Problem PROB_N of Problem Set SET_N of MAS962, Fall 1997

/* $Id$ */

/*
$Log$
*/

import java.applet.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.net.*;


public class Affine extends Applet {

				// ***************
				// ** Constants **
				// ***************

				// *****************
				// ** Member data **
				// *****************
protected GridArea gridArea;
protected Choice choice;
protected Button button;
protected Panel panel0;
protected Panel panel1;

protected Lexer expand;
protected Lexer revert;

				// ********************
				// ** Member methods **
				// ********************

				// Init is called by the superclass to
				// set up the applet.
  public void init() {

    layoutApplet();

    this.add("North",panel0);
    this.add("Center",panel1);
    this.show();

    initializeLexers();
  }

  protected void initializeLexers() {
    String expandTokens[][] = {
      { "a",  "b",  "c",  "d",  "e",  "f",
	"3",   "3.1", "3.14",         "3.1415926505"},

      { "Ab", "Bc", "Cd", "De", "Ef", "FooBArBElCh",
	"3.1", "3.14", "3.1415926505", "3" } };

    String deliminators = " \n";

    String revertTokens[][] = new String[2][];
    revertTokens[0] = expandTokens[1];
    revertTokens[1] = expandTokens[0];

    expand = new Lexer(expandTokens,deliminators);
    revert = new Lexer(revertTokens,deliminators);
  }
				// Action is an event-handler called
				// by the super-class.

  public boolean action(Event event, Object arg) {
    if (event.target == button) {
      handleChoice();
      return true;
    }
    return super.action(event,arg);
  }

				// layoutApplet is called to lay out
				// the various components of the user
				// interface.  It is broken out as a
				// seperate method so that it can be
				// overridden in derived classes.
  protected void layoutApplet() {
    this.setLayout(new BorderLayout(5,10));

    gridArea = new GridArea(this,12,40);

    gridArea.setText("This is a test\nThis is only a test\n\n1\t2\t3\t4\n\n"
		     +"\t\t\tThis is a test of the\n\n\tUnix broadcasting system.");

    panel0 = new Panel();
    panel0.setLayout(new BorderLayout(0,0));
    panel0.add("Center",gridArea);

    button = new Button("Apply");

    choice = new Choice();
    choice.addItem("expand");
    choice.addItem("revert");
    
    panel1 = new Panel();
    panel1.setLayout(new FlowLayout(FlowLayout.CENTER,30,0));
    panel1.add(choice);
    panel1.add(button);
  }
    
				// handleChoice is the routine which
				// interprets the status of the choice
				// object to take some action when the
				// button is pressed.

  protected void handleChoice() {

    if (choice.getSelectedItem() == "expand") {
      gridArea.setText(expand.process(gridArea.getText()),4);
    }
    else {
      if (choice.getSelectedItem() == "revert") {
	gridArea.setText(revert.process(gridArea.getText()),4);
      }
    }
  }    
  
}

