added opportunity to create task files of all supported file types
[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.coursecreator.actions.CCCreateLesson;
19 import com.jetbrains.edu.coursecreator.actions.CCCreateTask;
20 import com.jetbrains.edu.coursecreator.format.Course;
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   private 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(name, author, description);
67     course.setLanguage("Python");
68     service.setCourse(course);
69
70     final PsiDirectory projectDir = PsiManager.getInstance(project).findDirectory(baseDir);
71     if (projectDir == null) return;
72     new WriteCommandAction.Simple(project) {
73       @Override
74       protected void run() throws Throwable {
75         final FileTemplate template = FileTemplateManager.getInstance(project).getInternalTemplate("test_helper");
76         try {
77           FileTemplateUtil.createFromTemplate(template, "test_helper.py", null, projectDir);
78         }
79         catch (Exception ignored) {
80         }
81         DirectoryUtil.createSubdirectories("hints", projectDir, "\\/");
82         final PsiDirectory lessonDir = CCCreateLesson.createLessonDir(projectDir, 1, null, null, course);
83         CCCreateTask.createTask(null, project, lessonDir, false);
84       }
85     }.execute();
86   }
87
88   @NotNull
89   @Override
90   public ValidationResult validate(@NotNull String s) {
91     String message = "";
92     message = mySettingsPanel.getDescription().isEmpty() ? "Enter description" : message;
93     message = mySettingsPanel.getAuthor().isEmpty() ? "Enter author name" : message;
94     message = mySettingsPanel.getName().isEmpty() ? "Enter course name" : message;
95     return message.isEmpty() ? ValidationResult.OK : new ValidationResult(message);
96   }
97
98   @Nullable
99   @Override
100   public JPanel extendBasePanel() throws ProcessCanceledException {
101     mySettingsPanel = new CCNewProjectPanel();
102     mySettingsPanel.registerValidators(new FacetValidatorsManager() {
103       public void registerValidator(FacetEditorValidator validator, JComponent... componentsToWatch) {
104         throw new UnsupportedOperationException();
105       }
106
107       public void validate() {
108         fireStateChanged();
109       }
110     });
111     return mySettingsPanel.getMainPanel();
112   }
113 }