extract util method
authorLiana Bakradze <liana.bakradze@jetbrains.com>
Fri, 5 Aug 2016 15:39:18 +0000 (18:39 +0300)
committerLiana Bakradze <liana.bakradze@jetbrains.com>
Fri, 5 Aug 2016 15:41:22 +0000 (18:41 +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
python/educational-core/student/src/com/jetbrains/edu/learning/core/EduUtils.java

index 7996e8ff8b329b9befc7de87ad48e0c3313e7b81..70e2f210eaaa8f892f1af37e4012e6bc2c9398e6 100644 (file)
@@ -2,10 +2,7 @@ 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.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.command.undo.UnexpectedUndoException;
 import com.intellij.openapi.editor.Document;
 import com.intellij.openapi.editor.Editor;
@@ -21,6 +18,7 @@ import com.jetbrains.edu.coursecreator.ui.CCCreateAnswerPlaceholderDialog;
 import com.jetbrains.edu.learning.StudyUtils;
 import com.jetbrains.edu.learning.core.EduAnswerPlaceholderPainter;
 import com.jetbrains.edu.learning.core.EduNames;
 import com.jetbrains.edu.learning.StudyUtils;
 import com.jetbrains.edu.learning.core.EduAnswerPlaceholderPainter;
 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.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;
@@ -84,12 +82,7 @@ public class CCAddAnswerPlaceholder extends CCAnswerPlaceholderAction {
     taskFile.sortAnswerPlaceholders();
     answerPlaceholder.setPossibleAnswer(model.hasSelection() ? model.getSelectedText() : defaultPlaceholderText);
     AddAction action = new AddAction(answerPlaceholder, taskFile, editor);
     taskFile.sortAnswerPlaceholders();
     answerPlaceholder.setPossibleAnswer(model.hasSelection() ? model.getSelectedText() : defaultPlaceholderText);
     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();
+    EduUtils.runUndoableAction(project, "Add Answer Placeholder", action);
   }
 
   static class AddAction extends BasicUndoableAction {
   }
 
   static class AddAction extends BasicUndoableAction {
@@ -123,23 +116,6 @@ public class CCAddAnswerPlaceholder extends CCAnswerPlaceholderAction {
     }
   }
 
     }
   }
 
-  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
   protected void performAnswerPlaceholderAction(@NotNull CCState state) {
     if (canAddPlaceholder(state)) {
   @Override
   protected void performAnswerPlaceholderAction(@NotNull CCState state) {
     if (canAddPlaceholder(state)) {
@@ -155,14 +131,17 @@ public class CCAddAnswerPlaceholder extends CCAnswerPlaceholderAction {
     Project project = state.getProject();
     TaskFile taskFile = state.getTaskFile();
     AnswerPlaceholder answerPlaceholder = state.getAnswerPlaceholder();
     Project project = state.getProject();
     TaskFile taskFile = state.getTaskFile();
     AnswerPlaceholder answerPlaceholder = state.getAnswerPlaceholder();
-    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);
+    EduUtils.runUndoableAction(project, "Delete Answer Placeholder", new AddAction(answerPlaceholder, taskFile, state.getEditor()) {
+      @Override
+      public void undo() throws UnexpectedUndoException {
+        super.redo();
       }
       }
-    }.execute();
 
 
+      @Override
+      public void redo() throws UnexpectedUndoException {
+        super.undo();
+      }
+    });
   }
 
   @Override
   }
 
   @Override
index 2caf360fb9b0754e9b8283e75db49200a098757b..37b21bd120bec81abec667a1293d6c9ad9773e06 100644 (file)
@@ -4,11 +4,8 @@ import com.intellij.openapi.actionSystem.AnActionEvent;
 import com.intellij.openapi.actionSystem.CommonDataKeys;
 import com.intellij.openapi.actionSystem.DataContext;
 import com.intellij.openapi.actionSystem.Presentation;
 import com.intellij.openapi.actionSystem.CommonDataKeys;
 import com.intellij.openapi.actionSystem.DataContext;
 import com.intellij.openapi.actionSystem.Presentation;
-import com.intellij.openapi.application.Result;
 import com.intellij.openapi.command.UndoConfirmationPolicy;
 import com.intellij.openapi.command.UndoConfirmationPolicy;
-import com.intellij.openapi.command.WriteCommandAction;
 import com.intellij.openapi.command.undo.BasicUndoableAction;
 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.command.undo.UnexpectedUndoException;
 import com.intellij.openapi.editor.Editor;
 import com.intellij.openapi.fileEditor.FileEditor;
@@ -20,6 +17,7 @@ import com.intellij.openapi.vfs.VirtualFile;
 import com.jetbrains.edu.coursecreator.CCUtils;
 import com.jetbrains.edu.learning.StudyUtils;
 import com.jetbrains.edu.learning.core.EduNames;
 import com.jetbrains.edu.coursecreator.CCUtils;
 import com.jetbrains.edu.learning.StudyUtils;
 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.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;
@@ -61,17 +59,7 @@ public class CCDeleteAllAnswerPlaceholdersAction extends DumbAwareAction {
     }
     List<AnswerPlaceholder> placeholders = new ArrayList<AnswerPlaceholder>(taskFile.getAnswerPlaceholders());
     final ClearPlaceholders action = new ClearPlaceholders(taskFile, placeholders, editor);
     }
     List<AnswerPlaceholder> placeholders = new ArrayList<AnswerPlaceholder>(taskFile.getAnswerPlaceholders());
     final ClearPlaceholders action = new ClearPlaceholders(taskFile, placeholders, editor);
-    new WriteCommandAction(project, ACTION_NAME) {
-      protected void run(@NotNull final Result result) throws Throwable {
-        action.redo();
-        UndoManager.getInstance(project).undoableActionPerformed(action);
-      }
-
-      @Override
-      protected UndoConfirmationPolicy getUndoConfirmationPolicy() {
-        return UndoConfirmationPolicy.REQUEST_CONFIRMATION;
-      }
-    }.execute();
+    EduUtils.runUndoableAction(project, ACTION_NAME, action, UndoConfirmationPolicy.REQUEST_CONFIRMATION);
   }
 
   private static void updateView(@NotNull final Editor editor,
   }
 
   private static void updateView(@NotNull final Editor editor,
index 9966a7b80ebd3dd005255664a2d16555d074e8e1..fc6effb6ea939573ed62dabe9f5b75f1fc03827d 100644 (file)
@@ -4,7 +4,12 @@ import com.intellij.ide.SaveAndSyncHandler;
 import com.intellij.openapi.actionSystem.AnActionEvent;
 import com.intellij.openapi.actionSystem.Presentation;
 import com.intellij.openapi.application.ApplicationManager;
 import com.intellij.openapi.actionSystem.AnActionEvent;
 import com.intellij.openapi.actionSystem.Presentation;
 import com.intellij.openapi.application.ApplicationManager;
+import com.intellij.openapi.application.Result;
 import com.intellij.openapi.command.CommandProcessor;
 import com.intellij.openapi.command.CommandProcessor;
+import com.intellij.openapi.command.UndoConfirmationPolicy;
+import com.intellij.openapi.command.WriteCommandAction;
+import com.intellij.openapi.command.undo.UndoManager;
+import com.intellij.openapi.command.undo.UndoableAction;
 import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.editor.Document;
 import com.intellij.openapi.fileEditor.FileDocumentManager;
 import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.editor.Document;
 import com.intellij.openapi.fileEditor.FileDocumentManager;
@@ -225,4 +230,22 @@ public class EduUtils {
     }
     return false;
   }
     }
     return false;
   }
+
+  public static void runUndoableAction(Project project, String name, UndoableAction action) {
+    runUndoableAction(project, name, action, UndoConfirmationPolicy.DO_NOT_REQUEST_CONFIRMATION);
+  }
+
+  public static void runUndoableAction(Project project, String name, UndoableAction action, UndoConfirmationPolicy confirmationPolicy) {
+    new WriteCommandAction(project, name) {
+      protected void run(@NotNull final Result result) throws Throwable {
+        action.redo();
+        UndoManager.getInstance(project).undoableActionPerformed(action);
+      }
+
+      @Override
+      protected UndoConfirmationPolicy getUndoConfirmationPolicy() {
+        return confirmationPolicy;
+      }
+    }.execute();
+  }
 }
 }