import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;

/** DemoTranslateEnglishGUI - program
* to translate English into Pig Latin -- GUI version.<p>
*
* The user is prompted to type phrases
* into a <code>JTextArea</code> component.  Each time the ENTER key is
* pressed, the words on the current line are translated into Pig Latin, with
* the result appearing on the following line.<p>
*
* Since this is a GUI application, an event-driven programming
* strategy is used.
* The design includes the three basic steps necessary
* for event-driven programming.  First, the extended <code>JFrame</code>
* signature includes the clause <code>implements KeyListener</code>.
* Second, the
* method <code>addKeyListener</code> is invoked on the text area object.
* This registers the text area to listen for key events.  Third, an implementation
* is provided for each of the three
* <code>KeyListener</code> methods.<p>
*
* The work is performed in the <code>keyPressed</code> method, while
* dummy (i.e., empty) implementations are provided for
* <code>keyReleased</code> and <code>keyTyped</code>.<p>
*
* Screen snap...<br>
* <center><img src = "DemoTranslateEnglishGUI-1.gif"></center><p>
*
* @see <a href="DemoTranslateEnglishGUI.java">source code</a>
* @author Scott MacKenzie, 2003
*/
public class DemoTranslateEnglishGUI
{
   public static void main(String[] args)
   {
      DemoTranslateEnglishGUIFrame frame = new DemoTranslateEnglishGUIFrame();
      frame.setTitle("DemoTranslateEnglishGUI");
      frame.pack();
      frame.show();
   }
}

class DemoTranslateEnglishGUIFrame extends JFrame implements KeyListener
{
   final String BANNER = "Translate English to Pig Latin\n";
   final String PROMPT = "Enter some text:\n";

   private JTextArea enterArea;

   public DemoTranslateEnglishGUIFrame()
   {
      // ----------------------------------
      // construct and configure components
      // ----------------------------------

      enterArea = new JTextArea(10, 30);
      enterArea.setFont(new Font("serif", Font.PLAIN, 16));
      enterArea.setText(BANNER);
      enterArea.append(PROMPT);
      enterArea.setCaretPosition(enterArea.getText().length());
      enterArea.setEditable(true);
      enterArea.requestFocus();

      // -------------
      // add listeners
      // -------------

      this.addWindowListener(new WindowCloser());
      enterArea.addKeyListener(this);

      // ------------------
      // arrange components
      // ------------------

      // add component to panel
      
      JPanel textPanel = new JPanel();
      textPanel.add(enterArea);
      
      // add panel to content pane

      Container c = getContentPane();
      c.add(textPanel, "North");
   }

   // ---------------------------------
   // implement KeyListener methods (3)
   // ---------------------------------

   public void keyReleased(KeyEvent ke) {}
   public void keyTyped(KeyEvent ke) {}

   public void keyPressed(KeyEvent ke)
   {
      if (ke.getKeyCode() == KeyEvent.VK_ENTER)
      {
         String s = enterArea.getText();

         // find index of new phrase
         int idx = s.length() - 1;
         while (s.charAt(idx) != '\n')
            --idx;

         // get phrase
         s = s.substring(idx, s.length());

         // translate phrase
         s = translatePhrase(s);

         // output translated phrase
         enterArea.append("\n" + s);
      }
   }

   // -------------
   // other methods
   // --------------

   // translate English phrase to Pig Latin
   public static String translatePhrase(String s)
   {
      StringTokenizer st = new StringTokenizer(s);

      String translation = "";
      while (st.hasMoreTokens())
      {
         String word = st.nextToken();
         String pigLatinWord = translateWord(word);
         translation += pigLatinWord + " ";
      }
      return translation;
   }

   // translate English word to Pig Latin
   private static String translateWord(String s)
   {
      String s1 = s.length() > 1 ? s.substring(1, s.length()) : "";
      String s2 = s.length() > 0 ? s.substring(0, 1) : "";
      String s3 = "ay";
      return s1 + s2 + s3;
   }

   // -------------
   // inner classes
   // -------------

   // Note: WindowAdapter implements WindowListener

   private class WindowCloser extends WindowAdapter
   {
      public void windowClosing(WindowEvent event)
      {
         System.exit(0);
      }
   }
}

