added idea support
[idea/community.git] / python / edu / 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.ide.util.DirectoryUtil;
10 import com.intellij.openapi.command.WriteCommandAction;
11 import com.intellij.openapi.module.Module;
12 import com.intellij.openapi.progress.ProcessCanceledException;
13 import com.intellij.openapi.project.Project;
14 import com.intellij.openapi.vfs.VirtualFile;
15 import com.intellij.platform.DirectoryProjectGenerator;
16 import com.intellij.psi.PsiDirectory;
17 import com.intellij.psi.PsiManager;
18 import com.jetbrains.edu.courseFormat.Course;
19 import com.jetbrains.edu.coursecreator.actions.CCCreateLesson;
20 import com.jetbrains.edu.coursecreator.actions.CCCreateTask;
21 import com.jetbrains.edu.coursecreator.ui.CCNewProjectPanel;
22 import com.jetbrains.python.newProject.PythonProjectGenerator;
23 import icons.CourseCreatorPythonIcons;
24 import org.jetbrains.annotations.Nls;
25 import org.jetbrains.annotations.NotNull;
26 import org.jetbrains.annotations.Nullable;
27
28 import javax.swing.*;
29
30
31 public class PyCCProjectGenerator extends PythonProjectGenerator implements DirectoryProjectGenerator {
32   private CCNewProjectPanel mySettingsPanel;
33
34   @Nls
35   @NotNull
36   @Override
37   public String getName() {
38     return "Course creation";
39   }
40
41   @Nullable
42   @Override
43   public Object showGenerationSettings(VirtualFile baseDir) throws ProcessCanceledException {
44     return null;
45   }
46
47   @Nullable
48   @Override
49   public Icon getLogo() {
50     return CourseCreatorPythonIcons.CourseCreationProjectType;
51   }
52
53
54   @Override
55   public void generateProject(@NotNull final Project project, @NotNull final VirtualFile baseDir,
56                               @Nullable Object settings, @NotNull Module module) {
57     generateProject(project, baseDir, mySettingsPanel.getName(),
58                     mySettingsPanel.getAuthor(), mySettingsPanel.getDescription());
59   }
60
61   public static void generateProject(@NotNull final Project project, @NotNull final VirtualFile baseDir,
62                                      @NotNull final String name, @NotNull final String author,
63                                      @NotNull final String description) {
64
65     final CCProjectService service = CCProjectService.getInstance(project);
66     final Course course = new Course();
67     course.setName(name);
68     course.setAuthor(author);
69     course.setDescription(description);
70     course.setLanguage("Python");
71     service.setCourse(course);
72
73     final PsiDirectory projectDir = PsiManager.getInstance(project).findDirectory(baseDir);
74     if (projectDir == null) return;
75     new WriteCommandAction.Simple(project) {
76       @Override
77       protected void run() throws Throwable {
78         final FileTemplate template = FileTemplateManager.getInstance(project).getInternalTemplate("test_helper");
79         try {
80           FileTemplateUtil.createFromTemplate(template, "test_helper.py", null, projectDir);
81         }
82         catch (Exception ignored) {
83         }
84         DirectoryUtil.createSubdirectories("hints", projectDir, "\\/");
85         final PsiDirectory lessonDir = CCCreateLesson.createLessonDir(project, 1, null, null);
86         CCCreateTask.createTask(null, project, lessonDir, false);
87       }
88     }.execute();
89   }
90
91   @NotNull
92   @Override
93   public ValidationResult validate(@NotNull String s) {
94     String message = "";
95     message = mySettingsPanel.getDescription().isEmpty() ? "Enter description" : message;
96     message = mySettingsPanel.getAuthor().isEmpty() ? "Enter author name" : message;
97     message = mySettingsPanel.getName().isEmpty() ? "Enter course name" : message;
98     return message.isEmpty() ? ValidationResult.OK : new ValidationResult(message);
99   }
100
101   @Nullable
102   @Override
103   public JPanel extendBasePanel() throws ProcessCanceledException {
104     mySettingsPanel = new CCNewProjectPanel();
105     mySettingsPanel.registerValidators(new FacetValidatorsManager() {
106       public void registerValidator(FacetEditorValidator validator, JComponent... componentsToWatch) {
107         throw new UnsupportedOperationException();
108       }
109
110       public void validate() {
111         fireStateChanged();
112       }
113     });
114     return mySettingsPanel.getMainPanel();
115   }
116 }