added validation to answer files rename (part of EDU-341)
authorliana.bakradze <liana.bakradze@jetbrains.com>
Sun, 26 Jul 2015 15:05:48 +0000 (18:05 +0300)
committerliana.bakradze <liana.bakradze@jetbrains.com>
Sun, 26 Jul 2015 15:05:48 +0000 (18:05 +0300)
python/educational/course-creator/resources/META-INF/plugin.xml
python/educational/course-creator/src/com/jetbrains/edu/coursecreator/CCProjectService.java
python/educational/course-creator/src/com/jetbrains/edu/coursecreator/CCRenameInputValidator.java [new file with mode: 0644]
python/educational/course-creator/src/com/jetbrains/edu/coursecreator/CCUtils.java

index 80390da031a0be3300d9ba1f955bc3c43b1152e3..dd10f386e674016718f1d0db35ccc4c42c827cc0 100644 (file)
@@ -25,6 +25,7 @@
     <refactoring.elementListenerProvider implementation="com.jetbrains.edu.coursecreator.CCRefactoringElementListenerProvider"/>
     <errorHandler implementation="com.intellij.diagnostic.ITNReporter"/>
     <renameHandler implementation="com.jetbrains.edu.coursecreator.CCRenameHandler"/>
     <refactoring.elementListenerProvider implementation="com.jetbrains.edu.coursecreator.CCRefactoringElementListenerProvider"/>
     <errorHandler implementation="com.intellij.diagnostic.ITNReporter"/>
     <renameHandler implementation="com.jetbrains.edu.coursecreator.CCRenameHandler"/>
+    <renameInputValidator implementation="com.jetbrains.edu.coursecreator.CCRenameInputValidator"/>
   </extensions>
 
   <application-components>
   </extensions>
 
   <application-components>
index 4d2081b369d4253325c78449253cf8b67d412c5e..02e061803ab8dacb0182f3561277c4a7097e4dce 100644 (file)
@@ -103,9 +103,10 @@ public class CCProjectService implements PersistentStateComponent<CCProjectServi
     myDocumentListeners.remove(document);
   }
 
     myDocumentListeners.remove(document);
   }
 
-  public boolean isTaskFile(VirtualFile file) {
+  @Nullable
+  public Task getTask(VirtualFile file) {
     if (myCourse == null || file == null) {
     if (myCourse == null || file == null) {
-      return false;
+      return null;
     }
     VirtualFile taskDir = file.getParent();
     if (taskDir != null) {
     }
     VirtualFile taskDir = file.getParent();
     if (taskDir != null) {
@@ -117,20 +118,30 @@ public class CCProjectService implements PersistentStateComponent<CCProjectServi
           int lessonIndex = EduUtils.getIndex(lessonDirName, EduNames.LESSON);
           List<Lesson> lessons = myCourse.getLessons();
           if (!EduUtils.indexIsValid(lessonIndex, lessons)) {
           int lessonIndex = EduUtils.getIndex(lessonDirName, EduNames.LESSON);
           List<Lesson> lessons = myCourse.getLessons();
           if (!EduUtils.indexIsValid(lessonIndex, lessons)) {
-            return false;
+            return null;
           }
           Lesson lesson = lessons.get(lessonIndex);
           int taskIndex = EduUtils.getIndex(taskDirName, EduNames.TASK);
           List<Task> tasks = lesson.getTaskList();
           if (!EduUtils.indexIsValid(taskIndex, tasks)) {
           }
           Lesson lesson = lessons.get(lessonIndex);
           int taskIndex = EduUtils.getIndex(taskDirName, EduNames.TASK);
           List<Task> tasks = lesson.getTaskList();
           if (!EduUtils.indexIsValid(taskIndex, tasks)) {
-            return false;
+            return null;
           }
           }
-          Task task = tasks.get(taskIndex);
-          return task.isTaskFile(file.getName());
+          return tasks.get(taskIndex);
         }
       }
     }
         }
       }
     }
-    return false;
+    return null;
+  }
+
+  public boolean isAnswerFile(VirtualFile file) {
+    Task task = getTask(file);
+    String fileName = getRealTaskFileName(file.getName());
+    return task != null && fileName != null && task.isTaskFile(fileName);
+  }
+
+  public boolean isTaskFile(VirtualFile file) {
+    Task task = getTask(file);
+    return task != null && task.isTaskFile(file.getName());
   }
 
   @Nullable
   }
 
   @Nullable
@@ -179,5 +190,4 @@ public class CCProjectService implements PersistentStateComponent<CCProjectServi
   public static CCProjectService getInstance(@NotNull Project project) {
     return ServiceManager.getService(project, CCProjectService.class);
   }
   public static CCProjectService getInstance(@NotNull Project project) {
     return ServiceManager.getService(project, CCProjectService.class);
   }
-
 }
 }
diff --git a/python/educational/course-creator/src/com/jetbrains/edu/coursecreator/CCRenameInputValidator.java b/python/educational/course-creator/src/com/jetbrains/edu/coursecreator/CCRenameInputValidator.java
new file mode 100644 (file)
index 0000000..f6a8b54
--- /dev/null
@@ -0,0 +1,22 @@
+package com.jetbrains.edu.coursecreator;
+
+import com.intellij.patterns.ElementPattern;
+import com.intellij.patterns.PlatformPatterns;
+import com.intellij.psi.PsiElement;
+import com.intellij.refactoring.rename.RenameInputValidator;
+import com.intellij.util.ProcessingContext;
+
+public class CCRenameInputValidator implements RenameInputValidator {
+  @Override
+  public ElementPattern<? extends PsiElement> getPattern() {
+    return PlatformPatterns.psiFile();
+  }
+
+  @Override
+  public boolean isInputValid(String newName, PsiElement element, ProcessingContext context) {
+    if (!CCUtils.isAnswerFile(element)) {
+      return true;
+    }
+    return newName.contains(".answer.");
+  }
+}
index 12df80a347acd05e30c03fde9daafd9d05175868..2e46c635d8a89381805dac09ca14b137fc0efad7 100644 (file)
@@ -1,6 +1,9 @@
 package com.jetbrains.edu.coursecreator;
 
 import com.intellij.lang.Language;
 package com.jetbrains.edu.coursecreator;
 
 import com.intellij.lang.Language;
+import com.intellij.openapi.vfs.VirtualFile;
+import com.intellij.psi.PsiElement;
+import com.intellij.psi.PsiFile;
 import com.jetbrains.edu.courseFormat.Course;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 import com.jetbrains.edu.courseFormat.Course;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
@@ -11,4 +14,12 @@ public class CCUtils {
     Language language = Language.findLanguageByID(course.getLanguage());
     return language == null ? null :  CCLanguageManager.INSTANCE.forLanguage(language);
   }
     Language language = Language.findLanguageByID(course.getLanguage());
     return language == null ? null :  CCLanguageManager.INSTANCE.forLanguage(language);
   }
+
+  public static boolean isAnswerFile(PsiElement element) {
+    if (!(element instanceof PsiFile)) {
+      return false;
+    }
+    VirtualFile file = ((PsiFile)element).getVirtualFile();
+    return CCProjectService.getInstance(element.getProject()).isAnswerFile(file);
+  }
 }
 }