EDU-364 "Introduction to Python" action often opens wrong project
authorLiana Bakradze <liana.bakradze@jetbrains.com>
Thu, 6 Aug 2015 09:28:45 +0000 (12:28 +0300)
committerLiana Bakradze <liana.bakradze@jetbrains.com>
Thu, 6 Aug 2015 09:28:45 +0000 (12:28 +0300)
the idea is not to show action at all if there is no introduction course

python/edu/interactive-learning-python/src/com/jetbrains/edu/learning/actions/PyStudyIntroductionCourseAction.java

index 4e16c6c8bab1c5086e978addf6efca473f04ea26..a99d68b02ac49a5846b89f6741173887791fe9f0 100644 (file)
@@ -18,49 +18,60 @@ package com.jetbrains.edu.learning.actions;
 import com.intellij.ide.impl.ProjectUtil;
 import com.intellij.openapi.actionSystem.AnAction;
 import com.intellij.openapi.actionSystem.AnActionEvent;
 import com.intellij.ide.impl.ProjectUtil;
 import com.intellij.openapi.actionSystem.AnAction;
 import com.intellij.openapi.actionSystem.AnActionEvent;
+import com.intellij.openapi.actionSystem.Presentation;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.project.ProjectManager;
 import com.intellij.openapi.projectRoots.Sdk;
 import com.jetbrains.edu.learning.PyStudyDirectoryProjectGenerator;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.project.ProjectManager;
 import com.intellij.openapi.projectRoots.Sdk;
 import com.jetbrains.edu.learning.PyStudyDirectoryProjectGenerator;
-import com.jetbrains.edu.learning.StudyUtils;
 import com.jetbrains.edu.stepic.CourseInfo;
 import com.jetbrains.python.configuration.PyConfigurableInterpreterList;
 import com.jetbrains.python.newProject.actions.GenerateProjectCallback;
 import com.jetbrains.python.newProject.actions.ProjectSpecificSettingsStep;
 import icons.InteractiveLearningPythonIcons;
 import org.jetbrains.annotations.NotNull;
 import com.jetbrains.edu.stepic.CourseInfo;
 import com.jetbrains.python.configuration.PyConfigurableInterpreterList;
 import com.jetbrains.python.newProject.actions.GenerateProjectCallback;
 import com.jetbrains.python.newProject.actions.ProjectSpecificSettingsStep;
 import icons.InteractiveLearningPythonIcons;
 import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
 
 import java.io.File;
 import java.util.List;
 
 public class PyStudyIntroductionCourseAction extends AnAction {
 
 
 import java.io.File;
 import java.util.List;
 
 public class PyStudyIntroductionCourseAction extends AnAction {
 
+  public static final String INTRODUCTION_TO_PYTHON = "Introduction to Python";
+  public static final String INTRODUCTION_FOLDER = "PythonIntroduction";
+
   public PyStudyIntroductionCourseAction() {
   public PyStudyIntroductionCourseAction() {
-    super("Introduction to Python", "Introduction to Python", InteractiveLearningPythonIcons.EducationalProjectType);
+    super(INTRODUCTION_TO_PYTHON, INTRODUCTION_TO_PYTHON, InteractiveLearningPythonIcons.EducationalProjectType);
+  }
+
+  @Override
+  public void update(AnActionEvent e) {
+    final File projectDir = new File(ProjectUtil.getBaseDir(), INTRODUCTION_FOLDER);
+    if (projectDir.exists()) {
+      return;
+    }
+    final PyStudyDirectoryProjectGenerator generator = new PyStudyDirectoryProjectGenerator();
+    if (getIntroCourseInfo(generator.getCourses()) != null) {
+      return;
+    }
+    Presentation presentation = e.getPresentation();
+    presentation.setVisible(false);
+    presentation.setEnabled(false);
   }
 
   @Override
   public void actionPerformed(@NotNull AnActionEvent e) {
   }
 
   @Override
   public void actionPerformed(@NotNull AnActionEvent e) {
-    final File projectDir = new File(ProjectUtil.getBaseDir(), "PythonIntroduction");
+    final File projectDir = new File(ProjectUtil.getBaseDir(), INTRODUCTION_FOLDER);
     if (projectDir.exists()) {
       ProjectUtil.openProject(projectDir.getPath(), null, false);
     }
     else {
     if (projectDir.exists()) {
       ProjectUtil.openProject(projectDir.getPath(), null, false);
     }
     else {
-      final GenerateProjectCallback callback = new GenerateProjectCallback();
       final PyStudyDirectoryProjectGenerator generator = new PyStudyDirectoryProjectGenerator();
       final PyStudyDirectoryProjectGenerator generator = new PyStudyDirectoryProjectGenerator();
-      final List<CourseInfo> courses = generator.getCourses();
-      CourseInfo introCourse = null;
-      for (CourseInfo info : courses) {
-        if ("Introduction to Python".equals(info.getName())) {
-          introCourse = info;
-        }
-      }
+      CourseInfo introCourse = getIntroCourseInfo(generator.getCourses());
       if (introCourse == null) {
       if (introCourse == null) {
-        introCourse = StudyUtils.getFirst(courses);
+        return;
       }
       }
-
+      final GenerateProjectCallback callback = new GenerateProjectCallback();
       final ProjectSpecificSettingsStep step = new ProjectSpecificSettingsStep(generator, callback);
       final ProjectSpecificSettingsStep step = new ProjectSpecificSettingsStep(generator, callback);
-
       step.createPanel(); // initialize panel to set location
       step.setLocation(projectDir.toString());
       generator.setSelectedCourse(introCourse);
       step.createPanel(); // initialize panel to set location
       step.setLocation(projectDir.toString());
       generator.setSelectedCourse(introCourse);
@@ -72,4 +83,14 @@ public class PyStudyIntroductionCourseAction extends AnAction {
       callback.consume(step);
     }
   }
       callback.consume(step);
     }
   }
+
+  @Nullable
+  private static CourseInfo getIntroCourseInfo(final List<CourseInfo> courses) {
+    for (CourseInfo courseInfo : courses) {
+      if (INTRODUCTION_TO_PYTHON.equals(courseInfo.getName())) {
+        return courseInfo;
+      }
+    }
+    return null;
+  }
 }
 }