001package edu.pdx.cs410J.grader.poa.ui;
002
003import com.google.inject.Singleton;
004import edu.pdx.cs410J.grader.poa.POAGradeView;
005
006import javax.swing.*;
007import java.awt.*;
008import java.awt.event.FocusEvent;
009import java.awt.event.FocusListener;
010
011@Singleton
012public class POAGradeWidgets implements POAGradeView {
013  private final JCheckBox isLateCheckbox;
014  private final JLabel totalPointsLabel;
015  private final JTextField score;
016  private final JButton recordGrade;
017
018  public POAGradeWidgets() {
019    isLateCheckbox = new JCheckBox("Late");
020    totalPointsLabel = new JLabel("out of");
021    score = new JTextField(4);
022    recordGrade = new JButton("Save Grade");
023  }
024
025  public JCheckBox getIsLateCheckbox() {
026    return isLateCheckbox;
027  }
028
029  @Override
030  public void setIsEnabled(boolean isEnabled) {
031    this.isLateCheckbox.setEnabled(isEnabled);
032    this.score.setEnabled(isEnabled);
033    this.recordGrade.setEnabled(isEnabled);
034  }
035
036  @Override
037  public void setIsLate(boolean isLate) {
038    this.isLateCheckbox.setSelected(isLate);
039  }
040
041  @Override
042  public void addIsLateHandler(IsLateHandler handler) {
043    this.isLateCheckbox.addItemListener(e -> handler.setIsLate(isLateCheckbox.isSelected()));
044  }
045
046  @Override
047  public void setTotalPoints(String totalPoints) {
048    this.totalPointsLabel.setText("out of " + totalPoints);
049  }
050
051  @Override
052  public void addScoreValueHandler(ScoreValueHandler handler) {
053    this.score.addFocusListener(new FocusListener() {
054      @Override
055      public void focusGained(FocusEvent e) {
056
057      }
058
059      @Override
060      public void focusLost(FocusEvent e) {
061        handler.scoreValue(score.getText());
062      }
063    });
064  }
065
066  @Override
067  public void setErrorInScore(boolean errorInScore) {
068    if (errorInScore) {
069      this.score.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
070      this.recordGrade.setEnabled(false);
071
072    } else {
073      this.score.setBorder(null);
074      this.recordGrade.setEnabled(true);
075    }
076  }
077
078  @Override
079  public void setScore(String score) {
080    this.score.setText(score);
081    this.recordGrade.setEnabled(true);
082  }
083
084  @Override
085  public void setScoreHasBeenRecorded(boolean hasScoreBeenRecorded) {
086    if (hasScoreBeenRecorded) {
087      this.recordGrade.setText("Update Grade");
088
089    } else {
090      this.recordGrade.setText("Save Grade");
091    }
092  }
093
094  @Override
095  public void addRecordGradeHandler(RecordGradeHandler handler) {
096    this.recordGrade.addActionListener(e -> handler.recordGrade());
097  }
098
099  public JComponent getGradeWidget() {
100    JPanel panel = new JPanel();
101    panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
102    panel.add(new JLabel("Grade:"));
103    panel.add(Box.createHorizontalStrut(3));
104    panel.add(this.score);
105    panel.add(Box.createHorizontalStrut(3));
106    panel.add(this.totalPointsLabel);
107    panel.add(Box.createHorizontalStrut(6));
108    panel.add(this.recordGrade);
109    panel.add(Box.createHorizontalGlue());
110    return panel;
111  }
112}