Refactoring Inline unnecessary "myHints" field
authorValentina Kiryushkina <valentina.kiryushkina@jetbrains.com>
Fri, 22 Jul 2016 17:01:04 +0000 (20:01 +0300)
committerValentina Kiryushkina <valentina.kiryushkina@jetbrains.com>
Fri, 22 Jul 2016 18:43:00 +0000 (21:43 +0300)
python/educational-core/student/src/com/jetbrains/edu/learning/ui/StudyHint.kt

index 9c23b101fc82bb29f37239c26e22469db01edb32..5b1c5655a165aa56e668535d7530fc3b77f6b44e 100644 (file)
@@ -16,22 +16,14 @@ import com.jetbrains.edu.learning.StudyTaskManager
 import com.jetbrains.edu.learning.StudyUtils
 import com.jetbrains.edu.learning.core.EduNames
 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder
-import java.util.*
 
 class StudyHint(private val myPlaceholder: AnswerPlaceholder?, project: Project) {
   val studyToolWindow: StudyToolWindow
-  private val myHints = LinkedList<String>()
   private var myShownHintNumber = 0
   private var isEditingMode = false
   private val newHintDefaultText = "Edit this hint"
 
   init {
-    if (myPlaceholder == null) {
-      myHints.add(OUR_WARNING_MESSAGE)
-    }
-    else {
-      myHints.addAll(myPlaceholder.hints)
-    }
     val taskManager = StudyTaskManager.getInstance(project)
     if (StudyUtils.hasJavaFx() && taskManager.shouldUseJavaFx()) {
       studyToolWindow = StudyJavaFxToolWindow()
@@ -40,24 +32,32 @@ class StudyHint(private val myPlaceholder: AnswerPlaceholder?, project: Project)
       studyToolWindow = StudySwingToolWindow()
     }
     studyToolWindow.init(project, false)
+    
+    if (myPlaceholder == null) {
+      studyToolWindow.setText(OUR_WARNING_MESSAGE)
+    }
+    
     val course = taskManager.course
     if (course != null) {
       val courseMode = course.courseMode
       val group = DefaultActionGroup()
-      if (courseMode == EduNames.STUDY) {
-        if (myHints.size > 1) {
-          group.addAll(GoBackward(), GoForward())
+      val hints = myPlaceholder?.hints
+      if (hints != null) {
+        if (courseMode == EduNames.STUDY) {
+          if (hints.size > 1) {
+            group.addAll(GoBackward(), GoForward())
+          }
+        }
+        else {
+          group.addAll(GoBackward(), GoForward(), Separator.getInstance(), EditHint(), AddHint(), RemoveHint())
+        }
+        studyToolWindow.setActionToolbar(group)
+        if (!hints.isEmpty()) {
+          studyToolWindow.setText(hints[myShownHintNumber])
+        }
+        else {
+          studyToolWindow.setText("No hints are provided")
         }
-      }
-      else {
-        group.addAll(GoBackward(), GoForward(), Separator.getInstance(), EditHint(), AddHint(), RemoveHint())
-      }
-      studyToolWindow.setActionToolbar(group)
-      if (!myHints.isEmpty()) {
-        studyToolWindow.setText(myHints[myShownHintNumber])
-      }
-      else {
-        studyToolWindow.setText("No hints are provided")
       }
     }
   }
@@ -65,18 +65,18 @@ class StudyHint(private val myPlaceholder: AnswerPlaceholder?, project: Project)
   private inner class GoForward : AnAction(AllIcons.Actions.Forward) {
 
     override fun actionPerformed(e: AnActionEvent) {
-      studyToolWindow.setText(myHints[++myShownHintNumber])
+      studyToolWindow.setText(myPlaceholder!!.hints[++myShownHintNumber])
     }
 
     override fun update(e: AnActionEvent) {
-      e.presentation.isEnabled = !isEditingMode && myShownHintNumber + 1 < myHints.size
+      e.presentation.isEnabled = !isEditingMode && myPlaceholder != null && myShownHintNumber + 1 < myPlaceholder.hints.size
     }
   }
 
   private inner class GoBackward : AnAction(AllIcons.Actions.Back) {
 
     override fun actionPerformed(e: AnActionEvent) {
-      studyToolWindow.setText(myHints[--myShownHintNumber])
+      studyToolWindow.setText(myPlaceholder!!.hints[--myShownHintNumber])
     }
 
     override fun update(e: AnActionEvent) {
@@ -103,7 +103,7 @@ class StudyHint(private val myPlaceholder: AnswerPlaceholder?, project: Project)
       if (state) {
         isEditingMode = true
         val factory = EditorFactory.getInstance()
-        currentDocument = factory.createDocument(myHints[myShownHintNumber])
+        currentDocument = factory.createDocument(myPlaceholder!!.hints[myShownHintNumber])
         WebBrowserManager.getInstance().isShowBrowserHover = false
         if (currentDocument != null) {
           val createdEditor = factory.createEditor(currentDocument as Document, project) as EditorEx
@@ -127,44 +127,40 @@ class StudyHint(private val myPlaceholder: AnswerPlaceholder?, project: Project)
       }
       else {
         isEditingMode = false
-        myHints[myShownHintNumber] = currentDocument!!.text
-        val hints = myPlaceholder!!.hints
-        hints[myShownHintNumber] = currentDocument!!.text
-        studyToolWindow.setText(myHints[myShownHintNumber])
+        myPlaceholder!!.hints[myShownHintNumber] = currentDocument!!.text
+        studyToolWindow.setText(myPlaceholder.hints[myShownHintNumber])
         studyToolWindow.setDefaultTopComponent()
       }
     }
 
     override fun update(e: AnActionEvent) {
-      e.presentation.isEnabled = !myHints.isEmpty() && myPlaceholder != null
+      e.presentation.isEnabled = myPlaceholder?.hints?.isEmpty() == false
     }
   }
 
   private inner class AddHint : AnAction(AllIcons.General.Add) {
 
     override fun actionPerformed(e: AnActionEvent) {
-      myHints.add(newHintDefaultText)
       myPlaceholder!!.hints.add(newHintDefaultText)
       myShownHintNumber++
       studyToolWindow.setText(newHintDefaultText)
     }
 
     override fun update(e: AnActionEvent?) {
-      e?.presentation?.isEnabled = !isEditingMode && !myHints.isEmpty()
+      e?.presentation?.isEnabled = !isEditingMode && myPlaceholder != null && !myPlaceholder.hints.isEmpty()
     }
   }
 
   private inner class RemoveHint : AnAction(AllIcons.Actions.Cancel) {
 
     override fun actionPerformed(e: AnActionEvent) {
-      myHints.removeAt(myShownHintNumber)
       myPlaceholder!!.hints.removeAt(myShownHintNumber)
-      myShownHintNumber = if (myHints.size == 1) 0 else if (myShownHintNumber + 1 < myHints.size) myShownHintNumber + 1 else myShownHintNumber - 1
-      studyToolWindow.setText(myHints[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])
     }
 
     override fun update(e: AnActionEvent) {
-      e.presentation.isEnabled = myHints.size > 1 && !isEditingMode
+      e.presentation.isEnabled = myPlaceholder != null && myPlaceholder.hints.size > 1 && !isEditingMode
     }
   }