package com.jetbrains.edu.coursecreator.actions.placeholder;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
import com.jetbrains.edu.learning.ui.CCCreateAnswerPlaceholderDialog;
import org.jetbrains.annotations.NotNull;

public class CCEditAnswerPlaceholder extends CCAnswerPlaceholderAction {

  public CCEditAnswerPlaceholder() {
    super("Edit", "Edit answer placeholder");
  }

  @Override
  protected void performAnswerPlaceholderAction(@NotNull CCState state) {
    final Project project = state.getProject();
    PsiFile file = state.getFile();
    final PsiDirectory taskDir = file.getContainingDirectory();
    final PsiDirectory lessonDir = taskDir.getParent();
    if (lessonDir == null) return;
    AnswerPlaceholder answerPlaceholder = state.getAnswerPlaceholder();
    if (answerPlaceholder == null) {
      return;
    }
    CCCreateAnswerPlaceholderDialog dlg = new CCCreateAnswerPlaceholderDialog(project, answerPlaceholder.getTaskText(), answerPlaceholder.getHints());
    dlg.setTitle("Edit Answer Placeholder");
    if (dlg.showAndGet()) {
      answerPlaceholder.setTaskText(dlg.getTaskText());
      answerPlaceholder.setHints(dlg.getHints());
    }
  }

  @Override
  public void update(AnActionEvent e) {
    Presentation presentation = e.getPresentation();
    presentation.setEnabledAndVisible(false);
    CCState state = getState(e);
    if (state == null || state.getAnswerPlaceholder() == null) {
      return;
    }
    presentation.setEnabledAndVisible(true);
  }
}
@@ -211,16 +211,66 @@
 roject;%0A
+import com.intellij.openapi.util.text.StringUtil;%0A
 import c
@@ -1353,24 +1353,37 @@
 ()) %7B%0A      
+final String 
 answerPlaceh
@@ -1391,36 +1391,179 @@
 lder
-.setTaskText(dlg.getTaskText
+Text = dlg.getTaskText();%0A      answerPlaceholder.setTaskText(answerPlaceholderText);%0A      answerPlaceholder.setLength(StringUtil.notNullize(answerPlaceholderText).length
 ());

Update length properly after editing answer placeholder