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