fixed NPE in case of task file invalidation
authorLiana Bakradze <liana.bakradze@jetbrains.com>
Thu, 2 Oct 2014 10:03:16 +0000 (14:03 +0400)
committerLiana Bakradze <liana.bakradze@jetbrains.com>
Thu, 2 Oct 2014 10:03:16 +0000 (14:03 +0400)
python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/CCRunTests.java
python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/actions/CCAddTaskWindow.java
python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/actions/CCDeleteTaskWindow.java

index eba98f96da19e6397830414c37dd7c14667cb77d..65bb8591c715324ad2d7ec6ea310303577a7fc40 100644 (file)
@@ -33,6 +33,7 @@ import com.intellij.openapi.util.TextRange;
 import com.intellij.openapi.util.io.FileUtil;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.openapi.vfs.VirtualFileManager;
+import com.intellij.psi.PsiDirectory;
 import com.intellij.psi.PsiElement;
 import com.intellij.psi.PsiFile;
 import com.intellij.util.PathUtil;
@@ -66,12 +67,39 @@ public class CCRunTests extends AnAction {
     }
     PsiElement psiElement = location.getPsiElement();
     PsiFile psiFile = psiElement.getContainingFile();
-    if (psiFile != null && psiFile.getName().contains(".answer")) {
+    Project project = e.getProject();
+    if (project == null || psiFile == null) {
+      presentation.setVisible(false);
+      presentation.setEnabled(false);
+      return;
+    }
+    final CCProjectService service = CCProjectService.getInstance(project);
+    final Course course = service.getCourse();
+    final PsiDirectory taskDir = psiFile.getContainingDirectory();
+    final PsiDirectory lessonDir = taskDir.getParent();
+    if (lessonDir == null) return;
+    final Lesson lesson = course.getLesson(lessonDir.getName());
+    final Task task = lesson.getTask(taskDir.getName());
+    if (task == null) {
+      presentation.setVisible(false);
+      presentation.setEnabled(false);
+      return;
+    }
+    TaskFile taskFile = task.getTaskFile(psiFile.getName());
+    if (taskFile == null) {
+      LOG.info("could not find task file");
+      presentation.setVisible(false);
+      presentation.setEnabled(false);
+      return;
+    }
+    if (psiFile.getName().contains(".answer")) {
       presentation.setEnabled(true);
+      presentation.setVisible(true);
       presentation.setText("Run tests from '" + psiFile.getName() + "'");
     }
     else {
       presentation.setEnabled(false);
+      presentation.setVisible(false);
     }
   }
 
index 453bbf49dc7e5b442b1de03619c2b759d9fc9a5a..9555de7e2e170a286ced70d10bb6406353a55a53 100644 (file)
@@ -4,6 +4,7 @@ import com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerImpl;
 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;
@@ -19,6 +20,8 @@ import org.jetbrains.plugins.coursecreator.format.*;
 import org.jetbrains.plugins.coursecreator.ui.CreateTaskWindowDialog;
 
 public class CCAddTaskWindow extends DumbAwareAction {
+  private static final Logger LOG = Logger.getInstance(CCAddTaskWindow.class);
+
   public CCAddTaskWindow() {
     super("Add task window","Add task window", null);
   }
@@ -103,8 +106,14 @@ public class CCAddTaskWindow extends DumbAwareAction {
       presentation.setEnabled(false);
       return;
     }
+    TaskFile taskFile = task.getTaskFile(file.getName());
+    if (taskFile == null) {
+      LOG.info("could not find task file");
+      presentation.setVisible(false);
+      presentation.setEnabled(false);
+      return;
+    }
     presentation.setVisible(true);
     presentation.setEnabled(true);
-
   }
 }
\ No newline at end of file
index dbbe4230b396c8f69ea1125974359421f68050fc..9257c2adf41c7dd04e7ba728e01cc7dad481845e 100644 (file)
@@ -4,6 +4,7 @@ import com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerImpl;
 import com.intellij.openapi.actionSystem.AnActionEvent;
 import com.intellij.openapi.actionSystem.CommonDataKeys;
 import com.intellij.openapi.actionSystem.PlatformDataKeys;
+import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.editor.Document;
 import com.intellij.openapi.editor.Editor;
 import com.intellij.openapi.project.DumbAwareAction;
@@ -19,6 +20,7 @@ import java.util.List;
 
 @SuppressWarnings("ComponentNotRegistered")
 public class CCDeleteTaskWindow extends DumbAwareAction {
+  private static final Logger LOG = Logger.getInstance(CCDeleteTaskWindow.class);
   @NotNull
   private final TaskWindow myTaskWindow;
 
@@ -49,6 +51,10 @@ public class CCDeleteTaskWindow extends DumbAwareAction {
     final Lesson lesson = course.getLesson(lessonDir.getName());
     final Task task = lesson.getTask(taskDir.getName());
     final TaskFile taskFile = task.getTaskFile(file.getName());
+    if (taskFile == null) {
+      LOG.info("could not find task file");
+      return;
+    }
     final List<TaskWindow> taskWindows = taskFile.getTaskWindows();
     if (taskWindows.contains(myTaskWindow)) {
       myTaskWindow.removeResources(project);