fefa795f4ff6cb7c7c09d9fb3617ad4b8a6df6e9
[idea/community.git] / python / educational / course-creator / src / com / jetbrains / edu / coursecreator / actions / CCCreateTask.java
1 package com.jetbrains.edu.coursecreator.actions;
2
3 import com.intellij.ide.IdeView;
4 import com.intellij.ide.fileTemplates.FileTemplate;
5 import com.intellij.ide.fileTemplates.FileTemplateManager;
6 import com.intellij.ide.fileTemplates.FileTemplateUtil;
7 import com.intellij.ide.util.DirectoryChooserUtil;
8 import com.intellij.ide.util.DirectoryUtil;
9 import com.intellij.ide.util.EditorHelper;
10 import com.intellij.lang.Language;
11 import com.intellij.openapi.actionSystem.AnActionEvent;
12 import com.intellij.openapi.actionSystem.CommonDataKeys;
13 import com.intellij.openapi.actionSystem.LangDataKeys;
14 import com.intellij.openapi.actionSystem.Presentation;
15 import com.intellij.openapi.application.ApplicationManager;
16 import com.intellij.openapi.diagnostic.Logger;
17 import com.intellij.openapi.fileEditor.FileEditorManager;
18 import com.intellij.openapi.project.DumbAwareAction;
19 import com.intellij.openapi.project.Project;
20 import com.intellij.openapi.ui.Messages;
21 import com.intellij.openapi.vfs.VirtualFile;
22 import com.intellij.psi.PsiDirectory;
23 import com.intellij.psi.PsiElement;
24 import com.intellij.util.PlatformIcons;
25 import com.jetbrains.edu.coursecreator.CCProjectService;
26 import com.jetbrains.edu.coursecreator.CCUtils;
27 import com.jetbrains.edu.coursecreator.StudyLanguageManager;
28 import com.jetbrains.edu.coursecreator.format.Course;
29 import com.jetbrains.edu.coursecreator.format.Lesson;
30 import com.jetbrains.edu.coursecreator.format.Task;
31 import org.jetbrains.annotations.NotNull;
32 import org.jetbrains.annotations.Nullable;
33
34 public class CCCreateTask extends DumbAwareAction {
35   private static final Logger LOG = Logger.getInstance(CCCreateTask.class.getName());
36
37   public CCCreateTask() {
38     super("Task", "Create new Task", PlatformIcons.DIRECTORY_CLOSED_ICON);
39   }
40
41   @Override
42   public void actionPerformed(final AnActionEvent e) {
43     final IdeView view = e.getData(LangDataKeys.IDE_VIEW);
44     final Project project = e.getData(CommonDataKeys.PROJECT);
45
46     if (project == null || view == null) {
47       return;
48     }
49     final PsiDirectory directory = DirectoryChooserUtil.getOrChooseDirectory(view);
50
51     if (directory == null) return;
52     createTask(view, project, directory, true);
53   }
54
55   public static void createTask(final IdeView view, final Project project, final PsiDirectory lessonDir, boolean showDialog) {
56     final CCProjectService service = CCProjectService.getInstance(project);
57     final Course course = service.getCourse();
58     final Lesson lesson = course.getLesson(lessonDir.getName());
59     final int size = lesson.getTaskList().size();
60     final String taskName;
61     if (showDialog) {
62       taskName = Messages.showInputDialog("Name:", "Task Name", null, "task" + (size + 1), null);
63     }
64     else {
65       taskName = "task" + (size + 1);
66     }
67
68     if (taskName == null) {
69       return;
70     }
71     ApplicationManager.getApplication().runWriteAction(new Runnable() {
72       @Override
73       public void run() {
74         final PsiDirectory taskDirectory = DirectoryUtil.createSubdirectories("task" + (size + 1), lessonDir, "\\/");
75         if (taskDirectory != null) {
76           Language language = Language.findLanguageByID(course.getLanguage());
77           if (language == null) {
78             return;
79           }
80           final StudyLanguageManager studyLanguageManager = StudyLanguageManager.INSTANCE.forLanguage(language);
81           CCUtils.markDirAsSourceRoot(taskDirectory.getVirtualFile(), project);
82
83           createFromTemplateAndOpen(taskDirectory, studyLanguageManager.getTestsTemplate(project), view);
84           createFromTemplateAndOpen(taskDirectory, FileTemplateManager.getInstance(project).getInternalTemplate("task.html"), view);
85           String defaultExtension = studyLanguageManager.getDefaultTaskFileExtension();
86           String taskFileName = null;
87           if (defaultExtension != null) {
88             FileTemplate taskFileTemplate = studyLanguageManager.getTaskFileTemplateForExtension(project,
89                                                                                           defaultExtension);
90             createFromTemplateAndOpen(taskDirectory, taskFileTemplate, view);
91             if (taskFileTemplate != null) {
92               taskFileName = taskFileTemplate.getName();
93             }
94           }
95
96           final Task task = new Task(taskName);
97           task.setIndex(size + 1);
98           lesson.addTask(task, taskDirectory);
99           if (taskFileName != null) {
100             task.addTaskFile(taskFileName, size + 1);
101           }
102           ApplicationManager.getApplication().invokeLater(new Runnable() {
103             @Override
104             public void run() {
105               FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
106               for (VirtualFile virtualFile : fileEditorManager.getOpenFiles()) {
107                 fileEditorManager.closeFile(virtualFile);
108               }
109             }
110           });
111         }
112       }
113     });
114   }
115
116   private static void createFromTemplateAndOpen(@NotNull final PsiDirectory taskDirectory,
117                                                 @Nullable final FileTemplate template,
118                                                 @Nullable IdeView view) {
119     if (template == null) {
120       return;
121     }
122     try {
123       final PsiElement file = FileTemplateUtil.createFromTemplate(template, template.getName(), null, taskDirectory);
124       if (view != null) {
125         EditorHelper.openInEditor(file, false);
126         view.selectElement(file);
127       }
128     }
129     catch (Exception e) {
130       LOG.error(e);
131     }
132   }
133
134   @Override
135   public void update(@NotNull AnActionEvent event) {
136     if (!CCProjectService.setCCActionAvailable(event)) {
137       return;
138     }
139     final Presentation presentation = event.getPresentation();
140     final Project project = event.getData(CommonDataKeys.PROJECT);
141     if (project == null) {
142       presentation.setVisible(false);
143       presentation.setEnabled(false);
144       return;
145     }
146
147     final IdeView view = event.getData(LangDataKeys.IDE_VIEW);
148     if (view == null) {
149       presentation.setVisible(false);
150       presentation.setEnabled(false);
151       return;
152     }
153
154     final PsiDirectory[] directories = view.getDirectories();
155     if (directories.length == 0) {
156       presentation.setVisible(false);
157       presentation.setEnabled(false);
158       return;
159     }
160     final PsiDirectory directory = DirectoryChooserUtil.getOrChooseDirectory(view);
161     final CCProjectService service = CCProjectService.getInstance(project);
162     final Course course = service.getCourse();
163     if (course != null && directory != null && course.getLesson(directory.getName()) == null) {
164       presentation.setVisible(false);
165       presentation.setEnabled(false);
166       return;
167     }
168
169     presentation.setVisible(true);
170     presentation.setEnabled(true);
171   }
172 }