001package edu.pdx.cs410J.grader.poa.ui;
002
003import com.google.inject.Inject;
004import com.google.inject.Singleton;
005import edu.pdx.cs410J.grader.poa.StudentsView;
006
007import javax.swing.*;
008import java.awt.*;
009import java.util.List;
010
011@Singleton
012public class StudentsWidget extends JPanel implements StudentsView {
013
014  private final JComboBox<String> students;
015
016  @Inject
017  public StudentsWidget() {
018    students = new JComboBox<>();
019    students.setEditable(false);
020
021    this.setLayout(new BorderLayout());
022    this.add(new JLabel("Student: "), BorderLayout.WEST);
023    this.add(students, BorderLayout.CENTER);
024  }
025
026  @Override
027  public void setStudents(List<String> students) {
028    String[] array = students.toArray(new String[students.size()]);
029    this.students.setModel(new DefaultComboBoxModel<>(array));
030  }
031
032  @Override
033  public void setSelectedStudentIndex(int index) {
034    this.students.setSelectedIndex(index);
035  }
036
037  @Override
038  public void addSelectStudentHandler(SelectStudentHandler handler) {
039    this.students.addActionListener(e -> {
040      int index = students.getSelectedIndex();
041      handler.studentSelected(index);
042    });
043  }
044}