EDU-363 Additional "I give up" option
authorLiana Bakradze <liana.bakradze@jetbrains.com>
Fri, 14 Aug 2015 12:32:18 +0000 (15:32 +0300)
committerLiana Bakradze <liana.bakradze@jetbrains.com>
Fri, 14 Aug 2015 12:32:18 +0000 (15:32 +0300)
python/educational/interactive-learning/resources/META-INF/plugin.xml
python/educational/interactive-learning/src/com/jetbrains/edu/learning/actions/StudyShowAnswersAction.java [new file with mode: 0644]

index d657d2f945166403c794cffbee29298d72d9d546..52dbecb84bda9f32fc37ea51ead683e3328a01be 100644 (file)
             description="Show hint for answer placeholder"/>
     <action id="StudyRunAction" class="com.jetbrains.edu.learning.actions.StudyRunAction" text="Run File With Tests"
             description="Run your code with tests"/>
+    <action id="StudyShowAnswersAction" class="com.jetbrains.edu.learning.actions.StudyShowAnswersAction"
+            text="Fill Answer Placeholders">
+      <add-to-group group-id="HelpMenu" anchor="first"/>
+    </action>
     <action overrides="true"  id="RenameElement" class="com.jetbrains.edu.learning.actions.StudyRenameTaskFileAction"
       text="Rename..."
     />
diff --git a/python/educational/interactive-learning/src/com/jetbrains/edu/learning/actions/StudyShowAnswersAction.java b/python/educational/interactive-learning/src/com/jetbrains/edu/learning/actions/StudyShowAnswersAction.java
new file mode 100644 (file)
index 0000000..fb97023
--- /dev/null
@@ -0,0 +1,61 @@
+package com.jetbrains.edu.learning.actions;
+
+import com.intellij.openapi.actionSystem.AnAction;
+import com.intellij.openapi.actionSystem.AnActionEvent;
+import com.intellij.openapi.application.ApplicationManager;
+import com.intellij.openapi.command.CommandProcessor;
+import com.intellij.openapi.editor.Document;
+import com.intellij.openapi.project.Project;
+import com.jetbrains.edu.courseFormat.AnswerPlaceholder;
+import com.jetbrains.edu.courseFormat.TaskFile;
+import com.jetbrains.edu.learning.StudyState;
+import com.jetbrains.edu.learning.StudyUtils;
+import com.jetbrains.edu.learning.editor.StudyEditor;
+
+public class StudyShowAnswersAction extends AnAction {
+  @Override
+  public void actionPerformed(AnActionEvent e) {
+    final Project project = e.getProject();
+    if (project != null) {
+      StudyEditor studyEditor = StudyUtils.getSelectedStudyEditor(project);
+      StudyState studyState = new StudyState(studyEditor);
+      if (!studyState.isValid()) {
+        return;
+      }
+      final TaskFile taskFile = studyState.getTaskFile();
+      final Document document = studyState.getEditor().getDocument();
+      CommandProcessor.getInstance().runUndoTransparentAction(new Runnable() {
+        @Override
+        public void run() {
+          ApplicationManager.getApplication().runWriteAction(new Runnable() {
+            @Override
+            public void run() {
+              for (AnswerPlaceholder placeholder : taskFile.getAnswerPlaceholders()) {
+                String answer = placeholder.getPossibleAnswer();
+                if (answer == null) {
+                  continue;
+                }
+                int offset = placeholder.getRealStartOffset(document);
+                document.deleteString(offset, offset + placeholder.getLength());
+                document.insertString(offset, answer);
+              }
+            }
+          });
+        }
+      });
+    }
+  }
+
+  @Override
+  public void update(AnActionEvent e) {
+    StudyUtils.updateAction(e);
+    final Project project = e.getProject();
+    if (project != null) {
+      StudyEditor studyEditor = StudyUtils.getSelectedStudyEditor(project);
+      StudyState studyState = new StudyState(studyEditor);
+      if (!studyState.isValid()) {
+        e.getPresentation().setEnabledAndVisible(false);
+      }
+    }
+  }
+}
\ No newline at end of file