package edu.pdx.cs410J.grader.poa.ui;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import edu.pdx.cs410J.grader.poa.GradeBookView;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;

@Singleton
public class GradeBookWidget extends JPanel implements GradeBookView {
  private final JLabel gradeBookName;
  private final JButton loadGradeBookButton;
  private final Component parentComponent;

  @Inject
  public GradeBookWidget(TopLevelJFrame parentFrame) {
    this.parentComponent = parentFrame;
    gradeBookName = new JLabel();
    loadGradeBookButton = new JButton("Load grade book");

    this.setLayout(new FlowLayout());
    this.add(gradeBookName);
    this.add(loadGradeBookButton);
  }

  @Override
  public void setGradeBookName(String className) {
    this.gradeBookName.setText(className);
  }

  @Override
  public void addGradeBookFileListener(FileSelectedListener listener) {
    this.loadGradeBookButton.addActionListener(e -> displayFileChooser(listener));
  }

  @Override
  public void canSaveGradeBook(boolean canSaveGradeBook) {
    throw new UnsupportedOperationException("This method is not implemented yet");
  }

  private void displayFileChooser(FileSelectedListener listener) {
    JFileChooser chooser = new JFileChooser();
    chooser.setDialogTitle("Select Grade Book file");
    chooser.setApproveButtonMnemonic('l');
    chooser.setApproveButtonText("Load");
    chooser.setApproveButtonToolTipText("Load Grade Book from file");
    chooser.setFileFilter(new FileNameExtensionFilter("Grade Book XML files", "xml"));
    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

    int result = chooser.showOpenDialog(parentComponent);
    if (result == JFileChooser.APPROVE_OPTION) {
      listener.fileSelected(chooser.getSelectedFile());
    }
  }
}

@@ -413,16 +413,61 @@
 Button;%0A
+  private final JButton saveGradeBookButton;%0A
   privat
@@ -501,16 +501,16 @@
 ponent;%0A
-
 %0A  @Inje
@@ -698,16 +698,117 @@
  book%22);
+%0A    saveGradeBookButton = new JButton(%22Save grade book%22);%0A    saveGradeBookButton.setEnabled(false);
 %0A%0A    th
@@ -903,16 +903,51 @@
 utton);%0A
+    this.add(saveGradeBookButton);%0A
   %7D%0A%0A  @
@@ -1307,82 +1307,58 @@
   th
-row new UnsupportedOperationException(%22This method is not implemented yet%22
+is.saveGradeBookButton.setEnabled(canSaveGradeBook
 );%0A 

Add "Save grade book" button.