From 230e7528c4ca23b1d1b515eed3cbd3a9d6c713c3 Mon Sep 17 00:00:00 2001 From: "Liana.Bakradze" Date: Fri, 7 Apr 2017 16:11:47 +0300 Subject: [PATCH] move onTaskFailed logic to StudyChecker (EDU-882) --- .../edu/learning/checker/StudyCheckTask.java | 15 +----- .../edu/learning/checker/StudyCheckUtils.java | 50 ++++++++++--------- .../learning/checker/StudyTaskChecker.java | 5 ++ .../courseFormat/tasks/ChoiceTask.java | 23 +++++++++ .../learning/courseFormat/tasks/CodeTask.java | 14 ++++++ .../edu/learning/PyStudyCheckAction.java | 35 +------------ 6 files changed, 71 insertions(+), 71 deletions(-) diff --git a/python/educational-core/src/com/jetbrains/edu/learning/checker/StudyCheckTask.java b/python/educational-core/src/com/jetbrains/edu/learning/checker/StudyCheckTask.java index 096ec8e5af8d..09e411d82931 100644 --- a/python/educational-core/src/com/jetbrains/edu/learning/checker/StudyCheckTask.java +++ b/python/educational-core/src/com/jetbrains/edu/learning/checker/StudyCheckTask.java @@ -205,21 +205,8 @@ public class StudyCheckTask extends com.intellij.openapi.progress.Task.Backgroun } protected void onTaskFailed(@NotNull String message) { - final Course course = StudyTaskManager.getInstance(myProject).getCourse(); myTask.setStatus(StudyStatus.Failed); - if (course != null) { - if (course.isAdaptive()) { - ApplicationManager.getApplication().invokeLater( - () -> { - StudyCheckUtils.showTestResultPopUp("Failed", MessageType.ERROR.getPopupBackground(), myProject); - StudyCheckUtils.showTestResultsToolWindow(myProject, message, false); - }); - } - else { - ApplicationManager.getApplication() - .invokeLater(() -> StudyCheckUtils.showTestResultPopUp(message, MessageType.ERROR.getPopupBackground(), myProject)); - } - } + myTask.getChecker(myProject).onTaskFailed(message); } protected void onTaskSolved(@NotNull String message) { diff --git a/python/educational-core/src/com/jetbrains/edu/learning/checker/StudyCheckUtils.java b/python/educational-core/src/com/jetbrains/edu/learning/checker/StudyCheckUtils.java index a6da62dffb4e..761cb2cfab6d 100644 --- a/python/educational-core/src/com/jetbrains/edu/learning/checker/StudyCheckUtils.java +++ b/python/educational-core/src/com/jetbrains/edu/learning/checker/StudyCheckUtils.java @@ -28,8 +28,8 @@ import com.jetbrains.edu.learning.core.EduDocumentListener; import com.jetbrains.edu.learning.core.EduNames; import com.jetbrains.edu.learning.core.EduUtils; import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder; -import com.jetbrains.edu.learning.courseFormat.tasks.Task; import com.jetbrains.edu.learning.courseFormat.TaskFile; +import com.jetbrains.edu.learning.courseFormat.tasks.Task; import com.jetbrains.edu.learning.editor.StudyEditor; import com.jetbrains.edu.learning.navigation.StudyNavigator; import com.jetbrains.edu.learning.ui.StudyTestResultsToolWindowFactory; @@ -202,29 +202,31 @@ public class StudyCheckUtils { } public static void showTestResultsToolWindow(@NotNull final Project project, @NotNull final String message, boolean solved) { - final ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project); - ToolWindow window = toolWindowManager.getToolWindow(StudyTestResultsToolWindowFactoryKt.ID); - if (window == null) { - toolWindowManager.registerToolWindow(StudyTestResultsToolWindowFactoryKt.ID, true, ToolWindowAnchor.BOTTOM); - window = toolWindowManager.getToolWindow(StudyTestResultsToolWindowFactoryKt.ID); - new StudyTestResultsToolWindowFactory().createToolWindowContent(project, window); - } - - final Content[] contents = window.getContentManager().getContents(); - for (Content content : contents) { - final JComponent component = content.getComponent(); - if (component instanceof ConsoleViewImpl) { - ((ConsoleViewImpl)component).clear(); - if (!solved) { - ((ConsoleViewImpl)component).print(message, ConsoleViewContentType.ERROR_OUTPUT); - } - else { - ((ConsoleViewImpl)component).print(message, ConsoleViewContentType.NORMAL_OUTPUT); - } - window.setAvailable(true, () -> {}); - window.show(() -> {}); - return; + ApplicationManager.getApplication().invokeLater(() -> { + final ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project); + ToolWindow window = toolWindowManager.getToolWindow(StudyTestResultsToolWindowFactoryKt.ID); + if (window == null) { + toolWindowManager.registerToolWindow(StudyTestResultsToolWindowFactoryKt.ID, true, ToolWindowAnchor.BOTTOM); + window = toolWindowManager.getToolWindow(StudyTestResultsToolWindowFactoryKt.ID); + new StudyTestResultsToolWindowFactory().createToolWindowContent(project, window); } - } + + final Content[] contents = window.getContentManager().getContents(); + for (Content content : contents) { + final JComponent component = content.getComponent(); + if (component instanceof ConsoleViewImpl) { + ((ConsoleViewImpl)component).clear(); + if (!solved) { + ((ConsoleViewImpl)component).print(message, ConsoleViewContentType.ERROR_OUTPUT); + } + else { + ((ConsoleViewImpl)component).print(message, ConsoleViewContentType.NORMAL_OUTPUT); + } + window.setAvailable(true, () -> {}); + window.show(() -> {}); + return; + } + } + }); } } diff --git a/python/educational-core/src/com/jetbrains/edu/learning/checker/StudyTaskChecker.java b/python/educational-core/src/com/jetbrains/edu/learning/checker/StudyTaskChecker.java index 704cfc0b960f..879621a1a725 100644 --- a/python/educational-core/src/com/jetbrains/edu/learning/checker/StudyTaskChecker.java +++ b/python/educational-core/src/com/jetbrains/edu/learning/checker/StudyTaskChecker.java @@ -19,4 +19,9 @@ public class StudyTaskChecker { ApplicationManager.getApplication().invokeLater( () -> StudyCheckUtils.showTestResultPopUp(message, MessageType.INFO.getPopupBackground(), myProject)); } + + public void onTaskFailed(@NotNull String message) { + ApplicationManager.getApplication() + .invokeLater(() -> StudyCheckUtils.showTestResultPopUp(message, MessageType.ERROR.getPopupBackground(), myProject)); + } } diff --git a/python/educational-core/src/com/jetbrains/edu/learning/courseFormat/tasks/ChoiceTask.java b/python/educational-core/src/com/jetbrains/edu/learning/courseFormat/tasks/ChoiceTask.java index f7ce2ae9a162..476af7826116 100644 --- a/python/educational-core/src/com/jetbrains/edu/learning/courseFormat/tasks/ChoiceTask.java +++ b/python/educational-core/src/com/jetbrains/edu/learning/courseFormat/tasks/ChoiceTask.java @@ -2,6 +2,11 @@ package com.jetbrains.edu.learning.courseFormat.tasks; import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; +import com.intellij.openapi.project.Project; +import com.jetbrains.edu.learning.StudyUtils; +import com.jetbrains.edu.learning.checker.StudyTaskChecker; +import com.jetbrains.edu.learning.editor.StudyChoiceVariantsPanel; +import com.jetbrains.edu.learning.ui.StudyToolWindow; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; @@ -48,4 +53,22 @@ public class ChoiceTask extends Task { public String getTaskType() { return "choice"; } + + @Override + public StudyTaskChecker getChecker(@NotNull Project project) { + return new StudyTaskChecker(this, project) { + @Override + public void onTaskFailed(@NotNull String message) { + super.onTaskFailed(message); + repaintChoicePanel(project, myTask); + } + + private void repaintChoicePanel(@NotNull Project project, @NotNull ChoiceTask task) { + final StudyToolWindow toolWindow = StudyUtils.getStudyToolWindow(project); + if (toolWindow != null) { + toolWindow.setBottomComponent(new StudyChoiceVariantsPanel(task)); + } + } + }; + } } diff --git a/python/educational-core/src/com/jetbrains/edu/learning/courseFormat/tasks/CodeTask.java b/python/educational-core/src/com/jetbrains/edu/learning/courseFormat/tasks/CodeTask.java index fb2d701cb6ac..3493f17ac262 100644 --- a/python/educational-core/src/com/jetbrains/edu/learning/courseFormat/tasks/CodeTask.java +++ b/python/educational-core/src/com/jetbrains/edu/learning/courseFormat/tasks/CodeTask.java @@ -1,5 +1,8 @@ package com.jetbrains.edu.learning.courseFormat.tasks; +import com.intellij.openapi.project.Project; +import com.jetbrains.edu.learning.checker.StudyCheckUtils; +import com.jetbrains.edu.learning.checker.StudyTaskChecker; import org.jetbrains.annotations.NotNull; public class CodeTask extends Task { @@ -14,4 +17,15 @@ public class CodeTask extends Task { public String getTaskType() { return "code"; } + + @Override + public StudyTaskChecker getChecker(@NotNull Project project) { + return new StudyTaskChecker(this, project) { + @Override + public void onTaskFailed(@NotNull String message) { + super.onTaskFailed("Wrong solution"); + StudyCheckUtils.showTestResultsToolWindow(myProject, message, false); + } + }; + } } diff --git a/python/educational-python/Edu-Python/src/com/jetbrains/edu/learning/PyStudyCheckAction.java b/python/educational-python/Edu-Python/src/com/jetbrains/edu/learning/PyStudyCheckAction.java index f1d4c8a21eb2..1948c1ce49a8 100644 --- a/python/educational-python/Edu-Python/src/com/jetbrains/edu/learning/PyStudyCheckAction.java +++ b/python/educational-python/Edu-Python/src/com/jetbrains/edu/learning/PyStudyCheckAction.java @@ -6,7 +6,6 @@ import com.intellij.openapi.command.CommandProcessor; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.progress.ProgressManager; import com.intellij.openapi.project.Project; -import com.intellij.openapi.ui.MessageType; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.wm.IdeFocusManager; import com.jetbrains.edu.learning.actions.StudyCheckAction; @@ -15,14 +14,10 @@ import com.jetbrains.edu.learning.checker.StudyCheckUtils; import com.jetbrains.edu.learning.checker.StudyTestRunner; import com.jetbrains.edu.learning.core.EduNames; import com.jetbrains.edu.learning.courseFormat.Course; -import com.jetbrains.edu.learning.courseFormat.StudyStatus; import com.jetbrains.edu.learning.courseFormat.TaskFile; -import com.jetbrains.edu.learning.courseFormat.tasks.ChoiceTask; import com.jetbrains.edu.learning.courseFormat.tasks.Task; -import com.jetbrains.edu.learning.editor.StudyChoiceVariantsPanel; import com.jetbrains.edu.learning.editor.StudyEditor; import com.jetbrains.edu.learning.statistics.EduUsagesCollector; -import com.jetbrains.edu.learning.ui.StudyToolWindow; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -83,9 +78,9 @@ public class PyStudyCheckAction extends StudyCheckAction { return new StudyCheckTask(project, studyState, myCheckInProgress, testProcess, commandLine) { @Override protected void onTaskFailed(@NotNull String message) { + super.onTaskFailed(message); ApplicationManager.getApplication().invokeLater(() -> { if (myTaskDir == null) return; - myTask.setStatus(StudyStatus.Failed); for (Map.Entry entry : myTask.getTaskFiles().entrySet()) { final String name = entry.getKey(); final TaskFile taskFile = entry.getValue(); @@ -99,38 +94,12 @@ public class PyStudyCheckAction extends StudyCheckAction { () -> StudyCheckUtils.runSmartTestProcess(myTaskDir, testRunner, name, taskFile, project))); } } - final StudyToolWindow toolWindow = StudyUtils.getStudyToolWindow(project); - if (toolWindow != null) { - final Course course = StudyTaskManager.getInstance(project).getCourse(); - if (course != null) { - if (course.isAdaptive()) { - if (myTask instanceof ChoiceTask) { - StudyCheckUtils.showTestResultPopUp("Wrong answer", MessageType.ERROR.getPopupBackground(), project); - repaintChoicePanel(project, (ChoiceTask)myTask); - } - else { - StudyCheckUtils.showTestResultPopUp("Wrong answer", MessageType.ERROR.getPopupBackground(), project); - StudyCheckUtils.showTestResultsToolWindow(project, message, false); - } - } - else { - StudyCheckUtils.showTestResultPopUp(message, MessageType.ERROR.getPopupBackground(), project); - } - } - StudyCheckUtils.navigateToFailedPlaceholder(myStudyState, myTask, myTaskDir, project); - } + StudyCheckUtils.navigateToFailedPlaceholder(myStudyState, myTask, myTaskDir, project); }); } }; } - private static void repaintChoicePanel(@NotNull Project project, @NotNull ChoiceTask task) { - final StudyToolWindow toolWindow = StudyUtils.getStudyToolWindow(project); - if (toolWindow != null) { - toolWindow.setBottomComponent(new StudyChoiceVariantsPanel(task)); - } - } - @Nullable private static VirtualFile getTaskVirtualFile(@NotNull final StudyState studyState, @NotNull final Task task,