reorganize answer placeholder actions
[idea/community.git] / python / educational-core / course-creator / src / com / jetbrains / edu / coursecreator / actions / placeholder / CCDeleteAnswerPlaceholder.java
1 package com.jetbrains.edu.coursecreator.actions.placeholder;
2
3 import com.intellij.openapi.actionSystem.AnActionEvent;
4 import com.intellij.openapi.actionSystem.Presentation;
5 import com.intellij.openapi.command.undo.UnexpectedUndoException;
6 import com.intellij.openapi.project.Project;
7 import com.jetbrains.edu.learning.core.EduUtils;
8 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
9 import com.jetbrains.edu.learning.courseFormat.TaskFile;
10 import org.jetbrains.annotations.NotNull;
11
12 public class CCDeleteAnswerPlaceholder extends CCAnswerPlaceholderAction {
13   public CCDeleteAnswerPlaceholder() {
14     super("Delete", "Delete answer placeholder");
15   }
16
17   @Override
18   protected void performAnswerPlaceholderAction(@NotNull CCState state) {
19     deletePlaceholder(state);
20   }
21
22   private static void deletePlaceholder(@NotNull CCState state) {
23     Project project = state.getProject();
24     TaskFile taskFile = state.getTaskFile();
25     AnswerPlaceholder answerPlaceholder = state.getAnswerPlaceholder();
26     EduUtils.runUndoableAction(project, "Delete Answer Placeholder", new CCAddAnswerPlaceholder.AddAction(answerPlaceholder, taskFile, state.getEditor()) {
27       @Override
28       public void undo() throws UnexpectedUndoException {
29         super.redo();
30       }
31
32       @Override
33       public void redo() throws UnexpectedUndoException {
34         super.undo();
35       }
36     });
37   }
38
39
40
41   private static boolean canDeletePlaceholder(@NotNull CCState state) {
42     if (state.getEditor().getSelectionModel().hasSelection()) {
43       return false;
44     }
45     return state.getAnswerPlaceholder() != null;
46   }
47
48   @Override
49   public void update(@NotNull AnActionEvent event) {
50     final Presentation presentation = event.getPresentation();
51     presentation.setEnabledAndVisible(false);
52
53     CCState state = getState(event);
54     if (state == null) {
55       return;
56     }
57
58     if (canDeletePlaceholder(state)) {
59       presentation.setEnabledAndVisible(true);
60     }
61   }
62
63 }