import java.awt.*;
import java.awt.event.*;

/** A class to demonstrate list selection/deselection
 *  and action events.
 *
 *  Taken from Core Web Programming from 
 *  Prentice Hall and Sun Microsystems Press,
 *  http://www.corewebprogramming.com/.
 *  &copy; 2001 Marty Hall and Larry Brown;
 *  may be freely used or adapted. 
 */

public class ListEvents extends CloseableFrame {
  public static void main(String[] args) {
    new ListEvents();
  }

  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 ListEvents() {
    super("List Events");
    setFont(new Font("Serif", Font.BOLD, 16));
    add(makeLanguagePanel(), BorderLayout.WEST);
    add(makeReportPanel(), BorderLayout.CENTER);
    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(new Label("Choose Language"), 
                      BorderLayout.NORTH);
    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);
  }
}