EDU-555 Add As Task File action should be undoable and revertable
[idea/community.git] / python / educational-core / course-creator / src / com / jetbrains / edu / coursecreator / actions / CCHideFromStudent.java
index f794e69d0ea21bd2c45e182c206ca19e5cf37f98..3ce85b39d7f8d81cbc04dc66080df4d5f4f89765 100644 (file)
@@ -1,6 +1,10 @@
 package com.jetbrains.edu.coursecreator.actions;
 
+import com.intellij.ide.projectView.ProjectView;
 import com.intellij.openapi.application.ApplicationManager;
+import com.intellij.openapi.command.undo.DocumentReference;
+import com.intellij.openapi.command.undo.UndoableAction;
+import com.intellij.openapi.command.undo.UnexpectedUndoException;
 import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.editor.Editor;
 import com.intellij.openapi.fileEditor.FileEditor;
@@ -8,10 +12,13 @@ import com.intellij.openapi.fileEditor.FileEditorManager;
 import com.intellij.openapi.fileEditor.impl.text.PsiAwareTextEditorImpl;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.vfs.VirtualFile;
+import com.jetbrains.edu.coursecreator.CCUtils;
 import com.jetbrains.edu.learning.StudyUtils;
+import com.jetbrains.edu.learning.core.EduUtils;
 import com.jetbrains.edu.learning.courseFormat.Course;
 import com.jetbrains.edu.learning.courseFormat.Task;
 import com.jetbrains.edu.learning.courseFormat.TaskFile;
+import org.jetbrains.annotations.Nullable;
 
 import java.io.IOException;
 import java.util.Map;
@@ -19,14 +26,71 @@ import java.util.Map;
 public class CCHideFromStudent extends CCTaskFileActionBase {
 
   private static final Logger LOG = Logger.getInstance(CCHideFromStudent.class);
+  private static final String ACTION_NAME = "Hide from Student";
+
+  public CCHideFromStudent() {
+    super(ACTION_NAME);
+  }
 
   @Override
   protected void performAction(VirtualFile file, Task task, Course course, Project project) {
-    Map<String, TaskFile> taskFiles = task.getTaskFiles();
     TaskFile taskFile = StudyUtils.getTaskFile(project, file);
     if (taskFile == null) {
       return;
     }
+    EduUtils.runUndoableAction(project, ACTION_NAME, new HideTaskFile(project, course, file, task, taskFile));
+  }
+
+  private static class HideTaskFile implements UndoableAction {
+
+    private final Project myProject;
+    private final Course myCourse;
+    private final VirtualFile myFile;
+    private final Task myTask;
+    private final TaskFile myTaskFile;
+
+    public HideTaskFile(Project project, Course course, VirtualFile file, Task task, TaskFile taskFile) {
+      myProject = project;
+      myCourse = course;
+      myFile = file;
+      myTask = task;
+      myTaskFile = taskFile;
+    }
+
+    @Override
+    public void undo() throws UnexpectedUndoException {
+      myTask.getTaskFiles().put(myFile.getName(), myTaskFile);
+      CCUtils.createResourceFile(myFile, myCourse, StudyUtils.getTaskDir(myFile));
+      if (!myTaskFile.getAnswerPlaceholders().isEmpty() && FileEditorManager.getInstance(myProject).isFileOpen(myFile)) {
+        for (FileEditor fileEditor : FileEditorManager.getInstance(myProject).getEditors(myFile)) {
+          if (fileEditor instanceof PsiAwareTextEditorImpl) {
+            Editor editor = ((PsiAwareTextEditorImpl)fileEditor).getEditor();
+            StudyUtils.drawAllWindows(editor, myTaskFile);
+          }
+        }
+      }
+      ProjectView.getInstance(myProject).refresh();
+    }
+
+    @Override
+    public void redo() throws UnexpectedUndoException {
+      hideFromStudent(myFile, myProject, myTask.getTaskFiles(), myTaskFile);
+      ProjectView.getInstance(myProject).refresh();
+    }
+
+    @Nullable
+    @Override
+    public DocumentReference[] getAffectedDocuments() {
+      return new DocumentReference[0];
+    }
+
+    @Override
+    public boolean isGlobal() {
+      return true;
+    }
+  }
+
+  public static void hideFromStudent(VirtualFile file, Project project, Map<String, TaskFile> taskFiles, TaskFile taskFile) {
     if (!taskFile.getAnswerPlaceholders().isEmpty() && FileEditorManager.getInstance(project).isFileOpen(file)) {
       for (FileEditor fileEditor : FileEditorManager.getInstance(project).getEditors(file)) {
         if (fileEditor instanceof PsiAwareTextEditorImpl) {