EDU-666 Change Add Answer Placeholder dialog: add ability to add multiple hints to...
authorValentina Kiryushkina <valentina.kiryushkina@jetbrains.com>
Fri, 8 Jul 2016 17:02:21 +0000 (20:02 +0300)
committerValentina Kiryushkina <valentina.kiryushkina@jetbrains.com>
Sat, 9 Jul 2016 10:11:59 +0000 (13:11 +0300)
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/ui/CCCreateAnswerPlaceholderDialog.java
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/ui/CCCreateAnswerPlaceholderPanel.form
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/ui/CCCreateAnswerPlaceholderPanel.java
python/educational-core/student/src/com/jetbrains/edu/learning/ui/StudyHint.kt

index 0459c3da2557efb186e75180e3e1750a5798d4a9..3ce4e8ecae8feec2e56be9c3af0dba785e4f36e4 100644 (file)
@@ -9,7 +9,7 @@ import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
 import javax.swing.*;
-import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 
 public class CCCreateAnswerPlaceholderDialog extends DialogWrapper {
@@ -29,23 +29,21 @@ public class CCCreateAnswerPlaceholderDialog extends DialogWrapper {
     setTitle(ourTitle);
     myAnswerPlaceholder = answerPlaceholder;
     myPanel = new CCCreateAnswerPlaceholderPanel();
-    if (answerPlaceholder.getHints() != null) {
+    if (!answerPlaceholder.getHints().isEmpty()) {
       setHintText(answerPlaceholder);
     }
     myProject = project;
     String answerPlaceholderTaskText = answerPlaceholder.getTaskText();
     myPanel.setAnswerPlaceholderText(answerPlaceholderTaskText != null ? answerPlaceholderTaskText : "");
-    List<String> hintName = answerPlaceholder.getHints();
-    myPanel.setHintText(!hintName.isEmpty() ? hintName.get(0) : "");
     init();
     initValidation();
   }
 
   @SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
   private void setHintText(AnswerPlaceholder answerPlaceholder) {
-    List<String> hintText = answerPlaceholder.getHints();
-
-    myPanel.setHintText(hintText.isEmpty()? "" : hintText.get(0));
+    List<String> hintTexts = answerPlaceholder.getHints();
+    myPanel.setHintText(hintTexts.get(0));
+    myPanel.setHints(hintTexts);
   }
 
   @Override
@@ -53,10 +51,13 @@ public class CCCreateAnswerPlaceholderDialog extends DialogWrapper {
     String answerPlaceholderText = myPanel.getAnswerPlaceholderText();
     myAnswerPlaceholder.setTaskText(StringUtil.notNullize(answerPlaceholderText));
     myAnswerPlaceholder.setLength(StringUtil.notNullize(answerPlaceholderText).length());
-    myAnswerPlaceholder.setHints(
-      new ArrayList<String>() {{
-        add(myPanel.getHintText());
-      }});
+    final List<String> hints = myPanel.getHints();
+    if (hints.size() == 1 && hints.get(0).isEmpty()) {
+      myAnswerPlaceholder.setHints(Collections.emptyList());
+    }
+    else {
+      myAnswerPlaceholder.setHints(hints);
+    }
     super.doOKAction();
   }
 
@@ -69,7 +70,7 @@ public class CCCreateAnswerPlaceholderDialog extends DialogWrapper {
   @Nullable
   @Override
   public ValidationInfo doValidate() {
-    return myAnswerPlaceholder.getHints() != null ? null : new ValidationInfo("Type hint");
+    return !myPanel.getHints().isEmpty() ? null : new ValidationInfo("Type hint");
   }
 
   @Nullable
index e5ff6da604d1c67b2f2fced24b20e65935db17d8..d10bea0e75dc219df1e164f6e5ef80b36f5f12e1 100644 (file)
@@ -3,11 +3,33 @@
   <grid id="27dc6" binding="myPanel" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
     <margin top="0" left="0" bottom="0" right="0"/>
     <constraints>
-      <xy x="20" y="20" width="450" height="177"/>
+      <xy x="20" y="20" width="652" height="302"/>
     </constraints>
     <properties/>
     <border type="none"/>
     <children>
+      <grid id="ff283" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
+        <margin top="0" left="0" bottom="2" right="0"/>
+        <constraints>
+          <grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
+        </constraints>
+        <properties/>
+        <border type="none"/>
+        <children>
+          <component id="2e28f" class="javax.swing.JTextField" binding="myAnswerPlaceholderText">
+            <constraints>
+              <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
+                <minimum-size width="300" height="30"/>
+                <preferred-size width="-1" height="30"/>
+              </grid>
+            </constraints>
+            <properties>
+              <margin top="0" left="0" bottom="0" right="0"/>
+              <text value=""/>
+            </properties>
+          </component>
+        </children>
+      </grid>
       <component id="aaa28" class="javax.swing.JLabel">
         <constraints>
           <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
           <text value="Placeholder:"/>
         </properties>
       </component>
-      <component id="2e28f" class="javax.swing.JTextField" binding="myAnswerPlaceholderText">
+      <grid id="996a1" binding="myHintsPanel" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
+        <margin top="2" left="0" bottom="0" right="0"/>
         <constraints>
-          <grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
-            <minimum-size width="300" height="30"/>
-            <preferred-size width="-1" height="30"/>
-          </grid>
+          <grid row="1" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
         </constraints>
-        <properties>
-          <text value=""/>
-        </properties>
-      </component>
-      <component id="61da7" class="javax.swing.JLabel">
+        <properties/>
+        <border type="none"/>
+        <children>
+          <component id="d0efc" class="javax.swing.JTextArea" binding="myHintTextField">
+            <constraints>
+              <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
+                <minimum-size width="300" height="150"/>
+                <preferred-size width="-1" height="150"/>
+              </grid>
+            </constraints>
+            <properties>
+              <alignmentX value="0.5"/>
+              <text value=""/>
+            </properties>
+          </component>
+          <grid id="4a2a2" binding="actionsPanel" layout-manager="BorderLayout" hgap="0" vgap="0">
+            <constraints>
+              <grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
+            </constraints>
+            <properties/>
+            <border type="none"/>
+            <children/>
+          </grid>
+        </children>
+      </grid>
+      <component id="2c397" class="com.intellij.ui.components.JBLabel" binding="myHintLabel">
         <constraints>
-          <grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="9" fill="0" indent="0" use-parent-layout="false"/>
+          <grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="1" fill="0" indent="0" use-parent-layout="false"/>
         </constraints>
         <properties>
-          <labelFor value="d0efc"/>
           <text value="Hint:"/>
         </properties>
       </component>
-      <component id="d0efc" class="javax.swing.JTextArea" binding="myHintText">
-        <constraints>
-          <grid row="1" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
-            <minimum-size width="300" height="150"/>
-            <preferred-size width="-1" height="150"/>
-          </grid>
-        </constraints>
-        <properties/>
-      </component>
     </children>
   </grid>
 </form>
index f932a185d02d0b599f164bbbb45a1619b69900e9..c130ac3e9dcdb48b36e0aec4205e52837086d2ed 100644 (file)
 package com.jetbrains.edu.coursecreator.ui;
 
+import com.intellij.icons.AllIcons;
+import com.intellij.openapi.actionSystem.ActionManager;
+import com.intellij.openapi.actionSystem.AnAction;
+import com.intellij.openapi.actionSystem.AnActionEvent;
+import com.intellij.openapi.actionSystem.DefaultActionGroup;
 import com.intellij.ui.JBColor;
+import com.intellij.ui.components.JBLabel;
+import com.intellij.uiDesigner.core.GridLayoutManager;
 
 import javax.swing.*;
 import java.awt.*;
+import java.util.ArrayList;
+import java.util.List;
 
 public class CCCreateAnswerPlaceholderPanel extends JPanel {
 
   private JPanel myPanel;
-  private JTextArea myHintText;
+  private JTextArea myHintTextField;
   private JTextField myAnswerPlaceholderText;
+  private JBLabel myHintLabel;
+  private JPanel actionsPanel;
+  private JPanel myHintsPanel;
+
+  private List<String> myHints = new ArrayList<String>() {{
+    add("");
+  }};
+  private int myShownHintNumber = 0;
 
   public CCCreateAnswerPlaceholderPanel() {
     super(new BorderLayout());
     add(myPanel, BorderLayout.CENTER);
-    myHintText.setLineWrap(true);
-    myHintText.setWrapStyleWord(true);
-    myHintText.setBorder(BorderFactory.createLineBorder(JBColor.border()));
-    myHintText.setFont(myAnswerPlaceholderText.getFont());
+
+    myHintTextField.setLineWrap(true);
+    myHintTextField.setWrapStyleWord(true);
+    myHintTextField.setBorder(BorderFactory.createLineBorder(JBColor.border()));
+    myHintTextField.setFont(myAnswerPlaceholderText.getFont());
+    myHintTextField.setText(myHints.get(myShownHintNumber));
     myAnswerPlaceholderText.grabFocus();
+    updateHintNumberLabel();
+    
+    ((GridLayoutManager)myHintsPanel.getLayout()).setHGap(1);
+
+    final DefaultActionGroup addRemoveGroup = new DefaultActionGroup();
+    addRemoveGroup.addAll(new AddHint(), new RemoveHint(), new GoBackward(), new GoForward());
+    final JComponent addRemoveComponent = ActionManager.getInstance().createActionToolbar("Hint", addRemoveGroup, false).getComponent();
+    actionsPanel.add(addRemoveComponent, BorderLayout.WEST);
+  }
+
+  private void updateHintNumberLabel() {
+    myHintLabel.setText("Hints" + "(" + (myShownHintNumber + 1) + "/" + myHints.size() + "):");
   }
 
   public void setAnswerPlaceholderText(String answerPlaceholderText) {
     myAnswerPlaceholderText.setText(answerPlaceholderText);
   }
 
-  public void setHintText(String hintText) {
-    myHintText.setText(hintText);
+  public void setHintText(String hintTextField) {
+    myHintTextField.setText(hintTextField);
   }
 
   public String getAnswerPlaceholderText() {
     return myAnswerPlaceholderText.getText();
   }
 
-  public String getHintText() {
-    return myHintText.getText();
+  public List<String> getHints() {
+    if (myShownHintNumber == 0 && !myHintTextField.getText().isEmpty()) {
+      myHints.set(myShownHintNumber, myHintTextField.getText());
+    }
+    return myHints;
   }
 
   public JComponent getPreferredFocusedComponent() {
     return myAnswerPlaceholderText;
   }
+
+  public void setHints(List<String> hints) {
+    myHints = hints;
+    updateHintNumberLabel();
+  }
+
+  private void createUIComponents() {
+    // TODO: place custom component creation code here 
+  }
+
+  private class GoForward extends AnAction {
+
+    public GoForward() {
+      super(AllIcons.Actions.Forward);
+    }
+
+    @Override
+    public void actionPerformed(AnActionEvent e) {
+      myHints.set(myShownHintNumber, myHintTextField.getText());
+      myHintTextField.setText(myHints.get(++myShownHintNumber));
+      updateHintNumberLabel();
+    }
+
+    @Override
+    public void update(AnActionEvent e) {
+      e.getPresentation().setEnabled(myShownHintNumber + 1 < myHints.size());
+    }
+  }
+
+  private class GoBackward extends AnAction {
+
+    public GoBackward() {
+      super(AllIcons.Actions.Back);
+    }
+
+    @Override
+    public void actionPerformed(AnActionEvent e) {
+      myHints.set(myShownHintNumber, myHintTextField.getText());
+      myHintTextField.setText(myHints.get(--myShownHintNumber));
+      updateHintNumberLabel();
+    }
+
+    @Override
+    public void update(AnActionEvent e) {
+      e.getPresentation().setEnabled(myShownHintNumber - 1 >= 0);
+    }
+  }
+
+  private class AddHint extends AnAction {
+
+    public AddHint() {
+      super(AllIcons.General.Add);
+    }
+
+    @Override
+    public void actionPerformed(AnActionEvent e) {
+      final String hint = "This is the new hint";
+      myHints.add(hint);
+      myHintTextField.setText(hint);
+      myShownHintNumber++;
+      updateHintNumberLabel();
+    }
+  }
+
+  private class RemoveHint extends AnAction {
+
+    public RemoveHint() {
+      super(AllIcons.General.Remove);
+    }
+
+    @Override
+    public void actionPerformed(AnActionEvent e) {
+      myHints.remove(myShownHintNumber);
+      if (myHints.size() == 1) {
+        myShownHintNumber = 0;
+      }
+      else {
+        myShownHintNumber += myShownHintNumber + 1 < myHints.size() ? 1 : -1;
+      }
+      myHintTextField.setText(myHints.get(myShownHintNumber));
+      updateHintNumberLabel();
+    }
+
+    @Override
+    public void update(AnActionEvent e) {
+      e.getPresentation().setEnabled(myHints.size() > 1);
+    }
+  }
 }
index c468a0fc6c7cc44be47a6153886d0588bbd750c0..b587efea6622190c59f1eda6428158781d31bda2 100644 (file)
@@ -40,9 +40,13 @@ class StudyHint(private val myPlaceholder: AnswerPlaceholder?, project: Project)
     if (course != null) {
       val courseMode = course.courseMode
       val group = DefaultActionGroup()
-      group.addAll(GoBackward(), GoForward())
-      if (EduNames.STUDY != courseMode) {
-        group.addAll(Separator.getInstance(), EditHint(), AddHint(), RemoveHint())
+      if (courseMode == EduNames.STUDY) {
+        if (myHints.size > 1) {
+          group.addAll(GoBackward(), GoForward())
+        }
+      }
+      else {
+        group.addAll(GoBackward(), GoForward(), Separator.getInstance(), EditHint(), AddHint(), RemoveHint())
       }
       studyToolWindow.setActionToolbar(group)
       if (!myHints.isEmpty()) {