001package edu.pdx.cs410J.grader.poa.ui;
002
003import com.google.inject.Singleton;
004import edu.pdx.cs410J.grader.poa.POASubmissionsView;
005
006import javax.swing.*;
007import javax.swing.event.ListSelectionEvent;
008import java.awt.*;
009import java.util.List;
010import java.util.Vector;
011
012@Singleton
013public class POASubmissionsPanel extends JPanel implements POASubmissionsView {
014
015  private final JList<String> submissions;
016  private final JButton downloadSubmissions;
017
018  public POASubmissionsPanel() {
019    submissions = new JList<>();
020    submissions.setVisibleRowCount(10);
021
022    this.setLayout(new BorderLayout());
023
024    this.add(new JScrollPane(submissions), BorderLayout.CENTER);
025
026    downloadSubmissions = new JButton("Download");
027    this.add(downloadSubmissions, BorderLayout.SOUTH);
028  }
029
030  @Override
031  public void setPOASubmissionsDescriptions(List<String> strings) {
032    submissions.setListData(new Vector<>(strings));
033  }
034
035  @Override
036  public void addSubmissionSelectedListener(POASubmissionSelectedListener listener) {
037    submissions.addListSelectionListener(e -> {
038      if (isFinalEventInUserSelection(e)) {
039        int selectedIndex = submissions.getSelectedIndex();
040        if (selectedIndex >= 0) {
041          listener.submissionSelected(selectedIndex);
042        }
043      }
044    });
045  }
046
047  @Override
048  public void addDownloadSubmissionsListener(DownloadSubmissionsListener listener) {
049    this.downloadSubmissions.addActionListener(e -> listener.downloadSubmissions());
050  }
051
052  @Override
053  public void selectPOASubmission(int index) {
054    this.submissions.setSelectedIndex(index);
055  }
056
057  private boolean isFinalEventInUserSelection(ListSelectionEvent e) {
058    return !e.getValueIsAdjusting();
059  }
060}