hide and make visible from prev subtasks actions
authorLiana.Bakradze <liana.bakradze@jetbrains.com>
Wed, 26 Oct 2016 10:59:16 +0000 (13:59 +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/CCAddAnswerPlaceholder.java
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCChangePlaceholderVisibility.java [new file with mode: 0644]
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCHidePlaceholderFromPrevSubtasks.java [new file with mode: 0644]
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCMakeVisibleForPrevSubtasks.java [new file with mode: 0644]
python/educational-core/student/src/com/jetbrains/edu/learning/StudyTaskManager.java

index b6c87988b86cef985bb1949c2de460e4ab85a2cb..86f0f2b43f27f28519d2006ed8f983c7c9bb1aed 100644 (file)
@@ -80,6 +80,8 @@
     </group>
 
     <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="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"/>
index 564c68abc7a9ac8a0276a16e1bceab4546bebca2..7c00126fd105879dbc5b09376f022a08bc20c2f4 100644 (file)
@@ -75,7 +75,6 @@ public class CCAddAnswerPlaceholder extends CCAnswerPlaceholderAction {
     }
     final AnswerPlaceholder answerPlaceholder = new AnswerPlaceholder();
     AnswerPlaceholderSubtaskInfo info = new AnswerPlaceholderSubtaskInfo();
-    info.setNeedInsertText(!model.hasSelection());
     answerPlaceholder.getSubtaskInfos().put(stepIndex, info);
     int index = taskFile.getAnswerPlaceholders().size();
     answerPlaceholder.setIndex(index);
@@ -92,7 +91,7 @@ public class CCAddAnswerPlaceholder extends CCAnswerPlaceholderAction {
     String answerPlaceholderText = dlg.getTaskText();
     answerPlaceholder.setPossibleAnswer(model.hasSelection() ? model.getSelectedText() : defaultPlaceholderText);
     answerPlaceholder.setTaskText(StringUtil.notNullize(answerPlaceholderText));
-    answerPlaceholder.setLength(model.hasSelection() ? StringUtil.notNullize(answerPlaceholderText).length() : 0);
+    answerPlaceholder.setLength(StringUtil.notNullize(answerPlaceholderText).length());
     answerPlaceholder.setHints(dlg.getHints());
 
     if (!model.hasSelection()) {
diff --git a/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCChangePlaceholderVisibility.java b/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCChangePlaceholderVisibility.java
new file mode 100644 (file)
index 0000000..9c79952
--- /dev/null
@@ -0,0 +1,75 @@
+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.jetbrains.edu.learning.StudyUtils;
+import com.jetbrains.edu.learning.core.EduUtils;
+import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
+import com.jetbrains.edu.learning.courseFormat.Task;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import java.util.Collections;
+
+public abstract class CCChangePlaceholderVisibility extends CCAnswerPlaceholderAction {
+
+  protected CCChangePlaceholderVisibility(@Nullable String text, @Nullable String description) {
+    super(text, description);
+  }
+
+  @Override
+  protected void performAnswerPlaceholderAction(@NotNull CCState state) {
+    AnswerPlaceholder placeholder = state.getAnswerPlaceholder();
+    if (placeholder == null) {
+      return;
+    }
+    EduUtils.runUndoableAction(state.getProject(), getName(), new BasicUndoableAction(state.getEditor().getDocument()) {
+      @Override
+      public void undo() throws UnexpectedUndoException {
+        setVisible(placeholder, isVisible(), state);
+      }
+
+      @Override
+      public void redo() throws UnexpectedUndoException {
+        setVisible(placeholder, !isVisible(), state);
+      }
+    });
+  }
+
+  private void setVisible(AnswerPlaceholder placeholder, boolean visible, CCState state) {
+    placeholder.getActiveSubtaskInfo().setNeedInsertText(visible);
+    int length = isVisible() ? placeholder.getTaskText().length() : 0;
+    placeholder.setLength(length);
+    StudyUtils.drawAllWindows(state.getEditor(), state.getTaskFile());
+  }
+
+  protected abstract String getName();
+
+  protected abstract boolean isVisible();
+
+  @Override
+  public void update(AnActionEvent e) {
+    Presentation presentation = e.getPresentation();
+    presentation.setEnabledAndVisible(false);
+    CCState state = getState(e);
+    if (state == null) {
+      return;
+    }
+    AnswerPlaceholder placeholder = state.getAnswerPlaceholder();
+    if (placeholder == null) {
+      return;
+    }
+    Task task = state.getTaskFile().getTask();
+    if (!task.hasSubtasks()) {
+      return;
+    }
+    Integer minSubtaskIndex = Collections.min(placeholder.getSubtaskInfos().keySet());
+    if (placeholder.isActive() && minSubtaskIndex != 0 && minSubtaskIndex == task.getActiveSubtaskIndex() && isAvailable(placeholder)) {
+      presentation.setEnabledAndVisible(true);
+    }
+  }
+
+  protected abstract boolean isAvailable(AnswerPlaceholder placeholder);
+}
diff --git a/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCHidePlaceholderFromPrevSubtasks.java b/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCHidePlaceholderFromPrevSubtasks.java
new file mode 100644 (file)
index 0000000..d1f27b2
--- /dev/null
@@ -0,0 +1,27 @@
+package com.jetbrains.edu.coursecreator.actions;
+
+import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
+
+public class CCHidePlaceholderFromPrevSubtasks extends CCChangePlaceholderVisibility {
+
+  public static final String TITLE = "Hide for Previous Subtasks";
+
+  public CCHidePlaceholderFromPrevSubtasks() {
+    super(TITLE, TITLE);
+  }
+
+  @Override
+  protected String getName() {
+    return TITLE;
+  }
+
+  @Override
+  protected boolean isVisible() {
+    return false;
+  }
+
+  @Override
+  protected boolean isAvailable(AnswerPlaceholder placeholder) {
+    return !placeholder.getActiveSubtaskInfo().isNeedInsertText();
+  }
+}
diff --git a/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCMakeVisibleForPrevSubtasks.java b/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCMakeVisibleForPrevSubtasks.java
new file mode 100644 (file)
index 0000000..3a83815
--- /dev/null
@@ -0,0 +1,27 @@
+package com.jetbrains.edu.coursecreator.actions;
+
+import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
+
+public class CCMakeVisibleForPrevSubtasks extends CCChangePlaceholderVisibility {
+
+  public static final String TITLE = "Make Visible For Previous Subtasks";
+
+  protected CCMakeVisibleForPrevSubtasks() {
+    super(TITLE, TITLE);
+  }
+
+  @Override
+  protected String getName() {
+    return TITLE;
+  }
+
+  @Override
+  protected boolean isVisible() {
+    return true;
+  }
+
+  @Override
+  protected boolean isAvailable(AnswerPlaceholder placeholder) {
+    return placeholder.getActiveSubtaskInfo().isNeedInsertText();
+  }
+}
index b6d9a71b52415de6ad32fbeda0b6ea71366273d8..417814b77e86f11050ad2836edee4e6aa8bd5a95 100644 (file)
@@ -97,6 +97,9 @@ public class StudyTaskManager implements PersistentStateComponent<Element>, Dumb
   }
 
   public JBColor getColor(@NotNull final AnswerPlaceholder placeholder) {
+    if (!placeholder.getUseLength() && placeholder.isActive() && placeholder.getActiveSubtaskInfo().isNeedInsertText()) {
+      return JBColor.LIGHT_GRAY;
+    }
     final StudyStatus status = placeholder.getStatus();
     if (status == StudyStatus.Solved) {
       return JBColor.GREEN;