Fix EDU-726 Adding a hint in course creation mode throws an exception
authorValentina Kiryushkina <valentina.kiryushkina@jetbrains.com>
Mon, 8 Aug 2016 15:05:30 +0000 (18:05 +0300)
committerValentina Kiryushkina <valentina.kiryushkina@jetbrains.com>
Mon, 8 Aug 2016 16:13:57 +0000 (19:13 +0300)
python/educational-core/student/src/com/jetbrains/edu/learning/courseFormat/AnswerPlaceholder.java
python/educational-core/student/src/com/jetbrains/edu/learning/ui/StudyHint.kt

index 22ed1af65e4e3d80f06b9659ce9a4cdd9c637348..c3975ba5524a276ec8cd024de9df4ab5e911c6f5 100644 (file)
@@ -2,6 +2,7 @@ package com.jetbrains.edu.learning.courseFormat;
 
 import com.google.gson.annotations.Expose;
 import com.google.gson.annotations.SerializedName;
+import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.util.xmlb.annotations.Transient;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
@@ -15,6 +16,7 @@ import java.util.List;
  */
 
 public class AnswerPlaceholder {
+  private static final Logger LOG = Logger.getInstance(AnswerPlaceholder.class);
   
   @SerializedName("hint")
   @Expose private String myHint = "";
@@ -224,7 +226,12 @@ public class AnswerPlaceholder {
       myHint = "";
     }
     else {
-      myAdditionalHints.remove(i);
+      if (i - 1 <myAdditionalHints.size()) {
+        myAdditionalHints.remove(i - 1);
+      }
+      else {
+        LOG.warn("Trying to remove nonexistent hint. Hint to remove number: " + (i - 1) + "number of hints: " + getHints().size());
+      }
     }
   }
 
index cc86dd51596918e4d4b3ac261b7881d6eb8bc568..9dd7d85dbd7dacce0a5ee7f470c2ca87e2055693 100644 (file)
@@ -58,16 +58,21 @@ class StudyHint(private val myPlaceholder: AnswerPlaceholder?, project: Project)
           group.addAll(GoBackward(), GoForward(), Separator.getInstance(), EditHint(), AddHint(), RemoveHint())
         }
         studyToolWindow.setActionToolbar(group)
-        if (!hints.isEmpty()) {
-          studyToolWindow.setText(hints[myShownHintNumber])
-        }
-        else {
-          studyToolWindow.setText(HINTS_NOT_AVAILABLE)
-        }
+        setHintText(hints)
       }
     }
   }
 
+  private fun setHintText(hints: List<String>) {
+    if (!hints.isEmpty()) {
+      studyToolWindow.setText(hints[myShownHintNumber])
+    }
+    else {
+      myShownHintNumber = -1
+      studyToolWindow.setText(HINTS_NOT_AVAILABLE)
+    }
+  }
+
   private inner class GoForward : AnAction("Next Hint", "Next Hint", AllIcons.Actions.Forward) {
 
 
@@ -110,7 +115,8 @@ class StudyHint(private val myPlaceholder: AnswerPlaceholder?, project: Project)
       if (state) {
         isEditingMode = true
         val factory = EditorFactory.getInstance()
-        currentDocument = factory.createDocument(myPlaceholder!!.hints[myShownHintNumber])
+        val initialText = if (myShownHintNumber < myPlaceholder!!.hints.size) myPlaceholder.hints[myShownHintNumber] else ""
+        currentDocument = factory.createDocument(initialText)
         WebBrowserManager.getInstance().isShowBrowserHover = false
         if (currentDocument != null) {
           val createdEditor = factory.createEditor(currentDocument as Document, project) as EditorEx
@@ -134,8 +140,13 @@ class StudyHint(private val myPlaceholder: AnswerPlaceholder?, project: Project)
       }
       else {
         isEditingMode = false
-        myPlaceholder!!.setHintByIndex(myShownHintNumber, currentDocument!!.text)
-        studyToolWindow.setText(myPlaceholder.hints[myShownHintNumber])
+        if (myShownHintNumber < myPlaceholder!!.hints.size) {
+          myPlaceholder.setHintByIndex(myShownHintNumber, currentDocument!!.text)
+        }
+        else {
+          myPlaceholder.addHint(currentDocument!!.text)
+        }
+        setHintText(myPlaceholder.hints)
         studyToolWindow.setDefaultTopComponent()
       }
     }
@@ -154,7 +165,7 @@ class StudyHint(private val myPlaceholder: AnswerPlaceholder?, project: Project)
     }
 
     override fun update(e: AnActionEvent?) {
-      e?.presentation?.isEnabled = !isEditingMode && myPlaceholder != null && !myPlaceholder.hints.isEmpty()
+      e?.presentation?.isEnabled = !isEditingMode && myPlaceholder != null
     }
   }
 
@@ -162,12 +173,13 @@ class StudyHint(private val myPlaceholder: AnswerPlaceholder?, project: Project)
 
     override fun actionPerformed(e: AnActionEvent) {
       myPlaceholder!!.removeHint(myShownHintNumber)
-      myShownHintNumber = if (myPlaceholder.hints.size == 1) 0 else if (myShownHintNumber + 1 < myPlaceholder.hints.size) myShownHintNumber + 1 else myShownHintNumber - 1
-      studyToolWindow.setText(myPlaceholder.hints[myShownHintNumber])
+      myShownHintNumber += if (myShownHintNumber < myPlaceholder.hints.size) 0 else -1
+      
+      setHintText(myPlaceholder.hints)
     }
 
     override fun update(e: AnActionEvent) {
-      e.presentation.isEnabled = myPlaceholder != null && myPlaceholder.hints.size > 1 && !isEditingMode
+      e.presentation.isEnabled = myPlaceholder != null && myPlaceholder.hints.size > 0 && !isEditingMode
     }
   }
 }