EDU-596 Remove show hint checkbox in Add/Edit Answer Placeholder dialog
[idea/community.git] / python / educational-core / course-creator / src / com / jetbrains / edu / coursecreator / ui / CCCreateAnswerPlaceholderDialog.java
1 package com.jetbrains.edu.coursecreator.ui;
2
3 import com.intellij.openapi.project.Project;
4 import com.intellij.openapi.ui.DialogWrapper;
5 import com.intellij.openapi.ui.ValidationInfo;
6 import com.intellij.openapi.util.text.StringUtil;
7 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
8 import org.jetbrains.annotations.NotNull;
9 import org.jetbrains.annotations.Nullable;
10
11 import javax.swing.*;
12
13 public class CCCreateAnswerPlaceholderDialog extends DialogWrapper {
14
15   private static final String ourTitle = "Add Answer Placeholder";
16   private final AnswerPlaceholder myAnswerPlaceholder;
17   private final CCCreateAnswerPlaceholderPanel myPanel;
18   private final Project myProject;
19
20   public Project getProject() {
21     return myProject;
22   }
23
24   public CCCreateAnswerPlaceholderDialog(@NotNull final Project project,
25                                          @NotNull final AnswerPlaceholder answerPlaceholder) {
26     super(project, true);
27     setTitle(ourTitle);
28     myAnswerPlaceholder = answerPlaceholder;
29     myPanel = new CCCreateAnswerPlaceholderPanel();
30     if (answerPlaceholder.getHint() != null) {
31       setHintText(answerPlaceholder);
32     }
33     myProject = project;
34     String answerPlaceholderTaskText = answerPlaceholder.getTaskText();
35     myPanel.setAnswerPlaceholderText(answerPlaceholderTaskText != null ? answerPlaceholderTaskText : "");
36     String hintName = answerPlaceholder.getHint();
37     myPanel.setHintText(hintName != null ? hintName : "");
38     init();
39     initValidation();
40   }
41
42   @SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
43   private void setHintText(AnswerPlaceholder answerPlaceholder) {
44     String hintText = answerPlaceholder.getHint();
45
46     myPanel.setHintText(hintText);
47   }
48
49   @Override
50   protected void doOKAction() {
51     String answerPlaceholderText = myPanel.getAnswerPlaceholderText();
52     myAnswerPlaceholder.setTaskText(StringUtil.notNullize(answerPlaceholderText));
53     myAnswerPlaceholder.setLength(StringUtil.notNullize(answerPlaceholderText).length());
54     myAnswerPlaceholder.setHint(myPanel.getHintText());
55     super.doOKAction();
56   }
57
58   @Nullable
59   @Override
60   protected JComponent createCenterPanel() {
61     return myPanel;
62   }
63
64   @Nullable
65   @Override
66   public ValidationInfo doValidate() {
67     return myAnswerPlaceholder.getHint() != null ? null : new ValidationInfo("Type hint");
68   }
69
70   @Nullable
71   @Override
72   public JComponent getPreferredFocusedComponent() {
73     return myPanel.getPreferredFocusedComponent();
74   }
75 }