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