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

// This appears in Core Web Programming from
// Prentice Hall Publishers, and may be freely used
// or adapted. 1997 Marty Hall, hall@apl.jhu.edu.

/** A class to demonstrate list selection/deselection
 *  and action events in Java 1.1.
 */

public class ListEvents2 extends CloseableFrame {
  public static void main(String[] args) {
    new ListEvents2();
  }

  protected List languageList;
  private TextField selectionField, actionField;
  private String selection = "[NONE]", action;

  /** Build a Frame with list of language choices
   *  and two textfields to show the last selected
   *  and last activated items from this list.
   */
  public ListEvents2() {
    super("List Events in Java 1.1");
    setFont(new Font("Serif", Font.BOLD, 16));
    add("West", makeLanguagePanel());
    add("Center", makeReportPanel());
    pack();
    setVisible(true);
  }

  // Create Panel containing List with language choices.
  // Constructor puts this at left side of Frame.
  
  private Panel makeLanguagePanel() {
    Panel languagePanel = new Panel();
    languagePanel.setLayout(new BorderLayout());
    languagePanel.add("North",
                      new Label("Choose Language"));
    languageList = new List(3);
    String[] languages =
      { "Ada", "C", "C++", "Common Lisp", "Eiffel",
        "Forth", "Fortran", "Java", "Pascal",
        "Perl", "Scheme", "Smalltalk" };
    for(int i=0; i<languages.length; i++)
      languageList.add(languages[i]);
    showJava();
    languagePanel.add("Center", languageList);
    return(languagePanel);
  }

  // Creates Panel with two labels and two textfields.
  // The first will show the last selection in List; the
  // second the last item activated. The constructor puts
  // this Panel at the right of Frame.

  private Panel makeReportPanel() {
    Panel reportPanel = new Panel();
    reportPanel.setLayout(new GridLayout(4, 1));
    reportPanel.add(new Label("Last Selection:"));
    selectionField = new TextField();
    SelectionReporter selectionReporter =
      new SelectionReporter(selectionField);
    languageList.addItemListener(selectionReporter);
    reportPanel.add(selectionField);
    reportPanel.add(new Label("Last Action:"));
    actionField = new TextField();
    ActionReporter actionReporter =
      new ActionReporter(actionField);
    languageList.addActionListener(actionReporter);
    reportPanel.add(actionField);
    return(reportPanel);
  }

  /** Select and show "Java". */
  
  protected void showJava() {
    languageList.select(7);
    languageList.makeVisible(7);
  }
}

