hide and make visible from prev subtasks actions
[idea/community.git] / python / educational-core / course-creator / src / com / jetbrains / edu / coursecreator / actions / CCChangePlaceholderVisibility.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.jetbrains.edu.learning.StudyUtils;
8 import com.jetbrains.edu.learning.core.EduUtils;
9 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
10 import com.jetbrains.edu.learning.courseFormat.Task;
11 import org.jetbrains.annotations.NotNull;
12 import org.jetbrains.annotations.Nullable;
13
14 import java.util.Collections;
15
16 public abstract class CCChangePlaceholderVisibility extends CCAnswerPlaceholderAction {
17
18   protected CCChangePlaceholderVisibility(@Nullable String text, @Nullable String description) {
19     super(text, description);
20   }
21
22   @Override
23   protected void performAnswerPlaceholderAction(@NotNull CCState state) {
24     AnswerPlaceholder placeholder = state.getAnswerPlaceholder();
25     if (placeholder == null) {
26       return;
27     }
28     EduUtils.runUndoableAction(state.getProject(), getName(), new BasicUndoableAction(state.getEditor().getDocument()) {
29       @Override
30       public void undo() throws UnexpectedUndoException {
31         setVisible(placeholder, isVisible(), state);
32       }
33
34       @Override
35       public void redo() throws UnexpectedUndoException {
36         setVisible(placeholder, !isVisible(), state);
37       }
38     });
39   }
40
41   private void setVisible(AnswerPlaceholder placeholder, boolean visible, CCState state) {
42     placeholder.getActiveSubtaskInfo().setNeedInsertText(visible);
43     int length = isVisible() ? placeholder.getTaskText().length() : 0;
44     placeholder.setLength(length);
45     StudyUtils.drawAllWindows(state.getEditor(), state.getTaskFile());
46   }
47
48   protected abstract String getName();
49
50   protected abstract boolean isVisible();
51
52   @Override
53   public void update(AnActionEvent e) {
54     Presentation presentation = e.getPresentation();
55     presentation.setEnabledAndVisible(false);
56     CCState state = getState(e);
57     if (state == null) {
58       return;
59     }
60     AnswerPlaceholder placeholder = state.getAnswerPlaceholder();
61     if (placeholder == null) {
62       return;
63     }
64     Task task = state.getTaskFile().getTask();
65     if (!task.hasSubtasks()) {
66       return;
67     }
68     Integer minSubtaskIndex = Collections.min(placeholder.getSubtaskInfos().keySet());
69     if (placeholder.isActive() && minSubtaskIndex != 0 && minSubtaskIndex == task.getActiveSubtaskIndex() && isAvailable(placeholder)) {
70       presentation.setEnabledAndVisible(true);
71     }
72   }
73
74   protected abstract boolean isAvailable(AnswerPlaceholder placeholder);
75 }