EDU-469 Add ability to undo answer placeholder creation/deletion
authorLiana Bakradze <liana.bakradze@jetbrains.com>
Fri, 5 Aug 2016 12:44:02 +0000 (15:44 +0300)
committerLiana Bakradze <liana.bakradze@jetbrains.com>
Fri, 5 Aug 2016 12:44:02 +0000 (15:44 +0300)
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCAddAnswerPlaceholder.java
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCDeleteAllAnswerPlaceholdersAction.java

index 0be876ae46e19d5c8496708df3556fa3e8df70d1..7996e8ff8b329b9befc7de87ad48e0c3313e7b81 100644 (file)
@@ -2,6 +2,11 @@ package com.jetbrains.edu.coursecreator.actions;
 
 import com.intellij.openapi.actionSystem.AnActionEvent;
 import com.intellij.openapi.actionSystem.Presentation;
 
 import com.intellij.openapi.actionSystem.AnActionEvent;
 import com.intellij.openapi.actionSystem.Presentation;
+import com.intellij.openapi.application.Result;
+import com.intellij.openapi.command.WriteCommandAction;
+import com.intellij.openapi.command.undo.BasicUndoableAction;
+import com.intellij.openapi.command.undo.UndoManager;
+import com.intellij.openapi.command.undo.UnexpectedUndoException;
 import com.intellij.openapi.editor.Document;
 import com.intellij.openapi.editor.Editor;
 import com.intellij.openapi.editor.SelectionModel;
 import com.intellij.openapi.editor.Document;
 import com.intellij.openapi.editor.Editor;
 import com.intellij.openapi.editor.SelectionModel;
@@ -75,13 +80,64 @@ public class CCAddAnswerPlaceholder extends CCAnswerPlaceholderAction {
     TaskFile taskFile = state.getTaskFile();
     int index = taskFile.getAnswerPlaceholders().size();
     answerPlaceholder.setIndex(index);
     TaskFile taskFile = state.getTaskFile();
     int index = taskFile.getAnswerPlaceholders().size();
     answerPlaceholder.setIndex(index);
-    taskFile.addAnswerPlaceholder(answerPlaceholder);
     answerPlaceholder.setTaskFile(taskFile);
     taskFile.sortAnswerPlaceholders();
     answerPlaceholder.setTaskFile(taskFile);
     taskFile.sortAnswerPlaceholders();
-
     answerPlaceholder.setPossibleAnswer(model.hasSelection() ? model.getSelectedText() : defaultPlaceholderText);
     answerPlaceholder.setPossibleAnswer(model.hasSelection() ? model.getSelectedText() : defaultPlaceholderText);
-    EduAnswerPlaceholderPainter.drawAnswerPlaceholder(editor, answerPlaceholder, JBColor.BLUE);
-    EduAnswerPlaceholderPainter.createGuardedBlocks(editor, answerPlaceholder);
+    AddAction action = new AddAction(answerPlaceholder, taskFile, editor);
+    new WriteCommandAction(project, "Add Answer Placeholder") {
+      protected void run(@NotNull final Result result) throws Throwable {
+        action.redo();
+        UndoManager.getInstance(project).undoableActionPerformed(action);
+      }
+    }.execute();
+  }
+
+  static class AddAction extends BasicUndoableAction {
+    private final AnswerPlaceholder myPlaceholder;
+    private final TaskFile myTaskFile;
+    private final Editor myEditor;
+
+    public AddAction(AnswerPlaceholder placeholder, TaskFile taskFile, Editor editor) {
+      super(editor.getDocument());
+      myPlaceholder = placeholder;
+      myTaskFile = taskFile;
+      myEditor = editor;
+    }
+
+    @Override
+    public void undo() throws UnexpectedUndoException {
+      final List<AnswerPlaceholder> answerPlaceholders = myTaskFile.getAnswerPlaceholders();
+      if (answerPlaceholders.contains(myPlaceholder)) {
+        answerPlaceholders.remove(myPlaceholder);
+        myEditor.getMarkupModel().removeAllHighlighters();
+        StudyUtils.drawAllWindows(myEditor, myTaskFile);
+        EduAnswerPlaceholderPainter.createGuardedBlocks(myEditor, myTaskFile);
+      }
+    }
+
+    @Override
+    public void redo() throws UnexpectedUndoException {
+      myTaskFile.addAnswerPlaceholder(myPlaceholder);
+      EduAnswerPlaceholderPainter.drawAnswerPlaceholder(myEditor, myPlaceholder, JBColor.BLUE);
+      EduAnswerPlaceholderPainter.createGuardedBlocks(myEditor, myPlaceholder);
+    }
+  }
+
+  static class DeleteAction extends AddAction {
+
+    public DeleteAction(AnswerPlaceholder placeholder, TaskFile taskFile, Editor editor) {
+      super(placeholder, taskFile, editor);
+    }
+
+    @Override
+    public void undo() throws UnexpectedUndoException {
+      super.redo();
+    }
+
+    @Override
+    public void redo() throws UnexpectedUndoException {
+      super.undo();
+    }
   }
 
   @Override
   }
 
   @Override
@@ -97,19 +153,16 @@ public class CCAddAnswerPlaceholder extends CCAnswerPlaceholderAction {
 
   private static void deletePlaceholder(@NotNull CCState state) {
     Project project = state.getProject();
 
   private static void deletePlaceholder(@NotNull CCState state) {
     Project project = state.getProject();
-    PsiFile psiFile = state.getFile();
-    final Document document = PsiDocumentManager.getInstance(project).getDocument(psiFile);
-    if (document == null) return;
     TaskFile taskFile = state.getTaskFile();
     AnswerPlaceholder answerPlaceholder = state.getAnswerPlaceholder();
     TaskFile taskFile = state.getTaskFile();
     AnswerPlaceholder answerPlaceholder = state.getAnswerPlaceholder();
-    final List<AnswerPlaceholder> answerPlaceholders = taskFile.getAnswerPlaceholders();
-    if (answerPlaceholders.contains(answerPlaceholder)) {
-      answerPlaceholders.remove(answerPlaceholder);
-      final Editor editor = state.getEditor();
-      editor.getMarkupModel().removeAllHighlighters();
-      StudyUtils.drawAllWindows(editor, taskFile);
-      EduAnswerPlaceholderPainter.createGuardedBlocks(editor, taskFile);
-    }
+    DeleteAction action = new DeleteAction(answerPlaceholder, taskFile, state.getEditor());
+    new WriteCommandAction(project, "Delete Answer Placeholder") {
+      protected void run(@NotNull final Result result) throws Throwable {
+        action.redo();
+        UndoManager.getInstance(project).undoableActionPerformed(action);
+      }
+    }.execute();
+
   }
 
   @Override
   }
 
   @Override
index d5ca9e5b883884f418a05d3657635ec291dad0f5..2caf360fb9b0754e9b8283e75db49200a098757b 100644 (file)
@@ -7,7 +7,9 @@ import com.intellij.openapi.actionSystem.Presentation;
 import com.intellij.openapi.application.Result;
 import com.intellij.openapi.command.UndoConfirmationPolicy;
 import com.intellij.openapi.command.WriteCommandAction;
 import com.intellij.openapi.application.Result;
 import com.intellij.openapi.command.UndoConfirmationPolicy;
 import com.intellij.openapi.command.WriteCommandAction;
-import com.intellij.openapi.command.undo.*;
+import com.intellij.openapi.command.undo.BasicUndoableAction;
+import com.intellij.openapi.command.undo.UndoManager;
+import com.intellij.openapi.command.undo.UnexpectedUndoException;
 import com.intellij.openapi.editor.Editor;
 import com.intellij.openapi.fileEditor.FileEditor;
 import com.intellij.openapi.fileEditor.FileEditorManager;
 import com.intellij.openapi.editor.Editor;
 import com.intellij.openapi.fileEditor.FileEditor;
 import com.intellij.openapi.fileEditor.FileEditorManager;
@@ -21,7 +23,6 @@ import com.jetbrains.edu.learning.core.EduNames;
 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
 import com.jetbrains.edu.learning.courseFormat.TaskFile;
 import org.jetbrains.annotations.NotNull;
 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
 import com.jetbrains.edu.learning.courseFormat.TaskFile;
 import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
 
 import java.util.ArrayList;
 import java.util.List;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -104,12 +105,13 @@ public class CCDeleteAllAnswerPlaceholdersAction extends DumbAwareAction {
   }
 
 
   }
 
 
-  private static class ClearPlaceholders implements UndoableAction {
+  private static class ClearPlaceholders extends BasicUndoableAction {
     private final List<AnswerPlaceholder> myPlaceholders;
     private final Editor myEditor;
     private final TaskFile myTaskFile;
 
     public ClearPlaceholders(TaskFile taskFile, List<AnswerPlaceholder> placeholders, Editor editor) {
     private final List<AnswerPlaceholder> myPlaceholders;
     private final Editor myEditor;
     private final TaskFile myTaskFile;
 
     public ClearPlaceholders(TaskFile taskFile, List<AnswerPlaceholder> placeholders, Editor editor) {
+      super(editor.getDocument());
       myTaskFile = taskFile;
       myPlaceholders = placeholders;
       myEditor = editor;
       myTaskFile = taskFile;
       myPlaceholders = placeholders;
       myEditor = editor;
@@ -127,13 +129,6 @@ public class CCDeleteAllAnswerPlaceholdersAction extends DumbAwareAction {
       updateView(myEditor, myTaskFile);
     }
 
       updateView(myEditor, myTaskFile);
     }
 
-    @Nullable
-    @Override
-    public DocumentReference[] getAffectedDocuments() {
-      DocumentReference reference = DocumentReferenceManager.getInstance().create(myEditor.getDocument());
-      return new DocumentReference[]{reference};
-    }
-
     @Override
     public boolean isGlobal() {
       return true;
     @Override
     public boolean isGlobal() {
       return true;