import java.awt.*;

// 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.0.
 */

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

  private 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 ListEvents1() {
    super("List Events in Java 1.0");
    setFont(new Font("TimesRoman", Font.BOLD, 16));
    add("West", makeLanguagePanel());
    add("Center", makeReportPanel());
    pack();
    show();
  }

  /** Watches for Event.LIST_SELECT and
   *  Event.LIST_DESELECT events in textfield.
   */
  public boolean handleEvent(Event event) {
    if (event.target == languageList) {
      if (event.id == Event.LIST_SELECT) {
        selection = languageList.getSelectedItem();
        selectionField.setText(selection);
        System.out.println("Selected " + selection);
        return(true);
      } else if (event.id == Event.LIST_DESELECT) {
        selectionField.setText("");
        System.out.println("Deselected " + selection);
        return(true);
      }
    }
    return(super.handleEvent(event));
  }

  /** Reports when list item is activated. Item
   *  is either (String)object or the list's
   *  getSelectedItem.
   */
  public boolean action(Event event, Object object) {
    if (event.target == languageList) {
      action = languageList.getSelectedItem();
      actionField.setText(action);
      System.out.println("Activated " + action);
      return(true);
    } else
      return(false);
  }
  
  // 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, false);
    String[] languages =
      { "Ada", "C", "C++", "Common Lisp", "Eiffel",
        "Forth", "Fortran", "Java", "Pascal",
        "Perl", "Scheme", "Smalltalk" };
    for(int i=0; i<languages.length; i++)
      languageList.addItem(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();
    reportPanel.add(selectionField);
    reportPanel.add(new Label("Last Action:"));
    actionField = new TextField();
    reportPanel.add(actionField);
    return(reportPanel);
  }

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

