b0b612df6f2ea984deb38bc191ecd4d119ecb608
[idea/community.git] / python / educational-python / course-creator-python / src / com / jetbrains / edu / coursecreator / PyCCProjectGenerator.java
1 package com.jetbrains.edu.coursecreator;
2
3 import com.intellij.facet.ui.FacetEditorValidator;
4 import com.intellij.facet.ui.FacetValidatorsManager;
5 import com.intellij.facet.ui.ValidationResult;
6 import com.intellij.ide.fileTemplates.FileTemplate;
7 import com.intellij.ide.fileTemplates.FileTemplateManager;
8 import com.intellij.ide.fileTemplates.FileTemplateUtil;
9 import com.intellij.openapi.application.PathManager;
10 import com.intellij.openapi.command.WriteCommandAction;
11 import com.intellij.openapi.diagnostic.Logger;
12 import com.intellij.openapi.module.Module;
13 import com.intellij.openapi.progress.ProcessCanceledException;
14 import com.intellij.openapi.project.Project;
15 import com.intellij.openapi.util.io.FileUtil;
16 import com.intellij.openapi.vfs.VirtualFile;
17 import com.intellij.platform.DirectoryProjectGenerator;
18 import com.intellij.psi.PsiDirectory;
19 import com.intellij.psi.PsiManager;
20 import com.jetbrains.edu.coursecreator.actions.CCCreateLesson;
21 import com.jetbrains.edu.coursecreator.actions.CCCreateTask;
22 import com.jetbrains.edu.coursecreator.ui.CCNewProjectPanel;
23 import com.jetbrains.edu.learning.StudyProjectComponent;
24 import com.jetbrains.edu.learning.StudyTaskManager;
25 import com.jetbrains.edu.learning.core.EduNames;
26 import com.jetbrains.edu.learning.courseFormat.Course;
27 import com.jetbrains.edu.learning.statistics.EduUsagesCollector;
28 import com.jetbrains.python.PythonLanguage;
29 import com.jetbrains.python.newProject.PythonProjectGenerator;
30 import icons.CourseCreatorPythonIcons;
31 import org.jetbrains.annotations.Nls;
32 import org.jetbrains.annotations.NotNull;
33 import org.jetbrains.annotations.Nullable;
34
35 import javax.swing.*;
36 import java.io.File;
37
38
39 public class PyCCProjectGenerator extends PythonProjectGenerator implements DirectoryProjectGenerator {
40   private static final Logger LOG = Logger.getInstance(PyCCProjectGenerator.class);
41   private CCNewProjectPanel mySettingsPanel;
42
43   @Nls
44   @NotNull
45   @Override
46   public String getName() {
47     return "Course creation";
48   }
49
50   @Nullable
51   @Override
52   public Icon getLogo() {
53     return CourseCreatorPythonIcons.CourseCreationProjectType;
54   }
55
56   @Override
57   public void generateProject(@NotNull final Project project, @NotNull final VirtualFile baseDir,
58                               @Nullable Object settings, @NotNull Module module) {
59     generateProject(project, baseDir, mySettingsPanel.getName(),
60                     mySettingsPanel.getAuthors(), mySettingsPanel.getDescription());
61   }
62
63   public static void generateProject(@NotNull final Project project, @NotNull final VirtualFile baseDir,
64                                      @NotNull final String name, @NotNull final String[] authors,
65                                      @NotNull final String description) {
66     final Course course = getCourse(project, name, authors, description);
67     EduUsagesCollector.projectTypeCreated(CCUtils.COURSE_MODE);
68
69     final PsiDirectory projectDir = PsiManager.getInstance(project).findDirectory(baseDir);
70     if (projectDir == null) return;
71     new WriteCommandAction.Simple(project) {
72       @Override
73       protected void run() throws Throwable {
74
75         createTestHelper(project, projectDir);
76
77         PsiDirectory lessonDir = new CCCreateLesson().createItem(null, project, projectDir, course);
78         if (lessonDir == null) {
79           LOG.error("Failed to create lesson");
80           return;
81         }
82         new CCCreateTask().createItem(null, project, lessonDir, course);
83       }
84     }.execute();
85   }
86
87   private static void createTestHelper(@NotNull Project project, PsiDirectory projectDir) {
88     final FileTemplate template = FileTemplateManager.getInstance(project).getInternalTemplate(FileUtil.getNameWithoutExtension(EduNames.TEST_HELPER));
89     try {
90       FileTemplateUtil.createFromTemplate(template, EduNames.TEST_HELPER, null, projectDir);
91     }
92     catch (Exception ignored) {
93     }
94   }
95
96   @NotNull
97   private static Course getCourse(@NotNull Project project, @NotNull String name, @NotNull String[] authors, @NotNull String description) {
98     final Course course = new Course();
99     course.setName(name);
100     course.setAuthors(authors);
101     course.setDescription(description);
102     course.setLanguage(PythonLanguage.getInstance().getID());
103     course.setCourseMode(CCUtils.COURSE_MODE);
104
105     File coursesDir = new File(PathManager.getConfigPath(), "courses");
106     File courseDir = new File(coursesDir, name + "-" + project.getName());
107     course.setCourseDirectory(courseDir.getPath());
108
109     StudyTaskManager.getInstance(project).setCourse(course);
110     StudyProjectComponent.getInstance(project).registerStudyToolWindow(course);
111     return course;
112   }
113
114   @NotNull
115   @Override
116   public ValidationResult validate(@NotNull String s) {
117     String message = "";
118     message = mySettingsPanel.getDescription().isEmpty() ? "Enter description" : message;
119     message = mySettingsPanel.getAuthors().length == 0 ? "Enter author name" : message;
120     message = mySettingsPanel.getName().isEmpty() ? "Enter course name" : message;
121     return message.isEmpty() ? ValidationResult.OK : new ValidationResult(message);
122   }
123
124   @Nullable
125   @Override
126   public JPanel extendBasePanel() throws ProcessCanceledException {
127     mySettingsPanel = new CCNewProjectPanel();
128     mySettingsPanel.registerValidators(new FacetValidatorsManager() {
129       public void registerValidator(FacetEditorValidator validator, JComponent... componentsToWatch) {
130         throw new UnsupportedOperationException();
131       }
132
133       public void validate() {
134         fireStateChanged();
135       }
136     });
137     return mySettingsPanel.getMainPanel();
138   }
139 }