allow lesson/task creation/modification in study projects with course creator mode
authorliana.bakradze <liana.bakradze@jetbrains.com>
Wed, 23 Mar 2016 15:02:47 +0000 (18:02 +0300)
committerliana.bakradze <liana.bakradze@jetbrains.com>
Wed, 23 Mar 2016 15:02:47 +0000 (18:02 +0300)
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCCreateStudyItemActionBase.java
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCCreateTask.java
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/handlers/CCLessonMoveHandlerDelegate.java [moved from python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCLessonMoveHandlerDelegate.java with 94% similarity]
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/handlers/CCLessonRenameHandler.java [moved from python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCLessonRenameHandler.java with 94% similarity]
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/handlers/CCRenameHandler.java [moved from python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCRenameHandler.java with 88% similarity]
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/handlers/CCTaskMoveHandlerDelegate.java [moved from python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCTaskMoveHandlerDelegate.java with 94% similarity]
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/handlers/CCTaskRenameHandler.java [moved from python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCTaskRenameHandler.java with 96% similarity]
python/educational-core/student/src/com/jetbrains/edu/learning/StudyMoveDelegate.java

index 520ce0961a6a13356c97dc421ffca64c3dc4af85..45310c7c0e8fe6959ec354846da78a94374f235a 100644 (file)
@@ -14,12 +14,13 @@ import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.psi.PsiDirectory;
 import com.intellij.util.Function;
+import com.jetbrains.edu.coursecreator.CCUtils;
+import com.jetbrains.edu.coursecreator.ui.CCCreateStudyItemDialog;
+import com.jetbrains.edu.learning.StudyTaskManager;
+import com.jetbrains.edu.learning.StudyUtils;
 import com.jetbrains.edu.learning.core.EduUtils;
 import com.jetbrains.edu.learning.courseFormat.Course;
 import com.jetbrains.edu.learning.courseFormat.StudyItem;
-import com.jetbrains.edu.coursecreator.CCProjectService;
-import com.jetbrains.edu.coursecreator.CCUtils;
-import com.jetbrains.edu.coursecreator.ui.CCCreateStudyItemDialog;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
@@ -33,18 +34,16 @@ public abstract class CCCreateStudyItemActionBase extends DumbAwareAction {
     super(text, description, icon);
   }
 
-
   @Override
   public void actionPerformed(AnActionEvent e) {
     final IdeView view = e.getData(LangDataKeys.IDE_VIEW);
-    final Project project = e.getData(CommonDataKeys.PROJECT);
+    final Project project = e.getProject();
     if (view == null || project == null) {
       return;
     }
     final PsiDirectory directory = DirectoryChooserUtil.getOrChooseDirectory(view);
     if (directory == null) return;
-    final CCProjectService service = CCProjectService.getInstance(project);
-    final Course course = service.getCourse();
+    final Course course = StudyTaskManager.getInstance(project).getCourse();
     if (course == null) {
       return;
     }
@@ -54,47 +53,45 @@ public abstract class CCCreateStudyItemActionBase extends DumbAwareAction {
 
   @Override
   public void update(@NotNull AnActionEvent event) {
-    if (!CCProjectService.setCCActionAvailable(event)) {
-      return;
-    }
     final Presentation presentation = event.getPresentation();
+    presentation.setEnabledAndVisible(false);
     final Project project = event.getData(CommonDataKeys.PROJECT);
     final IdeView view = event.getData(LangDataKeys.IDE_VIEW);
     if (project == null || view == null) {
       presentation.setEnabledAndVisible(false);
       return;
     }
+    if (!StudyUtils.isStudyProject(project)) {
+      return;
+    }
     final PsiDirectory[] directories = view.getDirectories();
     if (directories.length == 0) {
-      presentation.setEnabledAndVisible(false);
       return;
     }
     final PsiDirectory sourceDirectory = DirectoryChooserUtil.getOrChooseDirectory(view);
-    final CCProjectService service = CCProjectService.getInstance(project);
-    final Course course = service.getCourse();
+    final Course course = StudyTaskManager.getInstance(project).getCourse();
     if (course == null || sourceDirectory == null) {
-      presentation.setEnabledAndVisible(false);
       return;
     }
-    if (!isAddedAsLast(sourceDirectory, project, course) &&
-        getThresholdItem(course, sourceDirectory) == null) {
-      presentation.setEnabledAndVisible(false);
+    if (!isAddedAsLast(sourceDirectory, project, course) && getThresholdItem(course, sourceDirectory) == null) {
+      return;
     }
     if (CommonDataKeys.PSI_FILE.getData(event.getDataContext()) != null) {
-      presentation.setEnabledAndVisible(false);
+      return;
     }
+    presentation.setEnabledAndVisible(true);
   }
 
 
   @Nullable
   protected abstract PsiDirectory getParentDir(@NotNull final Project project,
-                                          @NotNull final Course course,
-                                          @NotNull final PsiDirectory directory);
+                                               @NotNull final Course course,
+                                               @NotNull final PsiDirectory directory);
 
 
   @Nullable
   public PsiDirectory createItem(@Nullable final IdeView view, @NotNull final Project project,
-                                             @NotNull final PsiDirectory sourceDirectory, @NotNull final Course course) {
+                                 @NotNull final PsiDirectory sourceDirectory, @NotNull final Course course) {
     StudyItem parentItem = getParentItem(course, sourceDirectory);
     final StudyItem item = getItem(sourceDirectory, project, course, view, parentItem);
     if (item == null) {
index 316b06d77c04f21103fac627f346f306ed033c1a..0c053ab57c0bfd47a04b58109562e5d5d74580cf 100644 (file)
@@ -9,19 +9,20 @@ import com.intellij.ide.util.EditorHelper;
 import com.intellij.openapi.application.ApplicationManager;
 import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.project.Project;
+import com.intellij.openapi.util.Ref;
 import com.intellij.openapi.util.io.FileUtil;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.psi.PsiDirectory;
 import com.intellij.psi.PsiElement;
 import com.intellij.util.Function;
+import com.jetbrains.edu.coursecreator.CCLanguageManager;
+import com.jetbrains.edu.coursecreator.CCUtils;
 import com.jetbrains.edu.learning.core.EduNames;
 import com.jetbrains.edu.learning.core.EduUtils;
 import com.jetbrains.edu.learning.courseFormat.Course;
 import com.jetbrains.edu.learning.courseFormat.Lesson;
 import com.jetbrains.edu.learning.courseFormat.StudyItem;
 import com.jetbrains.edu.learning.courseFormat.Task;
-import com.jetbrains.edu.coursecreator.CCLanguageManager;
-import com.jetbrains.edu.coursecreator.CCUtils;
 import icons.InteractiveLearningIcons;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
@@ -38,15 +39,15 @@ public class CCCreateTask extends CCCreateStudyItemActionBase {
   }
 
 
-  private static void createFromTemplateAndOpen(@NotNull final PsiDirectory taskDirectory,
-                                                @Nullable final FileTemplate template,
-                                                @Nullable IdeView view) {
+  private static void createFromTemplate(@NotNull final PsiDirectory taskDirectory,
+                                         @Nullable final FileTemplate template,
+                                         @Nullable IdeView view, boolean open) {
     if (template == null) {
       return;
     }
     try {
       final PsiElement file = FileTemplateUtil.createFromTemplate(template, template.getName(), null, taskDirectory);
-      if (view != null) {
+      if (view != null && open) {
         EditorHelper.openInEditor(file, false);
         view.selectElement(file);
       }
@@ -91,22 +92,24 @@ public class CCCreateTask extends CCCreateStudyItemActionBase {
   protected PsiDirectory createItemDir(@NotNull final Project project, @NotNull final StudyItem item,
                                      @Nullable final IdeView view, @NotNull final PsiDirectory parentDirectory,
                                      @NotNull final Course course) {
-    final PsiDirectory[] taskDirectory = new PsiDirectory[1];
+
+    final Ref<PsiDirectory> taskDirectory = new Ref<>();
     ApplicationManager.getApplication().runWriteAction(new Runnable() {
       @Override
       public void run() {
-        taskDirectory[0] = DirectoryUtil.createSubdirectories(EduNames.TASK + item.getIndex(), parentDirectory, "\\/");
-        if (taskDirectory[0] != null) {
+        String taskDirName = EduNames.TASK + item.getIndex();
+        taskDirectory.set(DirectoryUtil.createSubdirectories(taskDirName, parentDirectory, "\\/"));
+        if (taskDirectory.get() != null) {
           CCLanguageManager manager = CCUtils.getStudyLanguageManager(course);
           if (manager == null) {
             return;
           }
-          createFromTemplateAndOpen(taskDirectory[0], manager.getTestsTemplate(project), view);
-          createFromTemplateAndOpen(taskDirectory[0], FileTemplateManager.getInstance(project).getInternalTemplate("task.html"), view);
+          createFromTemplate(taskDirectory.get(), manager.getTestsTemplate(project), view, false);
+          createFromTemplate(taskDirectory.get(), FileTemplateManager.getInstance(project).getInternalTemplate(EduNames.TASK_HTML), view, false);
           String defaultExtension = manager.getDefaultTaskFileExtension();
           if (defaultExtension != null) {
             FileTemplate taskFileTemplate = manager.getTaskFileTemplateForExtension(project, defaultExtension);
-            createFromTemplateAndOpen(taskDirectory[0], taskFileTemplate, view);
+            createFromTemplate(taskDirectory.get(), taskFileTemplate, view, true);
             if (taskFileTemplate != null) {
               String taskFileName = FileUtil.getNameWithoutExtension(taskFileTemplate.getName());
               ((Task)item).addTaskFile(taskFileName + "." + defaultExtension, 1);
@@ -115,7 +118,7 @@ public class CCCreateTask extends CCCreateStudyItemActionBase {
         }
       }
     });
-    return taskDirectory[0];
+    return taskDirectory.get();
   }
 
   @Override
similarity index 94%
rename from python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCLessonMoveHandlerDelegate.java
rename to python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/handlers/CCLessonMoveHandlerDelegate.java
index 2a1cf8ca77332b243cb08a6dbee2f3d797229b90..ee6a92fa5e0ec459a9862d66114c1ae4cb2b00f0 100644 (file)
@@ -1,4 +1,4 @@
-package com.jetbrains.edu.coursecreator;
+package com.jetbrains.edu.coursecreator.handlers;
 
 import com.intellij.ide.IdeView;
 import com.intellij.ide.util.DirectoryChooserUtil;
@@ -18,12 +18,14 @@ import com.intellij.psi.PsiReference;
 import com.intellij.refactoring.move.MoveCallback;
 import com.intellij.refactoring.move.MoveHandlerDelegate;
 import com.intellij.util.Function;
+import com.jetbrains.edu.coursecreator.CCUtils;
+import com.jetbrains.edu.coursecreator.ui.CCMoveStudyItemDialog;
+import com.jetbrains.edu.learning.StudyTaskManager;
 import com.jetbrains.edu.learning.core.EduNames;
 import com.jetbrains.edu.learning.core.EduUtils;
 import com.jetbrains.edu.learning.courseFormat.Course;
 import com.jetbrains.edu.learning.courseFormat.Lesson;
 import com.jetbrains.edu.learning.courseFormat.StudyItem;
-import com.jetbrains.edu.coursecreator.ui.CCMoveStudyItemDialog;
 import org.jetbrains.annotations.Nullable;
 
 import java.io.IOException;
@@ -67,7 +69,10 @@ public class CCLessonMoveHandlerDelegate extends MoveHandlerDelegate {
     if (targetContainer == null || !(targetContainer instanceof PsiDirectory)) {
       return;
     }
-    final Course course = CCProjectService.getInstance(project).getCourse();
+    final Course course = StudyTaskManager.getInstance(project).getCourse();
+    if (course == null) {
+      return;
+    }
     final PsiDirectory sourceDirectory = (PsiDirectory)elements[0];
     final Lesson sourceLesson = course.getLesson(sourceDirectory.getName());
     final Lesson targetLesson = course.getLesson(((PsiDirectory)targetContainer).getName());
similarity index 88%
rename from python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCRenameHandler.java
rename to python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/handlers/CCRenameHandler.java
index 27bd42ca248425a3b2278ae6453a0208bf4463ee..e98ba1ab133f27b4b989880324fba8666271b6c5 100644 (file)
@@ -1,4 +1,4 @@
-package com.jetbrains.edu.coursecreator;
+package com.jetbrains.edu.coursecreator.handlers;
 
 import com.intellij.ide.projectView.ProjectView;
 import com.intellij.openapi.actionSystem.CommonDataKeys;
@@ -12,6 +12,8 @@ import com.intellij.psi.PsiDirectory;
 import com.intellij.psi.PsiElement;
 import com.intellij.psi.PsiFile;
 import com.intellij.refactoring.rename.RenameHandler;
+import com.jetbrains.edu.coursecreator.CCUtils;
+import com.jetbrains.edu.learning.StudyTaskManager;
 import com.jetbrains.edu.learning.courseFormat.Course;
 import com.jetbrains.edu.learning.courseFormat.StudyItem;
 import org.jetbrains.annotations.NotNull;
@@ -23,9 +25,7 @@ public abstract class CCRenameHandler implements RenameHandler {
     if (element == null || !(element instanceof PsiDirectory)) {
       return false;
     }
-    CCProjectService instance = CCProjectService.getInstance(element.getProject());
-    Course course = instance.getCourse();
-    if (course == null) {
+    if (!CCUtils.isCourseCreator(element.getProject())) {
       return false;
     }
     VirtualFile directory = ((PsiDirectory)element).getVirtualFile();
@@ -44,8 +44,10 @@ public abstract class CCRenameHandler implements RenameHandler {
     PsiElement element = CommonDataKeys.PSI_ELEMENT.getData(dataContext);
     assert element != null;
     PsiDirectory directory = (PsiDirectory)element;
-    CCProjectService instance = CCProjectService.getInstance(project);
-    Course course = instance.getCourse();
+    Course course = StudyTaskManager.getInstance(project).getCourse();
+    if (course == null) {
+      return;
+    }
     rename(project, course, directory);
     ProjectView.getInstance(project).refresh();
   }
similarity index 94%
rename from python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCTaskMoveHandlerDelegate.java
rename to python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/handlers/CCTaskMoveHandlerDelegate.java
index 37cd620bc2e6dccf9e53786777d8a227cd027c67..a132e408b85f1c768ba8a47185dded30318c3f92 100644 (file)
@@ -1,4 +1,4 @@
-package com.jetbrains.edu.coursecreator;
+package com.jetbrains.edu.coursecreator.handlers;
 
 import com.intellij.ide.IdeView;
 import com.intellij.ide.util.DirectoryChooserUtil;
@@ -18,13 +18,15 @@ import com.intellij.psi.PsiReference;
 import com.intellij.refactoring.move.MoveCallback;
 import com.intellij.refactoring.move.MoveHandlerDelegate;
 import com.intellij.util.Function;
+import com.jetbrains.edu.coursecreator.CCUtils;
+import com.jetbrains.edu.coursecreator.ui.CCMoveStudyItemDialog;
+import com.jetbrains.edu.learning.StudyTaskManager;
 import com.jetbrains.edu.learning.core.EduNames;
 import com.jetbrains.edu.learning.core.EduUtils;
 import com.jetbrains.edu.learning.courseFormat.Course;
 import com.jetbrains.edu.learning.courseFormat.Lesson;
 import com.jetbrains.edu.learning.courseFormat.StudyItem;
 import com.jetbrains.edu.learning.courseFormat.Task;
-import com.jetbrains.edu.coursecreator.ui.CCMoveStudyItemDialog;
 import org.jetbrains.annotations.Nullable;
 
 import java.io.IOException;
@@ -59,9 +61,9 @@ public class CCTaskMoveHandlerDelegate extends MoveHandlerDelegate {
     if (sourceDirectory == null) {
       return false;
     }
-    CCProjectService service = CCProjectService.getInstance(sourceDirectory.getProject());
-    Course course = service.getCourse();
-    if (course == null) {
+    Project project = sourceDirectory.getProject();
+    Course course = StudyTaskManager.getInstance(project).getCourse();
+    if (course == null || !CCUtils.isCourseCreator(project)) {
       return false;
     }
     return EduUtils.getTask(sourceDirectory, course) != null;
@@ -86,7 +88,7 @@ public class CCTaskMoveHandlerDelegate extends MoveHandlerDelegate {
       Messages.showInfoMessage("Tasks can be moved only to other lessons or inside lesson", "Incorrect Target For Move");
       return;
     }
-    final Course course = CCProjectService.getInstance(project).getCourse();
+    final Course course = StudyTaskManager.getInstance(project).getCourse();
     final PsiDirectory sourceDirectory = (PsiDirectory)elements[0];
 
     final Task taskToMove = EduUtils.getTask(sourceDirectory, course);
index e21bb4007af4ac767888d1f5863d0f6f5438700b..d7ff54c0560b992c02981871fe39beca1480ba29 100644 (file)
@@ -8,6 +8,7 @@ import com.intellij.psi.PsiElement;
 import com.intellij.psi.PsiReference;
 import com.intellij.refactoring.move.MoveCallback;
 import com.intellij.refactoring.move.MoveHandlerDelegate;
+import com.jetbrains.edu.learning.core.EduNames;
 import com.jetbrains.edu.learning.courseFormat.Course;
 import org.jetbrains.annotations.Nullable;
 
@@ -22,7 +23,7 @@ public class StudyMoveDelegate extends MoveHandlerDelegate{
     if (elements.length == 1) {
       Project project = elements[0].getProject();
       Course course = StudyTaskManager.getInstance(project).getCourse();
-      if (course == null) {
+      if (course == null || !EduNames.STUDY.equals(course.getCourseMode())) {
         return false;
       }
       return !StudyUtils.isRenameableOrMoveable(project, course, elements[0]);