001package edu.pdx.cs410J.grader.poa.ui;
002
003import com.google.inject.Inject;
004import com.google.inject.Singleton;
005import edu.pdx.cs410J.grader.poa.POAAssignmentsView;
006
007import javax.swing.*;
008import java.awt.*;
009import java.util.List;
010
011@Singleton
012public class POAAssignmentsWidget extends JPanel implements POAAssignmentsView {
013  private final JComboBox<String> assignments;
014  private final JLabel dueDate;
015
016  @Inject
017  public POAAssignmentsWidget() {
018    assignments = new JComboBox<>();
019    assignments.setEditable(false);
020
021    this.dueDate = new JLabel("Due:");
022
023    this.setLayout(new BorderLayout());
024    this.add(new JLabel("POA:"), BorderLayout.WEST);
025    this.add(this.assignments, BorderLayout.CENTER);
026    this.add(this.dueDate, BorderLayout.EAST);
027  }
028
029  @Override
030  public void setAssignments(List<String> assignments) {
031    String[] array = assignments.toArray(new String[assignments.size()]);
032    this.assignments.setModel(new DefaultComboBoxModel<>(array));
033  }
034
035  @Override
036  public void setSelectedAssignment(int index) {
037    this.assignments.setSelectedIndex(index);
038  }
039
040  @Override
041  public void addAssignmentSelectedHandler(AssignmentSelectedHandler handler) {
042    this.assignments.addActionListener(e -> {
043      int index = assignments.getSelectedIndex();
044      handler.assignmentSelected(index);
045    });
046  }
047
048  @Override
049  public void setSelectedAssignmentDueDate(String dueDate) {
050    this.dueDate.setText("Due: " + dueDate);
051  }
052}