activate/deactivate placeholders
[idea/community.git] / python / educational-core / course-creator / src / com / jetbrains / edu / coursecreator / actions / CCSubtaskPlaceholderAction.java
1 package com.jetbrains.edu.coursecreator.actions;
2
3 import com.intellij.openapi.actionSystem.AnActionEvent;
4 import com.intellij.openapi.actionSystem.Presentation;
5 import com.intellij.openapi.command.undo.BasicUndoableAction;
6 import com.intellij.openapi.command.undo.UnexpectedUndoException;
7 import com.intellij.openapi.editor.Editor;
8 import com.jetbrains.edu.learning.StudyUtils;
9 import com.jetbrains.edu.learning.core.EduUtils;
10 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
11 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholderSubtaskInfo;
12 import com.jetbrains.edu.learning.courseFormat.TaskFile;
13 import org.jetbrains.annotations.NotNull;
14 import org.jetbrains.annotations.Nullable;
15
16 public abstract class CCSubtaskPlaceholderAction extends CCAnswerPlaceholderAction {
17   protected CCSubtaskPlaceholderAction(@Nullable String text, @Nullable String description) {
18     super(text, description);
19   }
20
21   @Override
22   protected void performAnswerPlaceholderAction(@NotNull CCState state) {
23     Editor editor = state.getEditor();
24     final int offset = editor.getCaretModel().getOffset();
25     TaskFile taskFile = state.getTaskFile();
26     int subtaskIndex = state.getTaskFile().getTask().getActiveSubtaskIndex();
27     AnswerPlaceholder existingPlaceholder = taskFile.getAnswerPlaceholder(offset, taskFile.getAnswerPlaceholders());
28     if (existingPlaceholder == null) {
29       return;
30     }
31     AnswerPlaceholderSubtaskInfo info = getInfo(state, subtaskIndex, existingPlaceholder);
32     if (info == null) {
33       return;
34     }
35     EduUtils.runUndoableAction(state.getProject(), getTitle(), new BasicUndoableAction(state.getEditor().getDocument()) {
36       @Override
37       public void undo() throws UnexpectedUndoException {
38         undoAction(existingPlaceholder, subtaskIndex, info);
39         StudyUtils.drawAllWindows(editor, taskFile);
40       }
41
42       @Override
43       public void redo() throws UnexpectedUndoException {
44         redoAction(existingPlaceholder, subtaskIndex, info);
45         StudyUtils.drawAllWindows(editor, taskFile);
46       }
47     });
48   }
49
50   protected abstract AnswerPlaceholderSubtaskInfo getInfo(CCState state, int subtaskIndex, AnswerPlaceholder existingPlaceholder);
51
52   protected abstract String getTitle();
53
54   protected abstract void redoAction(AnswerPlaceholder existingPlaceholder, int subtaskIndex, AnswerPlaceholderSubtaskInfo info);
55
56   protected abstract void undoAction(AnswerPlaceholder existingPlaceholder, int subtaskIndex, AnswerPlaceholderSubtaskInfo info);
57
58   @Override
59   public void update(AnActionEvent e) {
60     Presentation presentation = e.getPresentation();
61     presentation.setEnabledAndVisible(false);
62     CCState state = getState(e);
63     if (state == null) {
64       return;
65     }
66     TaskFile taskFile = state.getTaskFile();
67     if (!taskFile.getTask().hasSubtasks()) {
68       return;
69     }
70     int offset = state.getEditor().getCaretModel().getOffset();
71     if (isAvailable(taskFile, offset)) {
72       presentation.setEnabledAndVisible(true);
73     }
74   }
75
76   protected abstract boolean isAvailable(TaskFile taskFile, int offset);
77 }