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;
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();
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;
}
+ 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));
}
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
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;
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;
}
public CCCreateAnswerPlaceholderDialog(@NotNull final Project project,
- @NotNull final AnswerPlaceholder answerPlaceholder) {
+ String placeholderText,
+ List<String> hints) {
super(project, true);
- myAnswerPlaceholder = answerPlaceholder;
myProject = project;
- myPanel = new CCCreateAnswerPlaceholderPanel(answerPlaceholder);
- myPanel.showAnswerPlaceholderText(StringUtil.notNullize(answerPlaceholder.getTaskText()));
-
- setTitle(ourTitle);
+ myPanel = new CCCreateAnswerPlaceholderPanel(placeholderText, hints);
+ setTitle(TITLE);
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();
- 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
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 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 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;
- 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 {
- myHints.addAll(answerPlaceholder.getHints());
+ myHints.addAll(hints);
}
myPlaceholderTextArea.setBorder(BorderFactory.createLineBorder(JBColor.border()));
actionsPanel.add(createHintToolbarComponent(), BorderLayout.WEST);
showHint(myHints.get(myShownHintNumber));
+ myPlaceholderTextArea.setText(placeholderText);
}
@NotNull
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("");
}
public void focusLost(FocusEvent e) {
if (myShownHintNumber == 0 && myHintTextArea.getText().isEmpty()) {
myHintTextArea.setForeground(UIUtil.getInactiveTextColor());
- myHintTextArea.setText(ourFirstHintText);
+ myHintTextArea.setText(HINT_PLACEHOLDER);
}
}
};
}
}
- public void showAnswerPlaceholderText(String answerPlaceholderText) {
- myPlaceholderTextArea.setText(answerPlaceholderText);
- }
-
public void showHint(String hintText) {
- if (myHints.get(myShownHintNumber).equals(ourFirstHintText)) {
+ if (myHints.get(myShownHintNumber).equals(HINT_PLACEHOLDER)) {
myHintTextArea.setForeground(UIUtil.getInactiveTextColor());
}
else {
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 {
private class ShowNext extends AnAction {
public ShowNext() {
- super("Next Hint", "Next Hint", AllIcons.Actions.Forward);
+ super(NEXT_HINT, NEXT_HINT, AllIcons.Actions.Forward);
}
@Override
private class ShowPrevious extends AnAction {
public ShowPrevious() {
- super("Previous Hint", "Previous Hint", AllIcons.Actions.Back);
+ super(PREVIOUS_HINT, PREVIOUS_HINT, AllIcons.Actions.Back);
}
@Override
private class AddHint extends AnAction {
public AddHint() {
- super("Add Hint", "Add Hint", AllIcons.General.Add);
+ super(ADD_HINT, ADD_HINT, AllIcons.General.Add);
}
@Override
private class RemoveHint extends AnAction {
public RemoveHint() {
- super("Remove Hint", "Remove Hint", AllIcons.General.Remove);
+ super(REMOVE_HINT, REMOVE_HINT, AllIcons.General.Remove);
}
@Override