diff --git a/tmc-plugin/src/fi/helsinki/cs/tmc/ui/SubmissionResultWaitingDialog.java b/tmc-plugin/src/fi/helsinki/cs/tmc/ui/SubmissionResultWaitingDialog.java index 2ead22cb..ee212e68 100644 --- a/tmc-plugin/src/fi/helsinki/cs/tmc/ui/SubmissionResultWaitingDialog.java +++ b/tmc-plugin/src/fi/helsinki/cs/tmc/ui/SubmissionResultWaitingDialog.java @@ -71,7 +71,7 @@ public void showSubmissionButton(URI showSubmissionUrl) { if (showSubmissionUrl == null) { return; } - showSubmissionButton.setAction(new AbstractAction("Show submission in browser") { + showSubmissionButton.setAction(new AbstractAction("View submission in browser") { @Override public void actionPerformed(ActionEvent ae) { try { diff --git a/tmc-plugin/src/fi/helsinki/cs/tmc/ui/SuccessfulSubmissionDialog.java b/tmc-plugin/src/fi/helsinki/cs/tmc/ui/SuccessfulSubmissionDialog.java index c15885a2..64af1fc5 100644 --- a/tmc-plugin/src/fi/helsinki/cs/tmc/ui/SuccessfulSubmissionDialog.java +++ b/tmc-plugin/src/fi/helsinki/cs/tmc/ui/SuccessfulSubmissionDialog.java @@ -2,17 +2,19 @@ import fi.helsinki.cs.tmc.core.domain.Exercise; import fi.helsinki.cs.tmc.core.domain.submission.SubmissionResult; - -import java.awt.Component; - import fi.helsinki.cs.tmc.core.domain.submission.FeedbackAnswer; import fi.helsinki.cs.tmc.core.domain.submission.FeedbackQuestion; import fi.helsinki.cs.tmc.ui.feedback.FeedbackQuestionPanel; import fi.helsinki.cs.tmc.ui.feedback.FeedbackQuestionPanelFactory; +import com.google.common.base.Optional; + +import java.awt.Component; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.net.MalformedURLException; +import java.net.URI; import java.net.URL; import java.util.ArrayList; import java.util.List; @@ -25,28 +27,31 @@ import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JPanel; + import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.StringUtils; import org.openide.awt.HtmlBrowser; -import static fi.helsinki.cs.tmc.ui.Boxer.*; +import static fi.helsinki.cs.tmc.ui.Boxer.hbox; +import static fi.helsinki.cs.tmc.ui.Boxer.hglue; public class SuccessfulSubmissionDialog extends JDialog { - + private static final Logger log = Logger.getLogger(SuccessfulSubmissionDialog.class.getName()); - + private JButton okButton; - + private JButton showSubmissionButton; + private List feedbackQuestionPanels; public SuccessfulSubmissionDialog(Exercise exercise, SubmissionResult result) { this.setTitle(exercise.getName() + " passed"); - + JPanel contentPane = new JPanel(); contentPane.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8)); contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); setContentPane(contentPane); - + addYayLabel(); addVSpace(6); if (exercise.requiresReview() && !result.getMissingReviewPoints().isEmpty()) { @@ -59,17 +64,17 @@ public SuccessfulSubmissionDialog(Exercise exercise, SubmissionResult result) { addVSpace(20); addFeedbackQuestions(result); //TODO: maybe put in box addVSpace(10); - addOkButton(); - + addButtons(result); + pack(); } - + public void addOkListener(ActionListener okListener) { this.okButton.addActionListener(okListener); } - + public List getFeedbackAnswers() { - List answers = new ArrayList(); + List answers = new ArrayList<>(); for (FeedbackQuestionPanel panel : feedbackQuestionPanels) { FeedbackAnswer answer = panel.getAnswer(); if (answer != null) { @@ -78,42 +83,42 @@ public List getFeedbackAnswers() { } return answers; } - + private void addVSpace(int height) { add(Box.createVerticalStrut(height)); } - + private Box leftAligned(Component component) { return hbox(component, hglue()); } - + private void addYayLabel() { JLabel yayLabel = new JLabel("All tests passed on the server."); - + Font font = yayLabel.getFont(); font = font.deriveFont(Font.BOLD, font.getSize2D() * 1.2f); yayLabel.setFont(font); - + yayLabel.setForeground(new java.awt.Color(0, 153, 51)); yayLabel.setIcon(ConvenientDialogDisplayer.getDefault().getSmileyIcon()); - + getContentPane().add(leftAligned(yayLabel)); } - + private void addRequiresReviewLabels() { JLabel lbl1 = new JLabel("This exercise requires a code review."); JLabel lbl2 = new JLabel("It will have a yellow marker until it's accepted by an instructor."); getContentPane().add(leftAligned(lbl1)); getContentPane().add(leftAligned(lbl2)); } - + private void addPointsLabel(SubmissionResult result) { JLabel pointsLabel = new JLabel(getPointsMsg(result)); pointsLabel.setFont(pointsLabel.getFont().deriveFont(Font.BOLD)); - + getContentPane().add(leftAligned(pointsLabel)); } - + private String getPointsMsg(SubmissionResult result) { if (!result.getPoints().isEmpty()) { String msg = "Points permanently awarded: " + StringUtils.join(result.getPoints(), ", ") + "."; @@ -131,19 +136,19 @@ private void addModelSolutionButton(SubmissionResult result) { public void actionPerformed(ActionEvent ev) { try { HtmlBrowser.URLDisplayer.getDefault().showURLExternal(new URL(solutionUrl)); - } catch (Exception ex) { + } catch (MalformedURLException ex) { ConvenientDialogDisplayer.getDefault().displayError("Failed to open browser.\n" + ex.getMessage()); } } }); - + getContentPane().add(leftAligned(solutionButton)); } } private void addFeedbackQuestions(SubmissionResult result) { - this.feedbackQuestionPanels = new ArrayList(); - + this.feedbackQuestionPanels = new ArrayList<>(); + if (!result.getFeedbackQuestions().isEmpty() && result.getFeedbackAnswerUrl() != null) { for (FeedbackQuestion question : result.getFeedbackQuestions()) { try { @@ -153,12 +158,12 @@ private void addFeedbackQuestions(SubmissionResult result) { log.warning(e.getMessage()); } } - + if (!feedbackQuestionPanels.isEmpty()) { // Some failsafety JLabel feedbackLabel = new JLabel("Feedback (leave empty to not send)"); feedbackLabel.setFont(feedbackLabel.getFont().deriveFont(Font.BOLD)); getContentPane().add(leftAligned(feedbackLabel)); - + for (FeedbackQuestionPanel panel : feedbackQuestionPanels) { getContentPane().add(leftAligned(panel)); } @@ -167,17 +172,45 @@ private void addFeedbackQuestions(SubmissionResult result) { } } } - - private void addOkButton() { + + private void addButtons(SubmissionResult result) { + JPanel buttonPane = new JPanel(); + buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS)); + + Optional submissionUrl = Optional.absent(); + final String submissionUrlString = result.getSubmissionUrl(); + if (submissionUrlString != null && !submissionUrlString.isEmpty()) { + submissionUrl = Optional.of(URI.create(submissionUrlString)); + } + addShowSubmissionButton(buttonPane, submissionUrl); + addOkButton(buttonPane); + getContentPane().add(buttonPane); + } + + private void addOkButton(JPanel buttonPane) { okButton = new JButton("OK"); - okButton.addActionListener(new ActionListener() { + okButton.addActionListener((ActionEvent e) -> { + setVisible(false); + dispose(); + }); + buttonPane.add(hbox(hglue(), okButton)); + } + + private void addShowSubmissionButton(JPanel buttonPane, Optional submissionUrl) { + if (!submissionUrl.isPresent()) { + return; + } + showSubmissionButton = new JButton("View submission in browser"); + showSubmissionButton.setAction(new AbstractAction("View submission in browser") { @Override - public void actionPerformed(ActionEvent e) { - setVisible(false); - dispose(); + public void actionPerformed(ActionEvent ae) { + try { + HtmlBrowser.URLDisplayer.getDefault().showURLExternal(submissionUrl.get().toURL()); + } catch (MalformedURLException ex) { + ConvenientDialogDisplayer.getDefault().displayError("Failed to open browser.\n" + ex.getMessage()); + } } }); - getContentPane().add(hbox(hglue(), okButton)); + buttonPane.add(showSubmissionButton); } - }