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 {
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
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();
}
@Nullable
@Override
public ValidationInfo doValidate() {
- return myAnswerPlaceholder.getHints() != null ? null : new ValidationInfo("Type hint");
+ return !myPanel.getHints().isEmpty() ? null : new ValidationInfo("Type hint");
}
@Nullable
<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>
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);
+ }
+ }
}