//---------- Program GreetingApplet (Step 2 Final Version) -----------//

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

/**
 * Introduction to OOP with Java 2nd Edition, McGraw-Hill
 *   
 *  Chapter 5
 *
 * <p>
 * An applet that accepts the user's name and
 * displays a personalized greeting.
 *
 * This applet accepts the user’s name via a TextField object. 
 * When the user presses the ENTER key, the applet displays 
 * a personalized greeting "Nice to meet you, <user name>." 
 * with <user name> replaced by the actual name entered by the user.
 *
 * @author Dr. Caffeine
 *
 */
public class GreetingApplet extends Applet implements ActionListener
{

//----------------------------------
//    Data Members
//----------------------------------

    /**
     * The Label to prompt the user for input
     */
    private Label      prompt;
    
    /**
     * The Lable to dislay the personalized greeting
     */
    private Label      greeting;
    
    /**
     * The TextField to accept the user input
     */
    private TextField  inputLine;

    
//----------------------------------
//    Constructors
//----------------------------------
                                
    /**
     * Default constructor that creates one TextField and
     * two Label objects.
     */
    public GreetingApplet( )
    {
        //Create GUI objects
        prompt    = new Label("Please enter your name:");
        greeting  = new Label( );
        inputLine = new TextField( 15 );
      
        //Now attach them to this applet, so they become
        //visible on the applet
        add( prompt    );
        add( inputLine );
        add( greeting  );  
        
        //Add this applet as an ActionListener
        inputLine.addActionListener( this );
    }

//-----------------------------------------------------
//     Public Methods:
// 
//         void   actionPerformed   (  ActionEvent   )
//
//-----------------------------------------------------
    /**
     * Implements the abstract method defined in the interface 
     * ActionListener. The method retrieves the text from the
     * TextField object ‘inputLine’ and displays the personalized
     * greeting using the Label object ‘greeting’.
     *
     * @param event the ActionEvent object
     *
     */
    public void actionPerformed(ActionEvent event)
    {
        greeting.setText("Nice to meet you, " + 
                       inputLine.getText( ) + ".");
        add( greeting );
        doLayout( );
    }

}