extracted placeholder actions into seperate package
[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.EduNames;
20 import com.jetbrains.edu.learning.core.EduUtils;
21 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
22 import com.jetbrains.edu.learning.courseFormat.TaskFile;
23 import org.jetbrains.annotations.NotNull;
24
25 import java.util.ArrayList;
26 import java.util.List;
27
28 public class CCDeleteAllAnswerPlaceholdersAction extends DumbAwareAction {
29
30   public static final String ACTION_NAME = "Delete All " + EduNames.ANSWER_PLACEHOLDER + "s";
31
32   public CCDeleteAllAnswerPlaceholdersAction() {
33     super(ACTION_NAME);
34   }
35
36   @Override
37   public void actionPerformed(AnActionEvent e) {
38     final DataContext context = e.getDataContext();
39     final VirtualFile file = CommonDataKeys.VIRTUAL_FILE.getData(context);
40     final Project project = e.getProject();
41     if (file == null || project == null) {
42       return;
43     }
44     final TaskFile taskFile = StudyUtils.getTaskFile(project, file);
45     if (taskFile == null) {
46       return;
47     }
48     Editor editor = CommonDataKeys.EDITOR.getData(context);
49     if (editor == null) {
50       FileEditorManager instance = FileEditorManager.getInstance(project);
51       if (!instance.isFileOpen(file)) {
52         return;
53       }
54       FileEditor fileEditor = instance.getSelectedEditor(file);
55       if (!(fileEditor instanceof TextEditor)) {
56         return;
57       }
58       editor = ((TextEditor)fileEditor).getEditor();
59     }
60     List<AnswerPlaceholder> placeholders = new ArrayList<>(taskFile.getAnswerPlaceholders());
61     final ClearPlaceholders action = new ClearPlaceholders(taskFile, placeholders, editor);
62     EduUtils.runUndoableAction(project, ACTION_NAME, action, UndoConfirmationPolicy.REQUEST_CONFIRMATION);
63   }
64
65   private static void updateView(@NotNull final Editor editor,
66                                  @NotNull final TaskFile taskFile) {
67     editor.getMarkupModel().removeAllHighlighters();
68     StudyUtils.drawAllWindows(editor, taskFile);
69   }
70
71   @Override
72   public void update(AnActionEvent e) {
73     Presentation presentation = e.getPresentation();
74     presentation.setEnabledAndVisible(false);
75
76     Project project = e.getProject();
77     if (project == null) {
78       return;
79     }
80     if (!CCUtils.isCourseCreator(project)) {
81       return;
82     }
83     DataContext context = e.getDataContext();
84     VirtualFile file = CommonDataKeys.VIRTUAL_FILE.getData(context);
85     if (file == null ) {
86       return;
87     }
88     TaskFile taskFile = StudyUtils.getTaskFile(project, file);
89     if (taskFile == null || taskFile.getAnswerPlaceholders().isEmpty()) {
90       return;
91     }
92     presentation.setEnabledAndVisible(true);
93   }
94
95
96   private static class ClearPlaceholders extends BasicUndoableAction {
97     private final List<AnswerPlaceholder> myPlaceholders;
98     private final Editor myEditor;
99     private final TaskFile myTaskFile;
100
101     public ClearPlaceholders(TaskFile taskFile, List<AnswerPlaceholder> placeholders, Editor editor) {
102       super(editor.getDocument());
103       myTaskFile = taskFile;
104       myPlaceholders = placeholders;
105       myEditor = editor;
106     }
107
108     @Override
109     public void undo() throws UnexpectedUndoException {
110       myTaskFile.getAnswerPlaceholders().addAll(myPlaceholders);
111       updateView(myEditor, myTaskFile);
112     }
113
114     @Override
115     public void redo() throws UnexpectedUndoException {
116       myTaskFile.getAnswerPlaceholders().clear();
117       updateView(myEditor, myTaskFile);
118     }
119
120     @Override
121     public boolean isGlobal() {
122       return true;
123     }
124   }
125 }