added opportunity to create task files of all supported file types
[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.FileTemplateUtil;
7 import com.intellij.ide.projectView.ProjectView;
8 import com.intellij.ide.util.DirectoryChooserUtil;
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.fileEditor.FileEditorManager;
17 import com.intellij.openapi.fileTypes.FileType;
18 import com.intellij.openapi.project.DumbAwareAction;
19 import com.intellij.openapi.project.Project;
20 import com.intellij.openapi.vfs.VirtualFile;
21 import com.intellij.psi.PsiDirectory;
22 import com.intellij.psi.PsiElement;
23 import com.jetbrains.edu.coursecreator.CCProjectService;
24 import com.jetbrains.edu.coursecreator.StudyLanguageManager;
25 import com.jetbrains.edu.coursecreator.format.Course;
26 import com.jetbrains.edu.coursecreator.format.Lesson;
27 import com.jetbrains.edu.coursecreator.format.Task;
28 import com.jetbrains.edu.coursecreator.ui.CreateTaskFileDialog;
29 import org.jetbrains.annotations.NotNull;
30
31 import static com.intellij.openapi.ui.DialogWrapper.OK_EXIT_CODE;
32
33 public class CCCreateTaskFile extends DumbAwareAction {
34
35   public CCCreateTaskFile() {
36     super("Task File", "Create new Task File", AllIcons.FileTypes.Text);
37   }
38
39   @Override
40   public void actionPerformed(final AnActionEvent e) {
41     final IdeView view = e.getData(LangDataKeys.IDE_VIEW);
42     final Project project = e.getData(CommonDataKeys.PROJECT);
43
44     if (view == null || project == null) {
45       return;
46     }
47     final PsiDirectory taskDir = DirectoryChooserUtil.getOrChooseDirectory(view);
48     if (taskDir == null) return;
49     PsiDirectory lessonDir = taskDir.getParent();
50     if (lessonDir == null) {
51       return;
52     }
53     final CCProjectService service = CCProjectService.getInstance(project);
54     final Course course = service.getCourse();
55     final Lesson lesson = course.getLesson(lessonDir.getName());
56     final Task task = lesson.getTask(taskDir.getName());
57
58     final int index = task.getTaskFiles().size() + 1;
59     String generatedName = "file" + index;
60     CreateTaskFileDialog dialog = new CreateTaskFileDialog(project, generatedName);
61     dialog.show();
62     if (dialog.getExitCode() != OK_EXIT_CODE) {
63       return;
64     }
65     final String taskFileName = dialog.getFileName();
66     if (taskFileName == null) return;
67     FileType type = dialog.getFileType();
68     if (type == null) {
69       return;
70     }
71     Language language = Language.findLanguageByID(course.getLanguage());
72     if (language == null) {
73       return;
74     }
75     final StudyLanguageManager studyLanguageManager = StudyLanguageManager.INSTANCE.forLanguage(language);
76     final String extension = type.getDefaultExtension();
77     ApplicationManager.getApplication().runWriteAction(new Runnable() {
78       @Override
79       public void run() {
80         final FileTemplate taskTemplate = studyLanguageManager.getTaskFileTemplateForExtension(project, extension);
81         final String answerFileName = taskFileName + ".answer." + extension;
82         try {
83           if (taskTemplate == null) {
84             VirtualFile file = taskDir.getVirtualFile().createChildData(this, answerFileName);
85             ProjectView.getInstance(project).select(file, file, false);
86             FileEditorManager.getInstance(project).openFile(file, true);
87           }
88           else {
89             final PsiElement taskFile = FileTemplateUtil.createFromTemplate(taskTemplate, answerFileName, null, taskDir);
90             ApplicationManager.getApplication().invokeLater(new Runnable() {
91               @Override
92               public void run() {
93                 EditorHelper.openInEditor(taskFile, false);
94                 view.selectElement(taskFile);
95               }
96             });
97           }
98           task.addTaskFile(taskFileName + "." + extension, index);
99         }
100         catch (Exception ignored) {
101         }
102       }
103     });
104   }
105
106   @Override
107   public void update(@NotNull AnActionEvent event) {
108     if (!CCProjectService.setCCActionAvailable(event)) {
109       return;
110     }
111     final Presentation presentation = event.getPresentation();
112     final Project project = event.getData(CommonDataKeys.PROJECT);
113     if (project == null) {
114       presentation.setVisible(false);
115       presentation.setEnabled(false);
116       return;
117     }
118
119     final IdeView view = event.getData(LangDataKeys.IDE_VIEW);
120     if (view == null) {
121       presentation.setVisible(false);
122       presentation.setEnabled(false);
123       return;
124     }
125
126     final PsiDirectory[] directories = view.getDirectories();
127     if (directories.length == 0) {
128       presentation.setVisible(false);
129       presentation.setEnabled(false);
130       return;
131     }
132     final PsiDirectory directory = DirectoryChooserUtil.getOrChooseDirectory(view);
133     final CCProjectService service = CCProjectService.getInstance(project);
134     final Course course = service.getCourse();
135     if (course != null && directory != null && !directory.getName().contains("task")) {
136       presentation.setVisible(false);
137       presentation.setEnabled(false);
138       return;
139     }
140     presentation.setVisible(true);
141     presentation.setEnabled(true);
142   }
143 }