import java.awt.event.*;
import java.awt.*;
import java.applet.*;
import java.util.*;

public class Talk2 extends Applet implements ItemListener, ActionListener
    {
     private Button clear;
     private Button talks;

     public AudioClip sounds[] = 
                    { "\"dogs.au\"", "\"monkeys.au\"", "\"trees.au\"", "\"people.au\"", "\"guys.au\"",
                     "\"cars.au\"", "\"computers.au\"", "\"guts.au\"", "\"fires.au\"", "\"guns.au\"",
                     "\"chickens.au\"", "\"bombs.au\"", "\"Mahe.au\"", "\"Jeff.au\"", "\"JohnandTepring.au\"",
                     "\"SarahJane.au\"", "\"talk.au\"", "\"eat.au\"", "\"climb.au\"", "\"rip.au\"", "\"demoralize.au\"",
                     "\"masticate.au\"", "\"fornicate.au\"", "\"shoot.au\"", "\"draw.au\"",
                     "\"disillusion.au\"", "\"pump.au\"", "\"languor.au\"", 
                     "\"fat.au\"", "\"slinky.au\"", "\"slimy.au\"", "\"tall.au\"", "\"repulsive.au\"",
                     "\"stupid.au\"", "\"busty.au\"", "\"falliferous.au\"", "\"long.au\"", "\"outrageous.au\"",
                     "\"teeny.au\"", "\"easy.au\"", 
                     "\"the.au\"", "\"or.au\"", "\"and.au\"", "\"often.au\"", "\"soon.au\"", "\"quickly.au\"",
                     "\"now.au\"", "\"then.au\"", "\"while.au\"", "\"also.au\"", "\"datata.au\"", "\"with.au\"",
                     "\"falliferously.au\"" };  
                     
     public String soundString[]= 
                    { "\"dogs.au\"", "\"monkeys.au\"", "\"trees.au\"", "\"people.au\"", "\"guys.au\"",
                     "\"cars.au\"", "\"computers.au\"", "\"guts.au\"", "\"fires.au\"", "\"guns.au\"",
                     "\"chickens.au\"", "\"bombs.au\"", "\"Mahe.au\"", "\"Jeff.au\"", "\"JohnandTepring.au\"",
                     "\"SarahJane.au\"", "\"talk.au\"", "\"eat.au\"", "\"climb.au\"", "\"rip.au\"", "\"demoralize.au\"",
                     "\"masticate.au\"", "\"fornicate.au\"", "\"shoot.au\"", "\"draw.au\"",
                     "\"disillusion.au\"", "\"pump.au\"", "\"languor.au\"", 
                     "\"fat.au\"", "\"slinky.au\"", "\"slimy.au\"", "\"tall.au\"", "\"repulsive.au\"",
                     "\"stupid.au\"", "\"busty.au\"", "\"falliferous.au\"", "\"long.au\"", "\"outrageous.au\"",
                     "\"teeny.au\"", "\"easy.au\"", 
                     "\"the.au\"", "\"or.au\"", "\"and.au\"", "\"often.au\"", "\"soon.au\"", "\"quickly.au\"",
                     "\"now.au\"", "\"then.au\"", "\"while.au\"", "\"also.au\"", "\"datata.au\"", "\"with.au\"",
                     "\"falliferously.au\"" };  

     private List nounList;
     private List verbList;
     private List adjectiveList;
     private List otherwords;

   
     private String NOUNS[] =
                    {"dogs", "monkeys", "trees", "people", "guys",
                     "cars", "computers", "guts", "fires", "guns",
                     "chickens", "bombs", "Mahe", "Jeff", "JohnandTepring",
                     "SarahJane"  };

     private String VERBS[] =
                    {"talk", "eat", "climb", "rip", "demoralize",
                     "masticate", "fornicate", "shoot", "draw",
                     "disillusion", "pump", "languor" };

     private String ADJECTIVES[] =
                    {"fat", "slinky", "slimy", "tall", "repulsive",
                     "stupid", "busty", "falliferous", "long", "outrageous",
                     "teeny", "easy" };
     private String OTHERWORDS[] =
                    {"the", "or", "and", "often", "soon", "quickly",
                     "now", "then", "while", "also", "datata", "with",
                     "falliferously" };

     public String s;
     
     public TextArea sentence;

     public void init()
        {
         for(int m = 0; m < sounds.length; m++)
          {  sounds[m] = getAudioClip(getDocumentBase(), soundString[m]); }
         
         s = new String("");
         sentence = new TextArea( s, 5, 60, TextArea.SCROLLBARS_NONE );

         clear = new Button("CLEAR");
         clear.addActionListener( new ButtonHandler(this) );
         add( clear, BorderLayout.SOUTH);

         talks = new Button("TALK");
         talks.addActionListener( new ButtalkHandler(this) );
         
         nounList = new List(5, false);
         nounList.addActionListener( this );
         nounList.addItemListener( this );

         for (int i =0; i < NOUNS.length; i ++)
            nounList.add( NOUNS[i] );

         add( nounList );

         verbList = new List(5, false);
         verbList.addActionListener( this );
         verbList.addItemListener( this );

         for (int i =0; i < VERBS.length; i ++)
            verbList.add( VERBS[i] );

         add( verbList );

         adjectiveList = new List(5, false);
         adjectiveList.addActionListener( this );
         adjectiveList.addItemListener( this );

         for (int i =0; i < ADJECTIVES.length; i ++)
            adjectiveList.add( ADJECTIVES[i] );

         add( adjectiveList );

         otherwords = new List(5, false);
         otherwords.addActionListener( this );
         otherwords.addItemListener( this );

         for (int i =0; i < OTHERWORDS.length; i ++)
            otherwords.add( OTHERWORDS[i] );

         add( otherwords );
         add( talks, BorderLayout.SOUTH);
         add( sentence );


        }
      public void actionPerformed( ActionEvent e )
      {
          
        if(e.getSource() == nounList)
          {
           s = s.concat( " " + NOUNS[nounList.getSelectedIndex()] );
           sentence.setText( s);
          }
        else if(e.getSource() == verbList)
          {                      
           s = s.concat(" " + VERBS[verbList.getSelectedIndex()] );
           sentence.setText( s);
          }
        else if(e.getSource() == adjectiveList)
          {
           s = s.concat(" " + ADJECTIVES[adjectiveList.getSelectedIndex()] );
           sentence.setText( s);
          }
        else if(e.getSource() == otherwords)
          {
           s = s.concat( " " + OTHERWORDS[otherwords.getSelectedIndex()] );
           sentence.setText( s);
          }


      }
      public void itemStateChanged( ItemEvent e)
      {
       List list = (List)e.getItemSelectable();
       showStatus(list.getSelectedItem() );
      }
}

class ButtonHandler implements ActionListener
{
 Talk2 t;
 public ButtonHandler(Talk2 t)
    {
        this.t = t;
    }
 public void actionPerformed(ActionEvent e)
 {
    t.s = "";
    t.sentence.setText(t.s);
 }
}
class ButtalkHandler implements ActionListener
{
 Talk2 t;
 public ButtalkHandler( Talk2 t )
    {
        this.t = t;
    }
 public void actionPerformed( ActionEvent e)
 {
  t.showStatus( "Talk button pushed");

  String TALK = t.s;
  String temp;
    
  StringTokenizer tokens = new StringTokenizer( TALK );
  while(tokens.hasMoreTokens() )
        {
           temp = tokens.nextToken();
           temp = temp.concat(".au\"");
           temp = "\"" + temp;

           for(int n = 0; n < t.sounds.length; n++)
              {
                if( temp == t.soundString[n])
                  { t.sounds[n].play();}
              }
        }

 }
}