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