part of EDU-331(improving lesson and task management)
authorLiana Bakradze <liana.bakradze@jetbrains.com>
Wed, 21 Jan 2015 15:32:02 +0000 (18:32 +0300)
committerLiana Bakradze <liana.bakradze@jetbrains.com>
Wed, 21 Jan 2015 15:32:02 +0000 (18:32 +0300)
added ability to create lesson in the middle of course

python/educational/course-creator/src/com/jetbrains/edu/coursecreator/CCProjectGeneratorUtil.java
python/educational/course-creator/src/com/jetbrains/edu/coursecreator/CCProjectService.java
python/educational/course-creator/src/com/jetbrains/edu/coursecreator/CCUtils.java
python/educational/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCCreateLesson.java
python/educational/course-creator/src/com/jetbrains/edu/coursecreator/projectView/CCDirectoryNode.java

index 2d7d03f1dc9e0ea6b4509c5e573aaa36c8d92245..3f0d977a900aa315327d51d42845e92ed39457ff 100644 (file)
@@ -39,7 +39,7 @@ public class CCProjectGeneratorUtil {
         catch (Exception ignored) {
         }
         DirectoryUtil.createSubdirectories("hints", projectDir, "\\/");
-        final PsiDirectory lessonDir = CCCreateLesson.createLesson(projectDir, 1, null, null, course);
+        final PsiDirectory lessonDir = CCCreateLesson.createLessonDir(projectDir, 1, null, null, course);
         CCCreateTask.createTask(null, project, lessonDir, false);
       }
     }.execute();
index 028e449f6fdadbc443437edb5797e5362fcaec8d..908ab17bc88625391d0659609cd462bea4005c81 100644 (file)
@@ -17,7 +17,6 @@ package com.jetbrains.edu.coursecreator;
 
 import com.intellij.ide.projectView.ProjectView;
 import com.intellij.openapi.actionSystem.AnActionEvent;
-import com.intellij.openapi.actionSystem.Presentation;
 import com.intellij.openapi.components.PersistentStateComponent;
 import com.intellij.openapi.components.ServiceManager;
 import com.intellij.openapi.components.State;
@@ -193,18 +192,15 @@ public class CCProjectService implements PersistentStateComponent<Element> {
   }
 
   public static boolean setCCActionAvailable(@NotNull AnActionEvent e) {
-    final Presentation presentation = e.getPresentation();
     final Project project = e.getProject();
     if (project == null) {
       return false;
     }
     if (getInstance(project).getCourse() == null) {
-      presentation.setVisible(false);
-      presentation.setEnabled(false);
+      CCUtils.enableAction(e, false);
       return false;
     }
-    presentation.setEnabled(true);
-    presentation.setVisible(true);
+    CCUtils.enableAction(e, true);
     return true;
   }
 }
index 130952e8ebaa6343cb53bd56ba82873a48824ac6..1e82811a6d8d034fe56abd30cc82e04e02ff30ca 100644 (file)
@@ -1,6 +1,8 @@
 package com.jetbrains.edu.coursecreator;
 
 import com.intellij.ide.projectView.actions.MarkRootActionBase;
+import com.intellij.openapi.actionSystem.AnActionEvent;
+import com.intellij.openapi.actionSystem.Presentation;
 import com.intellij.openapi.application.ApplicationManager;
 import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.module.Module;
@@ -36,4 +38,22 @@ public class CCUtils {
       }
     });
   }
+
+  public static void enableAction(@NotNull final AnActionEvent event, boolean isEnable) {
+    final Presentation presentation = event.getPresentation();
+    presentation.setVisible(isEnable);
+    presentation.setEnabled(isEnable);
+  }
+
+  public static int getIndex(@NotNull final String fullName, @NotNull final String logicalName) {
+    if (!fullName.startsWith(logicalName)) {
+      throw new IllegalArgumentException();
+    }
+    try {
+      return Integer.parseInt(fullName.substring(logicalName.length()));
+    }
+    catch (NumberFormatException e) {
+      return -1;
+    }
+  }
 }
index a92bdd7a87fbbebfe0f1daf4de6b45039d45ff6b..0866d4e206c4b9a5a29873cf7a412d08ac232eb3 100644 (file)
@@ -6,20 +6,27 @@ import com.intellij.ide.util.DirectoryUtil;
 import com.intellij.openapi.actionSystem.AnActionEvent;
 import com.intellij.openapi.actionSystem.CommonDataKeys;
 import com.intellij.openapi.actionSystem.LangDataKeys;
-import com.intellij.openapi.actionSystem.Presentation;
 import com.intellij.openapi.application.ApplicationManager;
+import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.project.DumbAwareAction;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.ui.Messages;
+import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.psi.PsiDirectory;
 import com.intellij.util.PlatformIcons;
 import com.jetbrains.edu.coursecreator.CCProjectService;
+import com.jetbrains.edu.coursecreator.CCUtils;
 import com.jetbrains.edu.coursecreator.format.Course;
 import com.jetbrains.edu.coursecreator.format.Lesson;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
+import java.io.IOException;
+import java.util.List;
+
 public class CCCreateLesson extends DumbAwareAction {
+  private static final Logger LOG = Logger.getInstance(CCCreateLesson.class.getName());
+
   public CCCreateLesson() {
     super("Lesson", "Create new Lesson", PlatformIcons.DIRECTORY_CLOSED_ICON);
   }
@@ -28,30 +35,85 @@ public class CCCreateLesson extends DumbAwareAction {
   public void actionPerformed(AnActionEvent e) {
     final IdeView view = e.getData(LangDataKeys.IDE_VIEW);
     final Project project = e.getData(CommonDataKeys.PROJECT);
-
     if (view == null || project == null) {
       return;
     }
     final PsiDirectory directory = DirectoryChooserUtil.getOrChooseDirectory(view);
     if (directory == null) return;
-
+    final String lessonDirName = "lesson";
     final CCProjectService service = CCProjectService.getInstance(project);
     final Course course = service.getCourse();
-    final int size = course.getLessons().size();
-    final String lessonName = Messages.showInputDialog("Name:", "Lesson Name", null, "lesson" + (size+1), null);
-    if (lessonName == null) return;
+    if (course == null) {
+      return;
+    }
+    //"Create Lesson" invoked from project root creates new lesson as last lesson
+    if (directory.getVirtualFile().equals(project.getBaseDir())) {
+      final int size = course.getLessons().size();
+      createLesson(directory, size + 1, view, course);
+      return;
+    }
+    //"Create Lesson" invoked from any of lesson directories creates new lesson as next lesson
+    Lesson lesson = course.getLesson(directory.getName());
+    if (lesson != null) {
+      int index = lesson.getIndex();
+      List<Lesson> lessons = course.getLessons();
+      int lessonNum = lessons.size();
+      for (int i = lessonNum; i >= index + 1; i--) {
+        updateLesson(project, lessonDirName, course, i);
+      }
+      final PsiDirectory parent = directory.getParent();
+      if (parent == null) {
+        return;
+      }
+      createLesson(parent, index + 1, view, course);
+      course.init();
+    }
+  }
+
+  private static void createLesson(@NotNull final PsiDirectory projectDir,
+                                   final int index,
+                                   final IdeView view,
+                                   @NotNull final Course course) {
+    final String lessonName = Messages.showInputDialog("Name:", "Lesson Name", null, "lesson" + index, null);
+    if (lessonName == null) {
+      return;
+    }
+    ApplicationManager.getApplication().runWriteAction(new Runnable() {
+      @Override
+      public void run() {
+        createLessonDir(projectDir, index, lessonName, view, course);
+      }
+    });
+  }
 
+  private void updateLesson(@NotNull final Project project, final String lessonDirName, @NotNull final Course course, int i) {
+    final VirtualFile lessonDir = project.getBaseDir().findChild(lessonDirName + i);
+    if (lessonDir == null) {
+      return;
+    }
+    Lesson l = course.getLesson(lessonDir.getName());
+    if (l == null) {
+      return;
+    }
+    l.setIndex(l.getIndex() + 1);
+    final int next = i + 1;
     ApplicationManager.getApplication().runWriteAction(new Runnable() {
       @Override
       public void run() {
-        createLesson(directory, size + 1, lessonName, view, course);
+        try {
+          lessonDir.rename(this, lessonDirName + next);
+        }
+        catch (IOException e1) {
+          LOG.error(e1);
+        }
       }
     });
+    course.getLessonsMap().put(lessonDir.getName(), l);
   }
 
   @Nullable
-  public static PsiDirectory createLesson(@NotNull final  PsiDirectory projectDir, int index, String name, final IdeView view,
-                                  @NotNull final Course course) {
+  public static PsiDirectory createLessonDir(@NotNull final PsiDirectory projectDir, int index, String name, final IdeView view,
+                                             @NotNull final Course course) {
     String lessonFolderName = "lesson" + index;
     final PsiDirectory lessonDirectory = DirectoryUtil.createSubdirectories("lesson" + index, projectDir, "\\/");
     if (lessonDirectory != null) {
@@ -70,35 +132,27 @@ public class CCCreateLesson extends DumbAwareAction {
     if (!CCProjectService.setCCActionAvailable(event)) {
       return;
     }
-    final Presentation presentation = event.getPresentation();
     final Project project = event.getData(CommonDataKeys.PROJECT);
-    if (project == null) {
-      presentation.setVisible(false);
-      presentation.setEnabled(false);
-      return;
-    }
-
     final IdeView view = event.getData(LangDataKeys.IDE_VIEW);
-    if (view == null) {
-      presentation.setVisible(false);
-      presentation.setEnabled(false);
+    if (project == null || view == null) {
+      CCUtils.enableAction(event, false);
       return;
     }
-
     final PsiDirectory[] directories = view.getDirectories();
     if (directories.length == 0) {
-      presentation.setVisible(false);
-      presentation.setEnabled(false);
+      CCUtils.enableAction(event, false);
       return;
     }
     final PsiDirectory directory = DirectoryChooserUtil.getOrChooseDirectory(view);
-    if (directory != null && !project.getBaseDir().equals(directory.getVirtualFile())) {
-      presentation.setVisible(false);
-      presentation.setEnabled(false);
+    if (directory != null && project.getBaseDir().equals(directory.getVirtualFile())) {
+      CCUtils.enableAction(event, true);
       return;
     }
-    presentation.setVisible(true);
-    presentation.setEnabled(true);
-
+    Course course = CCProjectService.getInstance(project).getCourse();
+    if (directory != null && course != null && course.getLesson(directory.getName()) != null) {
+      CCUtils.enableAction(event, true);
+      return;
+    }
+    CCUtils.enableAction(event, false);
   }
 }
\ No newline at end of file
index d6c3ef697dcc7c522b4126eda8b2418eeded1e28..ad6e9bae9149d233c93854f437ea3735117661eb 100644 (file)
@@ -7,6 +7,7 @@ import com.intellij.openapi.project.Project;
 import com.intellij.psi.PsiDirectory;
 import com.intellij.ui.SimpleTextAttributes;
 import com.jetbrains.edu.coursecreator.CCProjectService;
+import com.jetbrains.edu.coursecreator.CCUtils;
 import com.jetbrains.edu.coursecreator.format.Course;
 import com.jetbrains.edu.coursecreator.format.Lesson;
 import com.jetbrains.edu.coursecreator.format.Task;
@@ -62,18 +63,6 @@ public class CCDirectoryNode extends PsiDirectoryNode {
     data.setPresentableText(valueName);
   }
 
-  private static int getIndex(@NotNull final String fullName, @NotNull final String logicalName) {
-    if (!fullName.startsWith(logicalName)) {
-      throw new IllegalArgumentException();
-    }
-    try {
-      return Integer.parseInt(fullName.substring(logicalName.length())) - 1;
-    }
-    catch (NumberFormatException e) {
-      return -1;
-    }
-  }
-
   @Override
   public int getTypeSortWeight(boolean sortByType) {
     String name = myValue.getName();
@@ -81,7 +70,7 @@ public class CCDirectoryNode extends PsiDirectoryNode {
     String taskDirName = "task";
     if (name.startsWith(lessonDirName) || name.startsWith(taskDirName)) {
       String logicalName = name.contains(lessonDirName) ? lessonDirName : taskDirName;
-      int index = getIndex(name, logicalName) + 1;
+      int index = CCUtils.getIndex(name, logicalName) + 1;
       return index != -1 ? index : 0;
     }
     return 0;