001package edu.pdx.cs410J.grader.poa.ui;
002
003import com.google.inject.Inject;
004import com.google.inject.Singleton;
005import edu.pdx.cs410J.grader.poa.POASubmissionView;
006
007import javax.swing.*;
008import javax.swing.text.BadLocationException;
009import javax.swing.text.html.HTMLDocument;
010import javax.swing.text.html.HTMLEditorKit;
011import java.io.IOException;
012import java.io.StringReader;
013
014@Singleton
015public class POASubmissionInformationWidgets implements POASubmissionView {
016
017  private final JLabel subjectLabel;
018  private final JLabel submitterLabel;
019  private final JLabel submissionTimeLabel;
020  private final JEditorPane submissionContent;
021  private final POAGradeWidgets gradesWidgets;
022
023  @Inject
024  public POASubmissionInformationWidgets(POAGradeWidgets gradesWidgets) {
025    this.gradesWidgets = gradesWidgets;
026
027    this.subjectLabel = new JLabel();
028    this.submitterLabel = new JLabel();
029    this.submissionTimeLabel = new JLabel();
030    this.submissionContent = new JEditorPane();
031    this.submissionContent.setEditable(false);
032  }
033
034  public JComponent getSubjectWidget() {
035    return createLabeledWidget("Subject:", this.subjectLabel);
036  }
037
038  private JPanel createLabeledWidget(String label, JLabel widget) {
039    JPanel panel = new JPanel();
040    panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
041    panel.add(new JLabel(label));
042    panel.add(Box.createHorizontalStrut(3));
043    panel.add(widget);
044    return panel;
045  }
046
047  public JComponent getSubmitterWidget() {
048    return createLabeledWidget("Submitted by:", this.submitterLabel);
049  }
050
051  public JComponent getSubmissionTimeWidget() {
052    JPanel panel = createLabeledWidget("Submitted on:", this.submissionTimeLabel);
053    panel.add(Box.createHorizontalGlue());
054    panel.add(gradesWidgets.getIsLateCheckbox());
055    return panel;
056  }
057
058  @Override
059  public void setSubmissionSubject(String subject) {
060    this.subjectLabel.setText(subject);
061  }
062
063  @Override
064  public void setSubmissionSubmitter(String submitter) {
065    this.submitterLabel.setText(submitter);
066  }
067
068  @Override
069  public void setSubmissionTime(String time) {
070    this.submissionTimeLabel.setText(time);
071  }
072
073  @Override
074  public void setContent(String content, POAContentType contentType) throws CouldNotParseContent {
075    this.submissionContent.setContentType(contentType.getContentType());
076    if (contentType == POAContentType.HTML) {
077      try {
078        displayHtmlContent(content);
079
080      } catch (IOException | BadLocationException ex) {
081        throw new CouldNotParseContent(ex);
082      }
083    } else {
084      this.submissionContent.setText(content);
085    }
086    scrollPaneToTop();
087  }
088
089  private void displayHtmlContent(String html) throws IOException, BadLocationException {
090    HTMLEditorKit kit = (HTMLEditorKit) this.submissionContent.getEditorKit();
091    HTMLDocument document = (HTMLDocument) this.submissionContent.getDocument();
092    document.getDocumentProperties().put("IgnoreCharsetDirective", true);
093    StringReader reader = new StringReader(html);
094    kit.read(reader, document, 0);
095  }
096
097  private void scrollPaneToTop() {
098    this.submissionContent.setCaretPosition(0);
099  }
100
101  public JComponent getSubmissionContentWidget() {
102    return new JScrollPane(this.submissionContent);
103  }
104}