/*
 * Translator applet
 * $Id: Translator.java,v 1.2 1996/06/19 23:09:21 las Exp las $
 *
 * Developed for "Rethinking CS101", a project of Lynn Andrea Stein's AP Group.
 * For more information, see <a href="http://www.ai.mit.edu/projects/cs101">the
 * CS101 homepage</a> or email <las@ai.mit.edu>.
 *
 * Copyright (C) 1996 Massachusetts Institute of Technology.
 * Please do not redistribute without obtaining permission.
 */

import cs101.util.*;
import java.awt.*;
import java.util.StringTokenizer;

/**
 * Translator is a GUI applet which creates an input TextField and an
 * output TextArea.  The intent is for the words from the TextField to
 * be read one by one (using getNextWord()), possibly transformed
 * (e.g., into pig latin or ubby dubby) and printed back to the
 * TextArea (using printWord()).<p>
 *
 * In the current version, the Translator is hard-wired to create a
 * new PigLatin object to perform the String transformation.  The
 * PigLatin object need not actually use Pig Latin, but is in any case 
 * expected to contain the appropriate while (true) {echo();} loop.
 * PigLatin is not defined here.<p>
 *
 * Note that the modularization of this applet is not what one might
 * expect from standard Java.  For more details, see the
 *      <a href="http://www.ai.mit.edu/projects/cs101/">
 *                    Rethinking CS101
 * </a> project of Lynn Andrea Stein's AP Group at the MIT
 * Artificial Intelligence Laboratory.<p>
 *
 * @see      PigLatin
 * @see      cs101.util.BS
 * @see      cs101.util.StringUtils
 *
 * @author   Emil Sit, sit@mit.edu
 * @author   Lynn Andrea Stein, las@ai.mit.edu
 * @author   Adrian Banard, inki@mit.edu
 * @version  $Id: Translator.java,v 1.2 1996/06/19 23:09:21 las Exp las $
 *
 * Copyright 1996 Massachusetts Institute of Technology
 *
 */
public class Translator extends java.applet.Applet {
    private TextField inText;
    private TextArea  outText;

    private String nextWord = null;
    private BS wordRead = new BS(true);
    private BS wordWrite = new BS(false);

    private PigLatin wordHandler;


   /**
    * void init()
    *
    * Initializes the Translator applet:<br>
    * (a) creates and sets up the text input field and output area<br>
    * (b) creates the PigLatin object to transform individual words.<p>
    *
    * This method is called automatically by the Java Runtime.  It
    * should not be called directly by the user.<p>
    */
    public void init() {
        this.inText  = new TextField( 50 );
        
        this.outText = new TextArea( 20, 50 );
        this.outText.setEditable( false );

        this.add( inText );
        this.add( outText );

        this.validate();

        this.wordHandler = new PigLatin(this);
    }

   /**
    * action( Event, Object ) 
    *
    * Uses the Applet's event loop to feed the words -- typed by the
    * user to the GUI -- one at a time, for use by getNextWord(). <p>
    *
    * Called automatically by the Java Runtime.  (Should not be called
    * by the user.) <p>   
    *
    * Relies heavily on cs101.util.BS (Binary Semaphores), 
    * java.util.StringTokenizer
    *
    * @see      cs101.util.BS (Binary Semaphores)
    * @see      java.util.StringTokenizer
    *
    * @param    evt       the action (event) which occurred.
    * @param    what      the GUI Object in which the event occurred.
    * @returns  true, indicating that the event has been handled.
    */
    public boolean action( Event evt, Object what ) {

        // Use fancier tokenization:  add newline to end,
        //                            2d arg is delimiter chars,
        //                            3d means return delimiters as tokens.
        StringTokenizer inTokens = 
                        new StringTokenizer( this.inText.getText() + "\n",
                                             StringUtils.nonWordChars ,
                                             true );

        // Walk down tokens, using semaphores to synchronize read/write. 
        while ( inTokens.hasMoreTokens() ) {
            this.wordWrite.request();                // When ok to write,
            this.nextWord = inTokens.nextToken(); // write,
            this.wordRead.release();              // then set ok to read.
        }

        // Highlight TextField for user's next input 
        this.inText.selectAll();

        // done handling event.
        return true;
    }

   /**
    * getNextWord()
    *
    * Gets the next word (from the String typed by the user to the
    * GUI's input field).
    *
    * @returns  the next word read from the GUI, as a String.
    */       
    public String getNextWord() {
        this.wordRead.request();             // When ok to read,
        String nw = this.nextWord;        // read,
        this.wordWrite.release();         // then set ok to write
        return nw;                        // (and return what was read).
    }

   /**
    * printWord( String )
    *
    * Prints the specified String to the GUI's output field.
    *
    * @param    word      a String to be printed.
    * @returns  nothing.
    */
    public void printWord( String word ) {
        this.outText.appendText( word );
    }

}



/* Comments:
 *     Some code ideas borrowed from Java language tutorial and other
 *      java references.
 *
 * History:
 *     6-18-96  "Final" version created by las@ai.mit.edu 
 *                  from PigLatin.java by sit@mit.edu
 *                       Translator.java,v 1.1 1996/06/08 19:30:11 sit Exp 
 *                       PigLatin.java,v 1.2 1996/06/08 19:26:16 sit Exp 
 *                  and IgpayAtinlay.java by inki@mit.edu
 *     6-19-96  Documentation, modularity improved by las@ai.mit.edu
 *     $Log: Translator.java,v $
 *     Revision 1.2  1996/06/19 23:09:21  las
 *     Cleaned up documentation.
 *
 *     Revision 1.1  1996/06/19 21:46:19  las
 *     Initial revision
 *
 */
