From f743e8baaf7ae18def1eaa620ee6bb52da8005d1 Mon Sep 17 00:00:00 2001 From: "Liana.Bakradze" Date: Wed, 26 Oct 2016 13:59:16 +0300 Subject: [PATCH] hide and make visible from prev subtasks actions --- .../resources/META-INF/plugin.xml | 2 + .../actions/CCAddAnswerPlaceholder.java | 3 +- .../CCChangePlaceholderVisibility.java | 75 +++++++++++++++++++ .../CCHidePlaceholderFromPrevSubtasks.java | 27 +++++++ .../actions/CCMakeVisibleForPrevSubtasks.java | 27 +++++++ .../edu/learning/StudyTaskManager.java | 3 + 6 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCChangePlaceholderVisibility.java create mode 100644 python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCHidePlaceholderFromPrevSubtasks.java create mode 100644 python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCMakeVisibleForPrevSubtasks.java diff --git a/python/educational-core/course-creator/resources/META-INF/plugin.xml b/python/educational-core/course-creator/resources/META-INF/plugin.xml index b6c87988b86c..86f0f2b43f27 100644 --- a/python/educational-core/course-creator/resources/META-INF/plugin.xml +++ b/python/educational-core/course-creator/resources/META-INF/plugin.xml @@ -80,6 +80,8 @@ + + diff --git a/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCAddAnswerPlaceholder.java b/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCAddAnswerPlaceholder.java index 564c68abc7a9..7c00126fd105 100644 --- a/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCAddAnswerPlaceholder.java +++ b/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCAddAnswerPlaceholder.java @@ -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 index 000000000000..9c79952e1167 --- /dev/null +++ b/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCChangePlaceholderVisibility.java @@ -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 index 000000000000..d1f27b244268 --- /dev/null +++ b/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCHidePlaceholderFromPrevSubtasks.java @@ -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 index 000000000000..3a838150e283 --- /dev/null +++ b/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCMakeVisibleForPrevSubtasks.java @@ -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(); + } +} diff --git a/python/educational-core/student/src/com/jetbrains/edu/learning/StudyTaskManager.java b/python/educational-core/student/src/com/jetbrains/edu/learning/StudyTaskManager.java index b6d9a71b5241..417814b77e86 100644 --- a/python/educational-core/student/src/com/jetbrains/edu/learning/StudyTaskManager.java +++ b/python/educational-core/student/src/com/jetbrains/edu/learning/StudyTaskManager.java @@ -97,6 +97,9 @@ public class StudyTaskManager implements PersistentStateComponent, 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; -- 2.23.3