activate/deactivate placeholders
authorLiana.Bakradze <liana.bakradze@jetbrains.com>
Wed, 26 Oct 2016 11:58:40 +0000 (14:58 +0300)
committerliana.bakradze <liana.bakradze@jetbrains.com>
Thu, 17 Nov 2016 14:08:32 +0000 (17:08 +0300)
python/educational-core/course-creator/resources/META-INF/plugin.xml
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCActivatePlaceholder.java [new file with mode: 0644]
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCAddAnswerPlaceholder.java
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCDeactivatePlaceholder.java [new file with mode: 0644]
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCSubtaskPlaceholderAction.java [new file with mode: 0644]

index 86f0f2b43f27f28519d2006ed8f983c7c9bb1aed..c1151da124fcd39e6f07e8956f1d93549e877158 100644 (file)
@@ -82,6 +82,8 @@
     <group id="AnswerPlaceholderGroup">
       <action id="CC.HideFromPrevSubtasks" class="com.jetbrains.edu.coursecreator.actions.CCHidePlaceholderFromPrevSubtasks"/>
       <action id="CC.MakeVisibleFromPrevSubtasks" class="com.jetbrains.edu.coursecreator.actions.CCMakeVisibleForPrevSubtasks"/>
+      <action id="CC.ActivatePlaceholder" class="com.jetbrains.edu.coursecreator.actions.CCActivatePlaceholder"/>
+      <action id="CC.DeactivatePlaceholder" class="com.jetbrains.edu.coursecreator.actions.CCDeactivatePlaceholder" />
       <action id="EditAnswerPlaceholder" class="com.jetbrains.edu.coursecreator.actions.CCEditAnswerPlaceholder"/>
       <action id="AddTaskWindow" class="com.jetbrains.edu.coursecreator.actions.CCAddAnswerPlaceholder"/>
       <action id="DeleteAllPlaceholders" class="com.jetbrains.edu.coursecreator.actions.CCDeleteAllAnswerPlaceholdersAction"/>
diff --git a/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCActivatePlaceholder.java b/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCActivatePlaceholder.java
new file mode 100644 (file)
index 0000000..814fa42
--- /dev/null
@@ -0,0 +1,48 @@
+package com.jetbrains.edu.coursecreator.actions;
+
+import com.intellij.openapi.util.TextRange;
+import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
+import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholderSubtaskInfo;
+import com.jetbrains.edu.learning.courseFormat.TaskFile;
+
+public class CCActivatePlaceholder extends CCSubtaskPlaceholderAction {
+
+  public static final String TITLE = "Activate Answer Placeholder";
+
+  protected CCActivatePlaceholder() {
+    super(TITLE, TITLE);
+  }
+
+  @Override
+  protected AnswerPlaceholderSubtaskInfo getInfo(CCState state,
+                                                 int subtaskIndex,
+                                                 AnswerPlaceholder existingPlaceholder) {
+    int visibleLength = existingPlaceholder.getVisibleLength(subtaskIndex);
+    int placeholderOffset = existingPlaceholder.getOffset();
+    String possibleAnswer = state.getEditor().getDocument().getText(TextRange.create(placeholderOffset, placeholderOffset + visibleLength));
+    AnswerPlaceholderSubtaskInfo info = new AnswerPlaceholderSubtaskInfo();
+    info.setPossibleAnswer(possibleAnswer);
+    return info;
+  }
+
+  @Override
+  protected String getTitle() {
+    return TITLE;
+  }
+
+  @Override
+  protected void redoAction(AnswerPlaceholder existingPlaceholder, int subtaskIndex, AnswerPlaceholderSubtaskInfo info) {
+    existingPlaceholder.getSubtaskInfos().put(subtaskIndex, info);
+  }
+
+  @Override
+  protected void undoAction(AnswerPlaceholder existingPlaceholder, int subtaskIndex, AnswerPlaceholderSubtaskInfo info) {
+    existingPlaceholder.getSubtaskInfos().remove(subtaskIndex);
+  }
+
+  @Override
+  protected boolean isAvailable(TaskFile taskFile, int offset) {
+    AnswerPlaceholder existingPlaceholder = taskFile.getAnswerPlaceholder(offset, taskFile.getAnswerPlaceholders());
+    return existingPlaceholder != null && !existingPlaceholder.isActive();
+  }
+}
index 7c00126fd105879dbc5b09376f022a08bc20c2f4..99d05c03477a2ba3c0f7d1d74dd6835657b11a50 100644 (file)
@@ -9,10 +9,7 @@ import com.intellij.openapi.editor.Editor;
 import com.intellij.openapi.editor.SelectionModel;
 import com.intellij.openapi.fileEditor.FileDocumentManager;
 import com.intellij.openapi.project.Project;
-import com.intellij.openapi.util.TextRange;
 import com.intellij.openapi.util.text.StringUtil;
-import com.intellij.psi.PsiDocumentManager;
-import com.intellij.psi.PsiFile;
 import com.intellij.ui.JBColor;
 import com.intellij.util.DocumentUtil;
 import com.jetbrains.edu.learning.StudyUtils;
@@ -50,32 +47,15 @@ public class CCAddAnswerPlaceholder extends CCAnswerPlaceholderAction {
   private void addPlaceholder(@NotNull CCState state) {
     Editor editor = state.getEditor();
     Project project = state.getProject();
-    PsiFile file = state.getFile();
-
-    final Document document = PsiDocumentManager.getInstance(project).getDocument(file);
-    if (document == null) {
-      return;
-    }
+    Document document = editor.getDocument();
     FileDocumentManager.getInstance().saveDocument(document);
     final SelectionModel model = editor.getSelectionModel();
     final int offset = model.hasSelection() ? model.getSelectionStart() : editor.getCaretModel().getOffset();
     TaskFile taskFile = state.getTaskFile();
-    int stepIndex = state.getTaskFile().getTask().getActiveSubtaskIndex();
-
-    AnswerPlaceholder existingPlaceholder = taskFile.getAnswerPlaceholder(offset, taskFile.getAnswerPlaceholders());
-    if (existingPlaceholder != null) {
-      int visibleLength = existingPlaceholder.getVisibleLength(stepIndex);
-      int placeholderOffset = existingPlaceholder.getOffset();
-      String possibleAnswer = document.getText(TextRange.create(placeholderOffset, placeholderOffset + visibleLength));
-      AnswerPlaceholderSubtaskInfo info = new AnswerPlaceholderSubtaskInfo();
-      existingPlaceholder.getSubtaskInfos().put(stepIndex, info);
-      info.setPossibleAnswer(possibleAnswer);
-      StudyUtils.drawAllWindows(editor, taskFile);
-      return;
-    }
+    int subtaskIndex = state.getTaskFile().getTask().getActiveSubtaskIndex();
     final AnswerPlaceholder answerPlaceholder = new AnswerPlaceholder();
     AnswerPlaceholderSubtaskInfo info = new AnswerPlaceholderSubtaskInfo();
-    answerPlaceholder.getSubtaskInfos().put(stepIndex, info);
+    answerPlaceholder.getSubtaskInfos().put(subtaskIndex, info);
     int index = taskFile.getAnswerPlaceholders().size();
     answerPlaceholder.setIndex(index);
     answerPlaceholder.setTaskFile(taskFile);
@@ -183,13 +163,14 @@ public class CCAddAnswerPlaceholder extends CCAnswerPlaceholderAction {
   private static boolean canAddPlaceholder(@NotNull CCState state) {
     Editor editor = state.getEditor();
     SelectionModel selectionModel = editor.getSelectionModel();
+    TaskFile taskFile = state.getTaskFile();
     if (selectionModel.hasSelection()) {
       int start = selectionModel.getSelectionStart();
       int end = selectionModel.getSelectionEnd();
-      return !arePlaceholdersIntersect(state.getTaskFile(), start, end);
+      return !arePlaceholdersIntersect(taskFile, start, end);
     }
     int offset = editor.getCaretModel().getOffset();
-    return state.getTaskFile().getAnswerPlaceholder(offset) == null;
+    return taskFile.getAnswerPlaceholder(offset, taskFile.getAnswerPlaceholders()) == null;
   }
 
   private static boolean canDeletePlaceholder(@NotNull CCState state) {
diff --git a/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCDeactivatePlaceholder.java b/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCDeactivatePlaceholder.java
new file mode 100644 (file)
index 0000000..85e3e60
--- /dev/null
@@ -0,0 +1,44 @@
+package com.jetbrains.edu.coursecreator.actions;
+
+import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
+import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholderSubtaskInfo;
+import com.jetbrains.edu.learning.courseFormat.TaskFile;
+
+import java.util.Collections;
+
+public class CCDeactivatePlaceholder extends CCSubtaskPlaceholderAction {
+
+  public static final String TITLE = "Deactivate Answer Placeholder";
+
+  protected CCDeactivatePlaceholder() {
+    super(TITLE, TITLE);
+  }
+
+  protected void undoAction(AnswerPlaceholder existingPlaceholder, int subtaskIndex, AnswerPlaceholderSubtaskInfo info) {
+    existingPlaceholder.getSubtaskInfos().put(subtaskIndex, info);
+  }
+
+  @Override
+  protected AnswerPlaceholderSubtaskInfo getInfo(CCState state,
+                                                 int subtaskIndex,
+                                                 AnswerPlaceholder existingPlaceholder) {
+    return existingPlaceholder.getSubtaskInfos().get(subtaskIndex);
+  }
+
+  @Override
+  protected String getTitle() {
+    return TITLE;
+  }
+
+  protected void redoAction(AnswerPlaceholder existingPlaceholder, int subtaskIndex, AnswerPlaceholderSubtaskInfo info) {
+    existingPlaceholder.getSubtaskInfos().remove(subtaskIndex);
+  }
+
+  protected boolean isAvailable(TaskFile taskFile, int offset) {
+    AnswerPlaceholder existingActivePlaceholder = taskFile.getAnswerPlaceholder(offset);
+    if (existingActivePlaceholder == null) {
+      return false;
+    }
+    return Collections.min(existingActivePlaceholder.getSubtaskInfos().keySet()) < taskFile.getTask().getActiveSubtaskIndex();
+  }
+}
diff --git a/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCSubtaskPlaceholderAction.java b/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCSubtaskPlaceholderAction.java
new file mode 100644 (file)
index 0000000..0b06175
--- /dev/null
@@ -0,0 +1,77 @@
+package com.jetbrains.edu.coursecreator.actions;
+
+import com.intellij.openapi.actionSystem.AnActionEvent;
+import com.intellij.openapi.actionSystem.Presentation;
+import com.intellij.openapi.command.undo.BasicUndoableAction;
+import com.intellij.openapi.command.undo.UnexpectedUndoException;
+import com.intellij.openapi.editor.Editor;
+import com.jetbrains.edu.learning.StudyUtils;
+import com.jetbrains.edu.learning.core.EduUtils;
+import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
+import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholderSubtaskInfo;
+import com.jetbrains.edu.learning.courseFormat.TaskFile;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+public abstract class CCSubtaskPlaceholderAction extends CCAnswerPlaceholderAction {
+  protected CCSubtaskPlaceholderAction(@Nullable String text, @Nullable String description) {
+    super(text, description);
+  }
+
+  @Override
+  protected void performAnswerPlaceholderAction(@NotNull CCState state) {
+    Editor editor = state.getEditor();
+    final int offset = editor.getCaretModel().getOffset();
+    TaskFile taskFile = state.getTaskFile();
+    int subtaskIndex = state.getTaskFile().getTask().getActiveSubtaskIndex();
+    AnswerPlaceholder existingPlaceholder = taskFile.getAnswerPlaceholder(offset, taskFile.getAnswerPlaceholders());
+    if (existingPlaceholder == null) {
+      return;
+    }
+    AnswerPlaceholderSubtaskInfo info = getInfo(state, subtaskIndex, existingPlaceholder);
+    if (info == null) {
+      return;
+    }
+    EduUtils.runUndoableAction(state.getProject(), getTitle(), new BasicUndoableAction(state.getEditor().getDocument()) {
+      @Override
+      public void undo() throws UnexpectedUndoException {
+        undoAction(existingPlaceholder, subtaskIndex, info);
+        StudyUtils.drawAllWindows(editor, taskFile);
+      }
+
+      @Override
+      public void redo() throws UnexpectedUndoException {
+        redoAction(existingPlaceholder, subtaskIndex, info);
+        StudyUtils.drawAllWindows(editor, taskFile);
+      }
+    });
+  }
+
+  protected abstract AnswerPlaceholderSubtaskInfo getInfo(CCState state, int subtaskIndex, AnswerPlaceholder existingPlaceholder);
+
+  protected abstract String getTitle();
+
+  protected abstract void redoAction(AnswerPlaceholder existingPlaceholder, int subtaskIndex, AnswerPlaceholderSubtaskInfo info);
+
+  protected abstract void undoAction(AnswerPlaceholder existingPlaceholder, int subtaskIndex, AnswerPlaceholderSubtaskInfo info);
+
+  @Override
+  public void update(AnActionEvent e) {
+    Presentation presentation = e.getPresentation();
+    presentation.setEnabledAndVisible(false);
+    CCState state = getState(e);
+    if (state == null) {
+      return;
+    }
+    TaskFile taskFile = state.getTaskFile();
+    if (!taskFile.getTask().hasSubtasks()) {
+      return;
+    }
+    int offset = state.getEditor().getCaretModel().getOffset();
+    if (isAvailable(taskFile, offset)) {
+      presentation.setEnabledAndVisible(true);
+    }
+  }
+
+  protected abstract boolean isAvailable(TaskFile taskFile, int offset);
+}