isolate UI for better testability
authorliana.bakradze <liana.bakradze@jetbrains.com>
Wed, 17 Aug 2016 14:03:39 +0000 (17:03 +0300)
committerliana.bakradze <liana.bakradze@jetbrains.com>
Wed, 17 Aug 2016 14:03:39 +0000 (17:03 +0300)
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCAddAnswerPlaceholder.java
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCEditAnswerPlaceholder.java
python/educational-core/student/src/com/jetbrains/edu/learning/ui/CCCreateAnswerPlaceholderDialog.java
python/educational-core/student/src/com/jetbrains/edu/learning/ui/CCCreateAnswerPlaceholderPanel.java
python/educational-core/student/src/com/jetbrains/edu/learning/ui/StudyHint.kt

index c6cfa9df1b21e185f7c39e8f9394b026231059d4..b2f26711ac5ad7dce3f629f8833387877b418fe3 100644 (file)
@@ -9,7 +9,7 @@ import com.intellij.openapi.editor.Editor;
 import com.intellij.openapi.editor.SelectionModel;
 import com.intellij.openapi.fileEditor.FileDocumentManager;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.editor.SelectionModel;
 import com.intellij.openapi.fileEditor.FileDocumentManager;
 import com.intellij.openapi.project.Project;
-import com.intellij.openapi.ui.DialogWrapper;
+import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.psi.PsiDocumentManager;
 import com.intellij.psi.PsiFile;
 import com.intellij.ui.JBColor;
 import com.intellij.psi.PsiDocumentManager;
 import com.intellij.psi.PsiFile;
 import com.intellij.ui.JBColor;
@@ -45,7 +45,7 @@ public class CCAddAnswerPlaceholder extends CCAnswerPlaceholderAction {
     return false;
   }
 
     return false;
   }
 
-  private static void addPlaceholder(@NotNull CCState state) {
+  private void addPlaceholder(@NotNull CCState state) {
     Editor editor = state.getEditor();
     Project project = state.getProject();
     PsiFile file = state.getFile();
     Editor editor = state.getEditor();
     Project project = state.getProject();
     PsiFile file = state.getFile();
@@ -65,11 +65,14 @@ public class CCAddAnswerPlaceholder extends CCAnswerPlaceholderAction {
     String defaultPlaceholderText = "type here";
     answerPlaceholder.setPossibleAnswer(model.hasSelection() ? model.getSelectedText() : defaultPlaceholderText);
 
     String defaultPlaceholderText = "type here";
     answerPlaceholder.setPossibleAnswer(model.hasSelection() ? model.getSelectedText() : defaultPlaceholderText);
 
-    CCCreateAnswerPlaceholderDialog dlg = new CCCreateAnswerPlaceholderDialog(project, answerPlaceholder);
-    dlg.show();
-    if (dlg.getExitCode() != DialogWrapper.OK_EXIT_CODE) {
+    CCCreateAnswerPlaceholderDialog dlg = createDialog(project, answerPlaceholder);
+    if (!dlg.showAndGet()) {
       return;
     }
       return;
     }
+    String answerPlaceholderText = dlg.getTaskText();
+    answerPlaceholder.setTaskText(StringUtil.notNullize(answerPlaceholderText));
+    answerPlaceholder.setLength(StringUtil.notNullize(answerPlaceholderText).length());
+    answerPlaceholder.setHints(dlg.getHints());
 
     if (!model.hasSelection()) {
       DocumentUtil.writeInRunUndoTransparentAction(() -> document.insertString(offset, defaultPlaceholderText));
 
     if (!model.hasSelection()) {
       DocumentUtil.writeInRunUndoTransparentAction(() -> document.insertString(offset, defaultPlaceholderText));
@@ -180,4 +183,8 @@ public class CCAddAnswerPlaceholder extends CCAnswerPlaceholderAction {
     }
     return state.getAnswerPlaceholder() != null;
   }
     }
     return state.getAnswerPlaceholder() != null;
   }
+
+  protected CCCreateAnswerPlaceholderDialog createDialog(Project project, AnswerPlaceholder answerPlaceholder) {
+    return new CCCreateAnswerPlaceholderDialog(project, StringUtil.notNullize(answerPlaceholder.getTaskText()), answerPlaceholder.getHints());
+  }
 }
\ No newline at end of file
 }
\ No newline at end of file
index f524329ad4feba6e9cb91af0a3dbd7685af68397..5dfb491d060a7bf8bacd51242959f4fa72a0bad2 100644 (file)
@@ -26,8 +26,7 @@ public class CCEditAnswerPlaceholder extends CCAnswerPlaceholderAction {
     if (answerPlaceholder == null) {
       return;
     }
     if (answerPlaceholder == null) {
       return;
     }
-    CCCreateAnswerPlaceholderDialog dlg = new CCCreateAnswerPlaceholderDialog(project, answerPlaceholder
-    );
+    CCCreateAnswerPlaceholderDialog dlg = new CCCreateAnswerPlaceholderDialog(project, answerPlaceholder.getTaskText(), answerPlaceholder.getHints());
     dlg.setTitle("Edit Answer Placeholder");
     dlg.show();
   }
     dlg.setTitle("Edit Answer Placeholder");
     dlg.show();
   }
index 854ff553784c0cdad11da0b15767c6083e4cce83..1f3571065a341200b102cde5c1f009a64f757acb 100644 (file)
@@ -4,7 +4,6 @@ import com.intellij.openapi.project.Project;
 import com.intellij.openapi.ui.DialogWrapper;
 import com.intellij.openapi.ui.ValidationInfo;
 import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.openapi.ui.DialogWrapper;
 import com.intellij.openapi.ui.ValidationInfo;
 import com.intellij.openapi.util.text.StringUtil;
-import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
@@ -14,8 +13,7 @@ import java.util.List;
 
 public class CCCreateAnswerPlaceholderDialog extends DialogWrapper {
 
 
 public class CCCreateAnswerPlaceholderDialog extends DialogWrapper {
 
-  private static final String ourTitle = "Add Answer Placeholder";
-  private final AnswerPlaceholder myAnswerPlaceholder;
+  private static final String TITLE = "Add Answer Placeholder";
   private final CCCreateAnswerPlaceholderPanel myPanel;
   private final Project myProject;
 
   private final CCCreateAnswerPlaceholderPanel myPanel;
   private final Project myProject;
 
@@ -24,32 +22,24 @@ public class CCCreateAnswerPlaceholderDialog extends DialogWrapper {
   }
 
   public CCCreateAnswerPlaceholderDialog(@NotNull final Project project,
   }
 
   public CCCreateAnswerPlaceholderDialog(@NotNull final Project project,
-                                         @NotNull final AnswerPlaceholder answerPlaceholder) {
+                                         String placeholderText,
+                                         List<String> hints) {
     super(project, true);
     
     super(project, true);
     
-    myAnswerPlaceholder = answerPlaceholder;
     myProject = project;
     myProject = project;
-    myPanel = new CCCreateAnswerPlaceholderPanel(answerPlaceholder);
-    myPanel.showAnswerPlaceholderText(StringUtil.notNullize(answerPlaceholder.getTaskText()));
-    
-    setTitle(ourTitle);
+    myPanel = new CCCreateAnswerPlaceholderPanel(placeholderText, hints);
+    setTitle(TITLE);
     init();
     initValidation();
   }
 
     init();
     initValidation();
   }
 
-  @Override
-  protected void doOKAction() {
-    String answerPlaceholderText = myPanel.getAnswerPlaceholderText();
-    myAnswerPlaceholder.setTaskText(StringUtil.notNullize(answerPlaceholderText));
-    myAnswerPlaceholder.setLength(StringUtil.notNullize(answerPlaceholderText).length());
+  public String getTaskText() {
+    return StringUtil.notNullize(myPanel.getAnswerPlaceholderText());
+  }
+
+  public List<String> getHints() {
     final List<String> hints = myPanel.getHints();
     final List<String> hints = myPanel.getHints();
-    if (hints.size() == 1 && hints.get(0).isEmpty()) {
-      myAnswerPlaceholder.setHints(Collections.emptyList());
-    }
-    else {
-      myAnswerPlaceholder.setHints(hints);
-    }
-    super.doOKAction();
+    return hints.size() == 1 && hints.get(0).isEmpty() ? Collections.emptyList() : hints;
   }
 
   @Nullable
   }
 
   @Nullable
index 9dd1757670c6db1e0f39d3bb070e28273dd46d89..f5ccc64ddd57bf9426486bfe5cc1b4dbe922c234 100644 (file)
@@ -10,7 +10,6 @@ import com.intellij.ui.JBColor;
 import com.intellij.ui.components.JBLabel;
 import com.intellij.uiDesigner.core.GridLayoutManager;
 import com.intellij.util.ui.UIUtil;
 import com.intellij.ui.components.JBLabel;
 import com.intellij.uiDesigner.core.GridLayoutManager;
 import com.intellij.util.ui.UIUtil;
-import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
 import org.jetbrains.annotations.NotNull;
 
 import javax.swing.*;
 import org.jetbrains.annotations.NotNull;
 
 import javax.swing.*;
@@ -21,7 +20,11 @@ import java.util.ArrayList;
 import java.util.List;
 
 public class CCCreateAnswerPlaceholderPanel {
 import java.util.List;
 
 public class CCCreateAnswerPlaceholderPanel {
-  private static String ourFirstHintText = "Type here to add hint";
+  private static final String NEXT_HINT = "Next Hint";
+  private static final String PREVIOUS_HINT = "Previous Hint";
+  private static final String ADD_HINT = "Add Hint";
+  private static final String REMOVE_HINT = "Remove Hint";
+  private static final String HINT_PLACEHOLDER = "Type here to add hint";
 
   private JPanel myPanel;
   private JTextArea myHintTextArea;
 
   private JPanel myPanel;
   private JTextArea myHintTextArea;
@@ -29,16 +32,15 @@ public class CCCreateAnswerPlaceholderPanel {
   private JBLabel myHintLabel;
   private JPanel actionsPanel;
   private JTextArea myPlaceholderTextArea;
   private JBLabel myHintLabel;
   private JPanel actionsPanel;
   private JTextArea myPlaceholderTextArea;
-  private List<String> myHints = new ArrayList<String>() {
-  };
+  private List<String> myHints = new ArrayList<>();
   private int myShownHintNumber = 0;
 
   private int myShownHintNumber = 0;
 
-  public CCCreateAnswerPlaceholderPanel(@NotNull final AnswerPlaceholder answerPlaceholder) {
-    if (answerPlaceholder.getHints().isEmpty()) {
-      myHints.add(ourFirstHintText);
+  public CCCreateAnswerPlaceholderPanel(String placeholderText, List<String> hints) {
+    if (hints.isEmpty()) {
+      myHints.add(HINT_PLACEHOLDER);
     }
     else {
     }
     else {
-      myHints.addAll(answerPlaceholder.getHints());
+      myHints.addAll(hints);
     }
 
     myPlaceholderTextArea.setBorder(BorderFactory.createLineBorder(JBColor.border()));
     }
 
     myPlaceholderTextArea.setBorder(BorderFactory.createLineBorder(JBColor.border()));
@@ -50,6 +52,7 @@ public class CCCreateAnswerPlaceholderPanel {
 
     actionsPanel.add(createHintToolbarComponent(), BorderLayout.WEST);
     showHint(myHints.get(myShownHintNumber));
 
     actionsPanel.add(createHintToolbarComponent(), BorderLayout.WEST);
     showHint(myHints.get(myShownHintNumber));
+    myPlaceholderTextArea.setText(placeholderText);
   }
 
   @NotNull
   }
 
   @NotNull
@@ -57,7 +60,7 @@ public class CCCreateAnswerPlaceholderPanel {
     return new FocusAdapter() {
       @Override
       public void focusGained(FocusEvent e) {
     return new FocusAdapter() {
       @Override
       public void focusGained(FocusEvent e) {
-        if (myHintTextArea.getText().equals(ourFirstHintText)) {
+        if (myHintTextArea.getText().equals(HINT_PLACEHOLDER)) {
           myHintTextArea.setForeground(UIUtil.getActiveTextColor());
           myHintTextArea.setText("");
         }
           myHintTextArea.setForeground(UIUtil.getActiveTextColor());
           myHintTextArea.setText("");
         }
@@ -67,7 +70,7 @@ public class CCCreateAnswerPlaceholderPanel {
       public void focusLost(FocusEvent e) {
         if (myShownHintNumber == 0 && myHintTextArea.getText().isEmpty()) {
           myHintTextArea.setForeground(UIUtil.getInactiveTextColor());
       public void focusLost(FocusEvent e) {
         if (myShownHintNumber == 0 && myHintTextArea.getText().isEmpty()) {
           myHintTextArea.setForeground(UIUtil.getInactiveTextColor());
-          myHintTextArea.setText(ourFirstHintText);
+          myHintTextArea.setText(HINT_PLACEHOLDER);
         }
       }
     };
         }
       }
     };
@@ -89,12 +92,8 @@ public class CCCreateAnswerPlaceholderPanel {
     }
   }
 
     }
   }
 
-  public void showAnswerPlaceholderText(String answerPlaceholderText) {
-    myPlaceholderTextArea.setText(answerPlaceholderText);
-  }
-
   public void showHint(String hintText) {
   public void showHint(String hintText) {
-    if (myHints.get(myShownHintNumber).equals(ourFirstHintText)) {
+    if (myHints.get(myShownHintNumber).equals(HINT_PLACEHOLDER)) {
       myHintTextArea.setForeground(UIUtil.getInactiveTextColor());
     }
     else {
       myHintTextArea.setForeground(UIUtil.getInactiveTextColor());
     }
     else {
@@ -111,7 +110,7 @@ public class CCCreateAnswerPlaceholderPanel {
 
   public List<String> getHints() {
     final String hintText = myHintTextArea.getText();
 
   public List<String> getHints() {
     final String hintText = myHintTextArea.getText();
-    if (myShownHintNumber == 0 && hintText.equals(ourFirstHintText)) {
+    if (myShownHintNumber == 0 && hintText.equals(HINT_PLACEHOLDER)) {
       myHints.set(myShownHintNumber, "");
     }
     else {
       myHints.set(myShownHintNumber, "");
     }
     else {
@@ -132,7 +131,7 @@ public class CCCreateAnswerPlaceholderPanel {
   private class ShowNext extends AnAction {
 
     public ShowNext() {
   private class ShowNext extends AnAction {
 
     public ShowNext() {
-      super("Next Hint", "Next Hint", AllIcons.Actions.Forward);
+      super(NEXT_HINT, NEXT_HINT, AllIcons.Actions.Forward);
     }
 
     @Override
     }
 
     @Override
@@ -150,7 +149,7 @@ public class CCCreateAnswerPlaceholderPanel {
   private class ShowPrevious extends AnAction {
 
     public ShowPrevious() {
   private class ShowPrevious extends AnAction {
 
     public ShowPrevious() {
-      super("Previous Hint", "Previous Hint", AllIcons.Actions.Back);
+      super(PREVIOUS_HINT, PREVIOUS_HINT, AllIcons.Actions.Back);
     }
 
     @Override
     }
 
     @Override
@@ -168,7 +167,7 @@ public class CCCreateAnswerPlaceholderPanel {
   private class AddHint extends AnAction {
 
     public AddHint() {
   private class AddHint extends AnAction {
 
     public AddHint() {
-      super("Add Hint", "Add Hint", AllIcons.General.Add);
+      super(ADD_HINT, ADD_HINT, AllIcons.General.Add);
     }
 
     @Override
     }
 
     @Override
@@ -182,7 +181,7 @@ public class CCCreateAnswerPlaceholderPanel {
   private class RemoveHint extends AnAction {
 
     public RemoveHint() {
   private class RemoveHint extends AnAction {
 
     public RemoveHint() {
-      super("Remove Hint", "Remove Hint", AllIcons.General.Remove);
+      super(REMOVE_HINT, REMOVE_HINT, AllIcons.General.Remove);
     }
 
     @Override
     }
 
     @Override
index 9ccc793a5b461fea34488a5417515a4ee795f435..530155108d1ee3e8299782ccf3a40dddd98b4294 100644 (file)
@@ -95,7 +95,7 @@ class StudyHint(private val myPlaceholder: AnswerPlaceholder?,
   private inner class EditHint : AnAction("Edit Hint", "Edit Hint", AllIcons.Modules.Edit) {
     
     override fun actionPerformed(e: AnActionEvent?) {
   private inner class EditHint : AnAction("Edit Hint", "Edit Hint", AllIcons.Modules.Edit) {
     
     override fun actionPerformed(e: AnActionEvent?) {
-      val dialog = CCCreateAnswerPlaceholderDialog(e!!.project!!, myPlaceholder!!)
+      val dialog = CCCreateAnswerPlaceholderDialog(e!!.project!!, myPlaceholder!!.taskText, myPlaceholder.hints)
       dialog.show()
     }
 
       dialog.show()
     }