extract task and lesson rename handlers
authorLiana Bakradze <liana.bakradze@jetbrains.com>
Mon, 29 Feb 2016 16:07:03 +0000 (19:07 +0300)
committerLiana Bakradze <liana.bakradze@jetbrains.com>
Mon, 29 Feb 2016 16:09:25 +0000 (19:09 +0300)
python/educational-core/course-creator/resources/META-INF/plugin.xml
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCLessonRenameHandler.java [new file with mode: 0644]
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCRenameHandler.java
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCTaskRenameHandler.java [new file with mode: 0644]

index 193cd710f76952f62e28adb6f82ea5faf8480130..3480859e7905142953badd24f0f2e4a789917d4a 100644 (file)
     <projectService serviceImplementation="com.jetbrains.edu.coursecreator.CCProjectService"/>
     <treeStructureProvider implementation="com.jetbrains.edu.coursecreator.projectView.CCTreeStructureProvider"/>
     <refactoring.elementListenerProvider implementation="com.jetbrains.edu.coursecreator.CCRefactoringElementListenerProvider"/>
-    <renameHandler implementation="com.jetbrains.edu.coursecreator.CCRenameHandler"/>
     <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"/>
   </extensions>
 
   <application-components>
diff --git a/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCLessonRenameHandler.java b/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCLessonRenameHandler.java
new file mode 100644 (file)
index 0000000..52affce
--- /dev/null
@@ -0,0 +1,29 @@
+package com.jetbrains.edu.coursecreator;
+
+import com.intellij.ide.TitledHandler;
+import com.intellij.openapi.project.Project;
+import com.intellij.psi.PsiDirectory;
+import com.jetbrains.edu.EduNames;
+import com.jetbrains.edu.courseFormat.Course;
+import com.jetbrains.edu.courseFormat.Lesson;
+import org.jetbrains.annotations.NotNull;
+
+public class CCLessonRenameHandler extends CCRenameHandler implements TitledHandler {
+  @Override
+  protected boolean isAvailable(String name) {
+    return name.contains(EduNames.LESSON);
+  }
+
+  @Override
+  protected void rename(@NotNull Project project, @NotNull Course course, @NotNull PsiDirectory directory) {
+    Lesson lesson = course.getLesson(directory.getName());
+    if (lesson != null) {
+      processRename(lesson, EduNames.LESSON, project);
+    }
+  }
+
+  @Override
+  public String getActionTitle() {
+    return "Rename lesson";
+  }
+}
index cc8866900169f8ad88a54b89ae10dd5f3b192ea8..99194c20744d7719e89a73fb0b43c904c9652c57 100644 (file)
@@ -12,14 +12,11 @@ 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.EduNames;
 import com.jetbrains.edu.courseFormat.Course;
-import com.jetbrains.edu.courseFormat.Lesson;
 import com.jetbrains.edu.courseFormat.StudyItem;
-import com.jetbrains.edu.courseFormat.Task;
 import org.jetbrains.annotations.NotNull;
 
-public class CCRenameHandler implements RenameHandler {
+public abstract class CCRenameHandler implements RenameHandler {
   @Override
   public boolean isAvailableOnDataContext(DataContext dataContext) {
     PsiElement element = CommonDataKeys.PSI_ELEMENT.getData(dataContext);
@@ -32,10 +29,11 @@ public class CCRenameHandler implements RenameHandler {
       return false;
     }
     VirtualFile directory = ((PsiDirectory)element).getVirtualFile();
-    String name = directory.getName();
-    return name.contains(EduNames.LESSON) || name.contains(EduNames.TASK);
+    return isAvailable(directory.getName());
   }
 
+  protected abstract boolean isAvailable(String name);
+
   @Override
   public boolean isRenaming(DataContext dataContext) {
     return isAvailableOnDataContext(dataContext);
@@ -46,20 +44,16 @@ public class CCRenameHandler implements RenameHandler {
     PsiElement element = CommonDataKeys.PSI_ELEMENT.getData(dataContext);
     assert element != null;
     PsiDirectory directory = (PsiDirectory)element;
-    String name = directory.getName();
     CCProjectService instance = CCProjectService.getInstance(project);
     Course course = instance.getCourse();
-    if (name.contains(EduNames.LESSON)) {
-      renameLesson(project, course, name);
-    }
-    if (name.contains(EduNames.TASK)) {
-      renameTask(project, course, directory);
-    }
+    rename(project, course, directory);
     ProjectView.getInstance(project).refresh();
   }
 
+  protected abstract void rename(@NotNull Project project, @NotNull Course course, @NotNull PsiDirectory directory);
 
-  private static void processRename(@NotNull final StudyItem item, String namePrefix, @NotNull final Project project) {
+
+  protected static void processRename(@NotNull final StudyItem item, String namePrefix, @NotNull final Project project) {
     String name = item.getName();
     String text = "Rename " + StringUtil.toTitleCase(namePrefix);
     String newName = Messages.showInputDialog(project, text + " '" + name + "' to", text, null, name, null);
@@ -68,29 +62,6 @@ public class CCRenameHandler implements RenameHandler {
     }
   }
 
-  private static void renameTask(@NotNull final Project project, Course course, @NotNull final PsiDirectory directory) {
-    PsiDirectory lessonDir = directory.getParent();
-    if (lessonDir == null || !lessonDir.getName().contains(EduNames.LESSON)) {
-      return;
-    }
-    Lesson lesson = course.getLesson(lessonDir.getName());
-    if (lesson == null) {
-      return;
-    }
-    String directoryName = directory.getName();
-    Task task = lesson.getTask(directoryName);
-    if (task != null) {
-      processRename(task, EduNames.TASK, project);
-    }
-  }
-
-  private static void renameLesson(@NotNull final Project project, Course course, String name) {
-    Lesson lesson = course.getLesson(name);
-    if (lesson != null) {
-      processRename(lesson, EduNames.LESSON, project);
-    }
-  }
-
   @Override
   public void invoke(@NotNull Project project, @NotNull PsiElement[] elements, DataContext dataContext) {
     invoke(project, null, null, dataContext);
diff --git a/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCTaskRenameHandler.java b/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCTaskRenameHandler.java
new file mode 100644 (file)
index 0000000..23ebfba
--- /dev/null
@@ -0,0 +1,39 @@
+package com.jetbrains.edu.coursecreator;
+
+import com.intellij.ide.TitledHandler;
+import com.intellij.openapi.project.Project;
+import com.intellij.psi.PsiDirectory;
+import com.jetbrains.edu.EduNames;
+import com.jetbrains.edu.courseFormat.Course;
+import com.jetbrains.edu.courseFormat.Lesson;
+import com.jetbrains.edu.courseFormat.Task;
+import org.jetbrains.annotations.NotNull;
+
+public class CCTaskRenameHandler extends CCRenameHandler implements TitledHandler {
+  @Override
+  protected boolean isAvailable(String name) {
+    return name.contains(EduNames.TASK);
+  }
+
+  @Override
+  protected void rename(@NotNull Project project, @NotNull Course course, @NotNull PsiDirectory directory) {
+    PsiDirectory lessonDir = directory.getParent();
+    if (lessonDir == null || !lessonDir.getName().contains(EduNames.LESSON)) {
+      return;
+    }
+    Lesson lesson = course.getLesson(lessonDir.getName());
+    if (lesson == null) {
+      return;
+    }
+    String directoryName = directory.getName();
+    Task task = lesson.getTask(directoryName);
+    if (task != null) {
+      processRename(task, EduNames.TASK, project);
+    }
+  }
+
+  @Override
+  public String getActionTitle() {
+    return "Rename task";
+  }
+}