added idea support
[idea/community.git] / python / edu / interactive-learning-python / src / com / jetbrains / edu / learning / PyStudyDirectoryProjectGenerator.java
1 package com.jetbrains.edu.learning;
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.diagnostic.Logger;
10 import com.intellij.openapi.module.Module;
11 import com.intellij.openapi.progress.ProcessCanceledException;
12 import com.intellij.openapi.project.Project;
13 import com.intellij.openapi.vfs.VirtualFile;
14 import com.intellij.platform.DirectoryProjectGenerator;
15 import com.intellij.psi.PsiDirectory;
16 import com.intellij.psi.PsiManager;
17 import com.jetbrains.edu.learning.courseGeneration.StudyProjectGenerator;
18 import com.jetbrains.edu.learning.ui.StudyNewProjectPanel;
19 import com.jetbrains.edu.stepic.CourseInfo;
20 import com.jetbrains.python.newProject.PythonProjectGenerator;
21 import icons.InteractiveLearningPythonIcons;
22 import org.jetbrains.annotations.Nls;
23 import org.jetbrains.annotations.NotNull;
24 import org.jetbrains.annotations.Nullable;
25
26 import javax.swing.*;
27 import java.util.List;
28
29
30 public class PyStudyDirectoryProjectGenerator extends PythonProjectGenerator implements DirectoryProjectGenerator {
31   private static final Logger LOG = Logger.getInstance(PyStudyDirectoryProjectGenerator.class.getName());
32   private final StudyProjectGenerator myGenerator;
33   public ValidationResult myValidationResult = new ValidationResult("selected course is not valid");
34
35   public PyStudyDirectoryProjectGenerator() {
36     myGenerator = new StudyProjectGenerator();
37     myGenerator.addSettingsStateListener(new StudyProjectGenerator.SettingsListener() {
38       @Override
39       public void stateChanged(ValidationResult result) {
40         setValidationResult(result);
41       }
42     });
43   }
44
45   @Nls
46   @NotNull
47   @Override
48   public String getName() {
49     return "Educational";
50   }
51
52   @Nullable
53   @Override
54   public Object showGenerationSettings(VirtualFile baseDir) throws ProcessCanceledException {
55     return null;
56   }
57
58   @Nullable
59   @Override
60   public Icon getLogo() {
61     return InteractiveLearningPythonIcons.EducationalProjectType;
62   }
63
64
65   @Override
66   public void generateProject(@NotNull final Project project, @NotNull final VirtualFile baseDir,
67                               @Nullable Object settings, @NotNull Module module) {
68     myGenerator.generateProject(project, baseDir);
69     final FileTemplate template = FileTemplateManager.getInstance(project).getInternalTemplate("test_helper");
70     final PsiDirectory projectDir = PsiManager.getInstance(project).findDirectory(baseDir);
71     if (projectDir == null) return;
72     try {
73       FileTemplateUtil.createFromTemplate(template, "test_helper.py", null, projectDir);
74     }
75     catch (Exception exception) {
76       LOG.error("Can't copy test_helper.py " + exception.getMessage());
77     }
78   }
79   @NotNull
80   @Override
81   public ValidationResult validate(@NotNull String s) {
82     return myValidationResult;
83   }
84
85   public void setValidationResult(ValidationResult validationResult) {
86     myValidationResult = validationResult;
87   }
88
89   @Nullable
90   @Override
91   public JPanel extendBasePanel() throws ProcessCanceledException {
92     StudyNewProjectPanel settingsPanel = new StudyNewProjectPanel(myGenerator);
93     settingsPanel.registerValidators(new FacetValidatorsManager() {
94       public void registerValidator(FacetEditorValidator validator, JComponent... componentsToWatch) {
95         throw new UnsupportedOperationException();
96       }
97       public void validate() {
98         fireStateChanged();
99       }
100     });
101     return settingsPanel.getContentPanel();
102   }
103
104   public List<CourseInfo> getCourses() {
105     return myGenerator.getCourses();
106   }
107
108   public void setSelectedCourse(CourseInfo course) {
109     myGenerator.setSelectedCourse(course);
110   }
111
112   public StudyProjectGenerator getGenerator() {
113     return myGenerator;
114   }
115 }