reorganize answer placeholder actions
[idea/community.git] / python / educational-core / course-creator / src / com / jetbrains / edu / coursecreator / actions / placeholder / CCActivatePlaceholder.java
1 package com.jetbrains.edu.coursecreator.actions.placeholder;
2
3 import com.intellij.openapi.util.TextRange;
4 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
5 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholderSubtaskInfo;
6 import com.jetbrains.edu.learning.courseFormat.TaskFile;
7
8 public class CCActivatePlaceholder extends CCSubtaskPlaceholderAction {
9
10   public static final String TITLE = "Activate";
11
12   protected CCActivatePlaceholder() {
13     super(TITLE, TITLE);
14   }
15
16   @Override
17   protected AnswerPlaceholderSubtaskInfo getInfo(CCState state,
18                                                  int subtaskIndex,
19                                                  AnswerPlaceholder existingPlaceholder) {
20     int visibleLength = existingPlaceholder.getVisibleLength(subtaskIndex);
21     int placeholderOffset = existingPlaceholder.getOffset();
22     String possibleAnswer = state.getEditor().getDocument().getText(TextRange.create(placeholderOffset, placeholderOffset + visibleLength));
23     AnswerPlaceholderSubtaskInfo info = new AnswerPlaceholderSubtaskInfo();
24     info.setPossibleAnswer(possibleAnswer);
25     return info;
26   }
27
28   @Override
29   protected String getTitle() {
30     return TITLE;
31   }
32
33   @Override
34   protected void redoAction(AnswerPlaceholder existingPlaceholder, int subtaskIndex, AnswerPlaceholderSubtaskInfo info) {
35     existingPlaceholder.getSubtaskInfos().put(subtaskIndex, info);
36   }
37
38   @Override
39   protected void undoAction(AnswerPlaceholder existingPlaceholder, int subtaskIndex, AnswerPlaceholderSubtaskInfo info) {
40     existingPlaceholder.getSubtaskInfos().remove(subtaskIndex);
41   }
42
43   @Override
44   protected boolean isAvailable(TaskFile taskFile, int offset) {
45     AnswerPlaceholder existingPlaceholder = taskFile.getAnswerPlaceholder(offset, taskFile.getAnswerPlaceholders());
46     return existingPlaceholder != null && !existingPlaceholder.isActive();
47   }
48 }