6296ee839af43896a4b3fe8e2260d985f1ff93db
[idea/community.git] / python / educational / course-creator / src / com / jetbrains / edu / coursecreator / actions / CCCreateTaskFile.java
1 package com.jetbrains.edu.coursecreator.actions;
2
3 import com.intellij.icons.AllIcons;
4 import com.intellij.ide.IdeView;
5 import com.intellij.ide.fileTemplates.FileTemplate;
6 import com.intellij.ide.fileTemplates.FileTemplateManager;
7 import com.intellij.ide.fileTemplates.FileTemplateUtil;
8 import com.intellij.ide.util.DirectoryChooserUtil;
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.project.DumbAwareAction;
16 import com.intellij.openapi.project.Project;
17 import com.intellij.openapi.ui.Messages;
18 import com.intellij.psi.PsiDirectory;
19 import com.intellij.psi.PsiElement;
20 import com.jetbrains.edu.coursecreator.CCProjectService;
21 import com.jetbrains.edu.coursecreator.format.Course;
22 import com.jetbrains.edu.coursecreator.format.Lesson;
23 import com.jetbrains.edu.coursecreator.format.Task;
24 import org.jetbrains.annotations.NotNull;
25
26 public class CCCreateTaskFile extends DumbAwareAction {
27
28   public CCCreateTaskFile() {
29     super("Task File", "Create new Task File", AllIcons.FileTypes.Text);
30   }
31
32   @Override
33   public void actionPerformed(AnActionEvent e) {
34     final IdeView view = e.getData(LangDataKeys.IDE_VIEW);
35     final Project project = e.getData(CommonDataKeys.PROJECT);
36
37     if (view == null || project == null) {
38       return;
39     }
40     final PsiDirectory taskDir = DirectoryChooserUtil.getOrChooseDirectory(view);
41     if (taskDir == null) return;
42     PsiDirectory lessonDir = taskDir.getParent();
43     if (lessonDir == null) {
44       return;
45     }
46     final CCProjectService service = CCProjectService.getInstance(project);
47     final Course course = service.getCourse();
48     final Lesson lesson = course.getLesson(lessonDir.getName());
49     final Task task = lesson.getTask(taskDir.getName());
50
51     final int index = task.getTaskFiles().size() + 1;
52     String generatedName = "file" + index;
53     final String taskFileName = Messages.showInputDialog("Name:", "Task File Name", null, generatedName, null);
54     if (taskFileName == null) return;
55
56     ApplicationManager.getApplication().runWriteAction(new Runnable() {
57       @Override
58       public void run() {
59           final FileTemplate taskTemplate = FileTemplateManager.getInstance(project).getInternalTemplate("task.answer");
60           try {
61             final PsiElement taskPyFile = FileTemplateUtil.createFromTemplate(taskTemplate, taskFileName, null, taskDir);
62             task.addTaskFile(taskFileName + ".py", index);
63             ApplicationManager.getApplication().invokeLater(new Runnable() {
64               @Override
65               public void run() {
66                 EditorHelper.openInEditor(taskPyFile, false);
67                 view.selectElement(taskPyFile);
68               }
69             });
70           }
71           catch (Exception ignored) {
72           }
73       }
74     });
75   }
76
77   @Override
78   public void update(@NotNull AnActionEvent event) {
79     if (!CCProjectService.setCCActionAvailable(event)) {
80       return;
81     }
82     final Presentation presentation = event.getPresentation();
83     final Project project = event.getData(CommonDataKeys.PROJECT);
84     if (project == null) {
85       presentation.setVisible(false);
86       presentation.setEnabled(false);
87       return;
88     }
89
90     final IdeView view = event.getData(LangDataKeys.IDE_VIEW);
91     if (view == null) {
92       presentation.setVisible(false);
93       presentation.setEnabled(false);
94       return;
95     }
96
97     final PsiDirectory[] directories = view.getDirectories();
98     if (directories.length == 0) {
99       presentation.setVisible(false);
100       presentation.setEnabled(false);
101       return;
102     }
103     final PsiDirectory directory = DirectoryChooserUtil.getOrChooseDirectory(view);
104     final CCProjectService service = CCProjectService.getInstance(project);
105     final Course course = service.getCourse();
106     if (course != null && directory != null && !directory.getName().contains("task")) {
107       presentation.setVisible(false);
108       presentation.setEnabled(false);
109       return;
110     }
111     presentation.setVisible(true);
112     presentation.setEnabled(true);
113   }
114 }