merge Add and Delete Placeholder actions
[idea/community.git] / python / educational-core / course-creator / src / com / jetbrains / edu / coursecreator / actions / CCAnswerPlaceholderAction.java
index 48425e247100938014dc46891a205968394bb1b2..e1633044d5e6ea34c1ba82900af38739bde6b054 100644 (file)
@@ -2,15 +2,15 @@ package com.jetbrains.edu.coursecreator.actions;
 
 import com.intellij.openapi.actionSystem.AnActionEvent;
 import com.intellij.openapi.actionSystem.CommonDataKeys;
-import com.intellij.openapi.actionSystem.Presentation;
 import com.intellij.openapi.editor.Editor;
 import com.intellij.openapi.project.DumbAwareAction;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.psi.PsiFile;
+import com.jetbrains.edu.coursecreator.CCUtils;
+import com.jetbrains.edu.learning.StudyUtils;
 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
 import com.jetbrains.edu.learning.courseFormat.TaskFile;
-import com.jetbrains.edu.coursecreator.CCProjectService;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
@@ -23,9 +23,9 @@ abstract public class CCAnswerPlaceholderAction extends DumbAwareAction {
   }
 
   @Nullable
-  private static CCState getState(@NotNull AnActionEvent e) {
+  protected static CCState getState(@NotNull AnActionEvent e) {
     final Project project = e.getProject();
-    if (project == null || CCProjectService.getInstance(project).getCourse() == null) {
+    if (project == null || !CCUtils.isCourseCreator(project)) {
       return null;
     }
     final PsiFile psiFile = CommonDataKeys.PSI_FILE.getData(e.getDataContext());
@@ -40,27 +40,16 @@ abstract public class CCAnswerPlaceholderAction extends DumbAwareAction {
     if (editor == null) {
       return null;
     }
-    TaskFile taskFile = CCProjectService.getInstance(project).getTaskFile(virtualFile);
+    TaskFile taskFile = StudyUtils.getTaskFile(project, virtualFile);
     if (taskFile == null) {
       return null;
     }
     AnswerPlaceholder answerPlaceholder = taskFile.getAnswerPlaceholder(editor.getDocument(),
                                                                         editor.getCaretModel().getLogicalPosition(),
                                                                         true);
-    if (answerPlaceholder == null) {
-      return null;
-    }
     return new CCState(taskFile, answerPlaceholder, psiFile, editor, project);
   }
 
-  @Override
-  public void update(@NotNull AnActionEvent e) {
-    Presentation presentation = e.getPresentation();
-    boolean isAvailable = getState(e) != null;
-    presentation.setEnabled(isAvailable);
-    presentation.setVisible(isAvailable);
-  }
-
   @Override
   public void actionPerformed(@NotNull AnActionEvent e) {
     CCState state = getState(e);
@@ -80,7 +69,7 @@ abstract public class CCAnswerPlaceholderAction extends DumbAwareAction {
     private Project myProject;
 
     public CCState(@NotNull final TaskFile taskFile,
-                   @NotNull final AnswerPlaceholder answerPlaceholder,
+                   @Nullable final AnswerPlaceholder answerPlaceholder,
                    @NotNull final PsiFile file,
                    @NotNull final Editor editor,
                    @NotNull final Project project) {
@@ -96,7 +85,7 @@ abstract public class CCAnswerPlaceholderAction extends DumbAwareAction {
       return myTaskFile;
     }
 
-    @NotNull
+    @Nullable
     public AnswerPlaceholder getAnswerPlaceholder() {
       return myAnswerPlaceholder;
     }