package uiintro;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ListDemo extends JFrame {

  String names[] = {
    "Adi,Maleakhi Nugraha", "Alemzadeh,Yasaman", "Bennett,Alister Charles",
    "Bhatt,Sanjay Kiran", "Cain,Ryan M.", "Chau,Huy Vinh",
    "Chirilov,Joseph Matthew", "Chung,Loc Thang", "Dao,Tim A.", "Diep,Hiep Bao",
    "Ghani,Zohaib Shahab", "Haverlock,Kristin Lynn", "Hoang,Guitar", "Ige,Emile Yuri",
    "Koh,Eun Saem", "Lam,An Thu", "Lam,Dennis", "Le,Hung D.",  "Le,Robert Minh",
    "Le,To Oanh Thi", "Le,Totrinh Thi", "Lecong,Phuong", "Lee,Ann Yoo-Sook",
    "Li,Santiago Ca Hung", "Lohia,Pooja Ramesh", "Lu,Nguyen", "Luong,Ivy Phuong",
    "Madjd,Amir Mehdi", "Murtha,Lili Alison", "Nguyen,Ben Toan", "Nguyen,Brandon Tai",
    "Nguyen,David Huy", "Nguyen,Tho Van", "On,Perry P.", "Pang,Henry Robert",
    "Pangestu,Jovanka Ursula", "Reyes,Alfredo P. Iii", "Sanchez,Samuel",
    "Singh,Meena", "Song,Chan-Young", "Song,Joseph Sup", "Stemke,Gerad Jeffrey",
    "Tran,Tran Huyen", "Tran,Vu Hoan", "Tran,Vu Quoc", "Vo,Thaibinh Ha",
    "Wu,Kevin Kaiyen", "Yamada,Kevin Kyosei"
  };

  private JList list;

  public ListDemo() {

    this.setSize(200, 350);

    list = new JList(names);

    // a panel with a couple of buttons for the bottom of the display
    //
    JPanel buttons = new JPanel(new FlowLayout());
    JButton ok = new JButton("OK");
    ok.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        okaySelected();
      }
    });
    JButton cancel = new JButton("Cancel");
    cancel.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        cancelSelected();
      }
    });
    buttons.add(ok);
    buttons.add(cancel);

    // put the list in the middle, inside a scrollpane, and the buttons on
    // the bottom
    //
    this.getContentPane().setLayout(new BorderLayout());
    this.getContentPane().add(new JScrollPane(list), BorderLayout.CENTER);
    this.getContentPane().add(buttons, BorderLayout.SOUTH);

    this.setVisible(true);
  }


  public void okaySelected() {
    String msg = "You selected " + list.getSelectedValues().length + " value(s).";
    JOptionPane.showMessageDialog(this, msg);
    this.setVisible(false);
  }

  public void cancelSelected() {
    this.setVisible(false);
  }





  // Lists follow an MVC model that separates models from views and controllers.
  // Renderer is an object that generates a view. Just to illlustrate the idea, this
  // is a slightly different view which also shows the index of the list items.
  //
  class studentNameCellRenderer extends JLabel implements ListCellRenderer {

    Color highlightColor = new Color(0, 0, 128);

    public studentNameCellRenderer() {
      setOpaque(true);
    }

    public Component getListCellRendererComponent(JList list, Object value, int index,
                                                  boolean isSelected, boolean cellHasFocus) {
      setText(names[index] + " (" + index + ")");
      if (isSelected) {
        setBackground(highlightColor);
        setForeground(Color.white);
      } else {
        setBackground(Color.white);
        setForeground(Color.black);
      }

      return this;
  }

}

}
