EDU-345 Delete all placeholders in task file action
[idea/community.git] / python / educational / course-creator / src / com / jetbrains / edu / coursecreator / actions / CCDeleteAllAnswerPlaceholdersAction.java
1 package com.jetbrains.edu.coursecreator.actions;
2
3 import com.intellij.openapi.actionSystem.AnActionEvent;
4 import com.intellij.openapi.actionSystem.CommonDataKeys;
5 import com.intellij.openapi.actionSystem.DataContext;
6 import com.intellij.openapi.application.Result;
7 import com.intellij.openapi.command.UndoConfirmationPolicy;
8 import com.intellij.openapi.command.WriteCommandAction;
9 import com.intellij.openapi.command.undo.*;
10 import com.intellij.openapi.editor.Editor;
11 import com.intellij.openapi.fileEditor.FileEditor;
12 import com.intellij.openapi.fileEditor.FileEditorManager;
13 import com.intellij.openapi.fileEditor.TextEditor;
14 import com.intellij.openapi.project.DumbAwareAction;
15 import com.intellij.openapi.project.Project;
16 import com.intellij.openapi.vfs.VirtualFile;
17 import com.jetbrains.edu.EduUtils;
18 import com.jetbrains.edu.courseFormat.AnswerPlaceholder;
19 import com.jetbrains.edu.courseFormat.TaskFile;
20 import com.jetbrains.edu.coursecreator.CCProjectService;
21 import org.jetbrains.annotations.NotNull;
22 import org.jetbrains.annotations.Nullable;
23
24 import java.util.ArrayList;
25 import java.util.List;
26
27 public class CCDeleteAllAnswerPlaceholdersAction extends DumbAwareAction {
28
29   public static final String ACTION_NAME = "Delete All Answer Placeholders";
30
31   public CCDeleteAllAnswerPlaceholdersAction() {
32     super(ACTION_NAME);
33   }
34
35   @Override
36   public void actionPerformed(AnActionEvent e) {
37     final DataContext context = e.getDataContext();
38     final VirtualFile file = CommonDataKeys.VIRTUAL_FILE.getData(context);
39     final Project project = e.getProject();
40     if (file == null || project == null) {
41       return;
42     }
43     final TaskFile taskFile = CCProjectService.getInstance(project).getTaskFile(file);
44     if (taskFile == null) {
45       return;
46     }
47     Editor editor = CommonDataKeys.EDITOR.getData(context);
48     if (editor == null) {
49       FileEditorManager instance = FileEditorManager.getInstance(project);
50       if (!instance.isFileOpen(file)) {
51         return;
52       }
53       FileEditor fileEditor = instance.getSelectedEditor(file);
54       if (!(fileEditor instanceof TextEditor)) {
55         return;
56       }
57       editor = ((TextEditor)fileEditor).getEditor();
58     }
59     List<AnswerPlaceholder> placeholders = new ArrayList<AnswerPlaceholder>(taskFile.getAnswerPlaceholders());
60     final ClearPlaceholders action = new ClearPlaceholders(taskFile, placeholders, editor, file, project);
61     new WriteCommandAction(project, ACTION_NAME) {
62       protected void run(@NotNull final Result result) throws Throwable {
63         action.redo();
64         UndoManager.getInstance(project).undoableActionPerformed(action);
65       }
66
67       @Override
68       protected UndoConfirmationPolicy getUndoConfirmationPolicy() {
69         return UndoConfirmationPolicy.REQUEST_CONFIRMATION;
70       }
71     }.execute();
72   }
73
74   private static void updateView(@NotNull final Editor editor,
75                                  @NotNull final VirtualFile file,
76                                  @NotNull final Project project) {
77     editor.getMarkupModel().removeAllHighlighters();
78     CCProjectService.getInstance(project).drawAnswerPlaceholders(file, editor);
79   }
80
81   @Override
82   public void update(AnActionEvent e) {
83     DataContext context = e.getDataContext();
84     VirtualFile file = CommonDataKeys.VIRTUAL_FILE.getData(context);
85     final Project project = e.getProject();
86     if (file == null || project == null) {
87       EduUtils.enableAction(e, false);
88       return;
89     }
90     TaskFile taskFile = CCProjectService.getInstance(project).getTaskFile(file);
91     if (taskFile == null) {
92       EduUtils.enableAction(e, false);
93       return;
94     }
95     if (taskFile.getAnswerPlaceholders().isEmpty()) {
96       EduUtils.enableAction(e, false);
97     }
98   }
99
100
101   private static class ClearPlaceholders implements UndoableAction {
102     private final List<AnswerPlaceholder> myPlaceholders;
103     private final Editor myEditor;
104     private final VirtualFile myFile;
105     private final Project myProject;
106     TaskFile myTaskFile;
107
108     public ClearPlaceholders(TaskFile taskFile, List<AnswerPlaceholder> placeholders, Editor editor, VirtualFile file, Project project) {
109       myTaskFile = taskFile;
110       myPlaceholders = placeholders;
111       myEditor = editor;
112       myFile = file;
113       myProject = project;
114     }
115
116     @Override
117     public void undo() throws UnexpectedUndoException {
118       myTaskFile.getAnswerPlaceholders().addAll(myPlaceholders);
119       updateView(myEditor, myFile, myProject);
120     }
121
122     @Override
123     public void redo() throws UnexpectedUndoException {
124       myTaskFile.getAnswerPlaceholders().clear();
125       updateView(myEditor, myFile, myProject);
126     }
127
128     @Nullable
129     @Override
130     public DocumentReference[] getAffectedDocuments() {
131       DocumentReference reference = DocumentReferenceManager.getInstance().create(myEditor.getDocument());
132       return new DocumentReference[]{reference};
133     }
134
135     @Override
136     public boolean isGlobal() {
137       return true;
138     }
139   }
140 }