merge Add and Delete Placeholder actions
authorliana.bakradze <liana.bakradze@jetbrains.com>
Wed, 6 Apr 2016 10:54:52 +0000 (13:54 +0300)
committerliana.bakradze <liana.bakradze@jetbrains.com>
Wed, 6 Apr 2016 10:54:52 +0000 (13:54 +0300)
python/educational-core/course-creator/resources/META-INF/plugin.xml
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCAddAnswerPlaceholder.java
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCAnswerPlaceholderAction.java
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCDeleteAnswerPlaceholder.java [deleted file]
python/educational-core/student/src/com/jetbrains/edu/learning/core/EduNames.java

index 24da17d3f29c45f6a1c40e5e394e5f069e253fa9..9b90a825914ac1c23a7d9f7c7c5f73a6de75f349 100644 (file)
   <depends>com.jetbrains.edu.interactivelearning</depends>
 
   <extensions defaultExtensionNs="com.intellij">
-    <projectService serviceImplementation="com.jetbrains.edu.coursecreator.CCProjectService"/>
     <treeStructureProvider implementation="com.jetbrains.edu.coursecreator.projectView.CCTreeStructureProvider"/>
     <refactoring.elementListenerProvider implementation="com.jetbrains.edu.coursecreator.CCRefactoringElementListenerProvider"/>
-    <refactoring.moveHandler implementation="com.jetbrains.edu.coursecreator.CCLessonMoveHandlerDelegate" order="first"/>
-    <refactoring.moveHandler implementation="com.jetbrains.edu.coursecreator.CCTaskMoveHandlerDelegate" order="first"/>
-    <renameHandler implementation="com.jetbrains.edu.coursecreator.CCTaskRenameHandler" order="first"/>
-    <renameHandler implementation="com.jetbrains.edu.coursecreator.CCLessonRenameHandler" order="first"/>
+    <refactoring.moveHandler implementation="com.jetbrains.edu.coursecreator.handlers.CCLessonMoveHandlerDelegate" order="first"/>
+    <refactoring.moveHandler implementation="com.jetbrains.edu.coursecreator.handlers.CCTaskMoveHandlerDelegate" order="first"/>
+    <renameHandler implementation="com.jetbrains.edu.coursecreator.handlers.CCTaskRenameHandler" order="first"/>
+    <renameHandler implementation="com.jetbrains.edu.coursecreator.handlers.CCLessonRenameHandler" order="first"/>
   </extensions>
   <extensions defaultExtensionNs="Edu">
     <studyActionsProvider implementation="com.jetbrains.edu.coursecreator.CCStudyActionsProvider"/>
@@ -33,7 +32,6 @@
   </application-components>
 
   <project-components>
-    <!-- Add your project components here -->
     <component>
       <implementation-class>com.jetbrains.edu.coursecreator.CCProjectComponent</implementation-class>
     </component>
     <action id="AddTaskFile" class="com.jetbrains.edu.coursecreator.actions.CCAddAsTaskFile" text="Add As Task File">
       <add-to-group group-id="ProjectViewPopupMenu" anchor="first"/>
     </action>
-    <action id="AddTaskWindow" class="com.jetbrains.edu.coursecreator.actions.CCAddAnswerPlaceholder">
-      <add-to-group group-id="EditorPopupMenu" anchor="before" relative-to-action="CopyReference"/>
-    </action>
+    <group id="AnswerPlaceholderGroup">
+      <action id="AddTaskWindow" class="com.jetbrains.edu.coursecreator.actions.CCAddAnswerPlaceholder"/>
+       <separator/>
+      <add-to-group group-id="EditorPopupMenu" anchor="first"/>
+    </group>
     <action id="ShowTaskWindowDetails" class="com.jetbrains.edu.coursecreator.actions.CCShowAnswerPlaceholderDetails">
       <add-to-group group-id="EditorPopupMenu" anchor="before" relative-to-action="CopyReference"/>
     </action>
-    <action id="DeleteTaskWindow" class="com.jetbrains.edu.coursecreator.actions.CCDeleteAnswerPlaceholder">
-      <add-to-group group-id="EditorPopupMenu" anchor="before" relative-to-action="CopyReference"/>
-    </action>
     <action id="ShowPreview" class="com.jetbrains.edu.coursecreator.actions.CCShowPreview">
       <add-to-group group-id="ProjectViewPopupMenu" anchor="first"/>
       <add-to-group group-id="EditorTabPopupMenu"/>
index 791d7fb0efae49949a7713ab5c2248292ba7f748..aab278175e0b62e83f3cde8e30fac1d023286266 100644 (file)
@@ -1,32 +1,29 @@
 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.diagnostic.Logger;
 import com.intellij.openapi.editor.Document;
 import com.intellij.openapi.editor.Editor;
 import com.intellij.openapi.editor.SelectionModel;
-import com.intellij.openapi.project.DumbAwareAction;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.ui.DialogWrapper;
-import com.intellij.psi.PsiDirectory;
 import com.intellij.psi.PsiDocumentManager;
 import com.intellij.psi.PsiFile;
 import com.intellij.ui.JBColor;
-import com.jetbrains.edu.learning.core.EduAnswerPlaceholderPainter;
-import com.jetbrains.edu.coursecreator.CCProjectService;
 import com.jetbrains.edu.coursecreator.ui.CCCreateAnswerPlaceholderDialog;
-import com.jetbrains.edu.learning.courseFormat.*;
+import com.jetbrains.edu.learning.StudyUtils;
+import com.jetbrains.edu.learning.core.EduAnswerPlaceholderPainter;
+import com.jetbrains.edu.learning.core.EduNames;
+import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
+import com.jetbrains.edu.learning.courseFormat.TaskFile;
 import org.jetbrains.annotations.NotNull;
 
 import java.util.List;
 
-public class CCAddAnswerPlaceholder extends DumbAwareAction {
-  private static final Logger LOG = Logger.getInstance(CCAddAnswerPlaceholder.class);
+public class CCAddAnswerPlaceholder extends CCAnswerPlaceholderAction {
 
   public CCAddAnswerPlaceholder() {
-    super("Add Answer Placeholder", "Add answer placeholder", null);
+    super("Add/Delete Answer Placeholder", "Add/Delete answer placeholder", null);
   }
 
 
@@ -43,47 +40,33 @@ public class CCAddAnswerPlaceholder extends DumbAwareAction {
     return false;
   }
 
-  @Override
-  public void actionPerformed(@NotNull AnActionEvent e) {
-    final Project project = e.getData(CommonDataKeys.PROJECT);
-    if (project == null) {
+  private static void addPlaceholder(@NotNull CCState state) {
+    Editor editor = state.getEditor();
+    Project project = state.getProject();
+    PsiFile file = state.getFile();
+
+    final Document document = PsiDocumentManager.getInstance(project).getDocument(file);
+    if (document == null) {
       return;
     }
-    final PsiFile file = CommonDataKeys.PSI_FILE.getData(e.getDataContext());
-    if (file == null) return;
-    final Editor editor = CommonDataKeys.EDITOR.getData(e.getDataContext());
-    if (editor == null) return;
+
     final SelectionModel model = editor.getSelectionModel();
-    final Document document = PsiDocumentManager.getInstance(project).getDocument(file);
-    if (document == null) return;
     final int start = model.getSelectionStart();
-    final int end = model.getSelectionEnd();
     final int lineNumber = document.getLineNumber(start);
     int realStart = start - document.getLineStartOffset(lineNumber);
-
-    final CCProjectService service = CCProjectService.getInstance(project);
-    final PsiDirectory taskDir = file.getContainingDirectory();
-    final PsiDirectory lessonDir = taskDir.getParent();
-    if (lessonDir == null) return;
-
-    final TaskFile taskFile = service.getTaskFile(file.getVirtualFile());
-    if (taskFile == null) {
-      return;
-    }
-    if (arePlaceholdersIntersect(taskFile, document, start, end)) {
-      return;
-    }
     final AnswerPlaceholder answerPlaceholder = new AnswerPlaceholder();
     answerPlaceholder.setLine(lineNumber);
     answerPlaceholder.setStart(realStart);
-    answerPlaceholder.setPossibleAnswer(model.getSelectedText());
+    String selectedText = model.getSelectedText();
+    answerPlaceholder.setPossibleAnswer(selectedText);
 
-    CCCreateAnswerPlaceholderDialog dlg = new CCCreateAnswerPlaceholderDialog(project, answerPlaceholder
-    );
+    CCCreateAnswerPlaceholderDialog dlg = new CCCreateAnswerPlaceholderDialog(project, answerPlaceholder);
     dlg.show();
     if (dlg.getExitCode() != DialogWrapper.OK_EXIT_CODE) {
       return;
     }
+
+    TaskFile taskFile = state.getTaskFile();
     int index = taskFile.getAnswerPlaceholders().size() + 1;
     answerPlaceholder.setIndex(index);
     taskFile.addAnswerPlaceholder(answerPlaceholder);
@@ -93,64 +76,66 @@ public class CCAddAnswerPlaceholder extends DumbAwareAction {
   }
 
   @Override
-  public void update(@NotNull AnActionEvent event) {
-    if (!CCProjectService.setCCActionAvailable(event)) {
+  protected void performAnswerPlaceholderAction(@NotNull CCState state) {
+    if (canAddPlaceholder(state)) {
+      addPlaceholder(state);
       return;
     }
+    if (canDeletePlaceholder(state)) {
+      deletePlaceholder(state);
+    }
+  }
+
+  private static void deletePlaceholder(@NotNull CCState state) {
+    Project project = state.getProject();
+    PsiFile psiFile = state.getFile();
+    final Document document = PsiDocumentManager.getInstance(project).getDocument(psiFile);
+    if (document == null) return;
+    TaskFile taskFile = state.getTaskFile();
+    AnswerPlaceholder answerPlaceholder = state.getAnswerPlaceholder();
+    final List<AnswerPlaceholder> answerPlaceholders = taskFile.getAnswerPlaceholders();
+    if (answerPlaceholders.contains(answerPlaceholder)) {
+      answerPlaceholders.remove(answerPlaceholder);
+      final Editor editor = state.getEditor();
+      editor.getMarkupModel().removeAllHighlighters();
+      StudyUtils.drawAllWindows(editor, taskFile);
+      EduAnswerPlaceholderPainter.createGuardedBlocks(editor, taskFile, false);
+    }
+  }
+
+  @Override
+  public void update(@NotNull AnActionEvent event) {
     final Presentation presentation = event.getPresentation();
-    final Project project = event.getData(CommonDataKeys.PROJECT);
-    if (project == null) {
-      presentation.setVisible(false);
-      presentation.setEnabled(false);
+    presentation.setEnabledAndVisible(false);
+
+    CCState state = getState(event);
+    if (state == null) {
       return;
     }
-    final Editor editor = CommonDataKeys.EDITOR.getData(event.getDataContext());
-    final PsiFile file = CommonDataKeys.PSI_FILE.getData(event.getDataContext());
-    if (editor == null || file == null) {
-      presentation.setVisible(false);
-      presentation.setEnabled(false);
-      return;
+
+    presentation.setVisible(true);
+    if (canAddPlaceholder(state) || canDeletePlaceholder(state)) {
+      presentation.setEnabled(true);
+      presentation.setText((state.getAnswerPlaceholder() == null ? "Add " : "Delete ") + EduNames.PLACEHOLDER);
     }
+  }
+
+
+  private static boolean canAddPlaceholder(@NotNull CCState state) {
+    Editor editor = state.getEditor();
     SelectionModel selectionModel = editor.getSelectionModel();
     if (!selectionModel.hasSelection()) {
-      presentation.setVisible(false);
-      presentation.setEnabled(false);
-      return;
+      return false;
     }
     int start = selectionModel.getSelectionStart();
     int end = selectionModel.getSelectionEnd();
+    return !arePlaceholdersIntersect(state.getTaskFile(), editor.getDocument(), start, end);
+  }
 
-    final CCProjectService service = CCProjectService.getInstance(project);
-    final PsiDirectory taskDir = file.getContainingDirectory();
-    final PsiDirectory lessonDir = taskDir.getParent();
-    if (lessonDir == null) return;
-
-    final Course course = service.getCourse();
-    final Lesson lesson = course.getLesson(lessonDir.getName());
-    if (lesson == null) {
-      presentation.setVisible(false);
-      presentation.setEnabled(false);
-      return;
-    }
-    final Task task = lesson.getTask(taskDir.getName());
-    if (task == null) {
-      presentation.setVisible(false);
-      presentation.setEnabled(false);
-      return;
+  private static boolean canDeletePlaceholder(@NotNull CCState state) {
+    if (state.getEditor().getSelectionModel().hasSelection()) {
+      return false;
     }
-    TaskFile taskFile = service.getTaskFile(file.getVirtualFile());
-    if (taskFile == null) {
-      LOG.info("could not find task file");
-      presentation.setVisible(false);
-      presentation.setEnabled(false);
-      return;
-    }
-    if (arePlaceholdersIntersect(taskFile, editor.getDocument(), start, end)) {
-      presentation.setVisible(false);
-      presentation.setEnabled(false);
-      return;
-    }
-    presentation.setVisible(true);
-    presentation.setEnabled(true);
+    return state.getAnswerPlaceholder() != null;
   }
 }
\ No newline at end of file
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;
     }
diff --git a/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCDeleteAnswerPlaceholder.java b/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCDeleteAnswerPlaceholder.java
deleted file mode 100644 (file)
index e385589..0000000
+++ /dev/null
@@ -1,39 +0,0 @@
-package com.jetbrains.edu.coursecreator.actions;
-
-import com.intellij.openapi.editor.Document;
-import com.intellij.openapi.editor.Editor;
-import com.intellij.openapi.project.Project;
-import com.intellij.psi.PsiDocumentManager;
-import com.intellij.psi.PsiFile;
-import com.jetbrains.edu.learning.core.EduAnswerPlaceholderPainter;
-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 java.util.List;
-
-public class CCDeleteAnswerPlaceholder extends CCAnswerPlaceholderAction {
-
-  public CCDeleteAnswerPlaceholder() {
-    super("Delete Answer Placeholder","Delete answer placeholder", null);
-  }
-
-  @Override
-  protected void performAnswerPlaceholderAction(@NotNull CCState state) {
-    Project project = state.getProject();
-    PsiFile psiFile = state.getFile();
-    final Document document = PsiDocumentManager.getInstance(project).getDocument(psiFile);
-    if (document == null) return;
-    TaskFile taskFile = state.getTaskFile();
-    AnswerPlaceholder answerPlaceholder = state.getAnswerPlaceholder();
-    final List<AnswerPlaceholder> answerPlaceholders = taskFile.getAnswerPlaceholders();
-    if (answerPlaceholders.contains(answerPlaceholder)) {
-      answerPlaceholders.remove(answerPlaceholder);
-      final Editor editor = state.getEditor();
-      editor.getMarkupModel().removeAllHighlighters();
-      CCProjectService.getInstance(project).drawAnswerPlaceholders(psiFile.getVirtualFile(), editor);
-      EduAnswerPlaceholderPainter.createGuardedBlocks(editor, taskFile, false);
-    }
-  }
-}
\ No newline at end of file
index b670ba1cb6c4d298ef20d7ee8cf8806e1e6ab773..34ef6f000899e2a78f5249bb2387e4a4e5e0d13c 100644 (file)
@@ -41,6 +41,8 @@ public class EduNames {
   public static final String STUDY = "Study";
   public static String PYCHARM_ADDITIONAL = "PyCharm additional materials";
   public static final String PYCHARM = "PyCharm";
+
+  public static String PLACEHOLDER = "Answer Placeholder";
   private EduNames() {
   }