reorganize answer placeholder actions
[idea/community.git] / python / educational-core / course-creator / src / com / jetbrains / edu / coursecreator / actions / placeholder / CCDeleteAllAnswerPlaceholdersAction.java
1 package com.jetbrains.edu.coursecreator.actions.placeholder;
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.command.UndoConfirmationPolicy;
8 import com.intellij.openapi.command.undo.BasicUndoableAction;
9 import com.intellij.openapi.command.undo.UnexpectedUndoException;
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.coursecreator.CCUtils;
18 import com.jetbrains.edu.learning.StudyUtils;
19 import com.jetbrains.edu.learning.core.EduUtils;
20 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
21 import com.jetbrains.edu.learning.courseFormat.TaskFile;
22 import org.jetbrains.annotations.NotNull;
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";
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 = StudyUtils.getTaskFile(project, 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<>(taskFile.getAnswerPlaceholders());
60     final ClearPlaceholders action = new ClearPlaceholders(taskFile, placeholders, editor);
61     EduUtils.runUndoableAction(project, ACTION_NAME, action, UndoConfirmationPolicy.REQUEST_CONFIRMATION);
62   }
63
64   private static void updateView(@NotNull final Editor editor,
65                                  @NotNull final TaskFile taskFile) {
66     editor.getMarkupModel().removeAllHighlighters();
67     StudyUtils.drawAllAnswerPlaceholders(editor, taskFile);
68   }
69
70   @Override
71   public void update(AnActionEvent e) {
72     Presentation presentation = e.getPresentation();
73     presentation.setEnabledAndVisible(false);
74
75     Project project = e.getProject();
76     if (project == null) {
77       return;
78     }
79     if (!CCUtils.isCourseCreator(project)) {
80       return;
81     }
82     DataContext context = e.getDataContext();
83     VirtualFile file = CommonDataKeys.VIRTUAL_FILE.getData(context);
84     if (file == null ) {
85       return;
86     }
87     TaskFile taskFile = StudyUtils.getTaskFile(project, file);
88     if (taskFile == null || taskFile.getAnswerPlaceholders().isEmpty()) {
89       return;
90     }
91     presentation.setEnabledAndVisible(true);
92   }
93
94
95   private static class ClearPlaceholders extends BasicUndoableAction {
96     private final List<AnswerPlaceholder> myPlaceholders;
97     private final Editor myEditor;
98     private final TaskFile myTaskFile;
99
100     public ClearPlaceholders(TaskFile taskFile, List<AnswerPlaceholder> placeholders, Editor editor) {
101       super(editor.getDocument());
102       myTaskFile = taskFile;
103       myPlaceholders = placeholders;
104       myEditor = editor;
105     }
106
107     @Override
108     public void undo() throws UnexpectedUndoException {
109       myTaskFile.getAnswerPlaceholders().addAll(myPlaceholders);
110       updateView(myEditor, myTaskFile);
111     }
112
113     @Override
114     public void redo() throws UnexpectedUndoException {
115       myTaskFile.getAnswerPlaceholders().clear();
116       updateView(myEditor, myTaskFile);
117     }
118
119     @Override
120     public boolean isGlobal() {
121       return true;
122     }
123   }
124 }