moved everything python dependent to language part of plugin
[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.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.fileEditor.FileEditorManager;
16 import com.intellij.openapi.fileTypes.FileType;
17 import com.intellij.openapi.project.DumbAwareAction;
18 import com.intellij.openapi.project.Project;
19 import com.intellij.openapi.vfs.VirtualFile;
20 import com.intellij.psi.PsiDirectory;
21 import com.intellij.psi.PsiElement;
22 import com.jetbrains.edu.coursecreator.CCProjectService;
23 import com.jetbrains.edu.coursecreator.CCUtils;
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, course);
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     final StudyLanguageManager studyLanguageManager = CCUtils.getStudyLanguageManager(course);
72     if (studyLanguageManager == null) {
73       return;
74     }
75     final String extension = type.getDefaultExtension();
76     ApplicationManager.getApplication().runWriteAction(new Runnable() {
77       @Override
78       public void run() {
79         final FileTemplate taskTemplate = studyLanguageManager.getTaskFileTemplateForExtension(project, extension);
80         final String answerFileName = taskFileName + ".answer." + extension;
81         try {
82           if (taskTemplate == null) {
83             VirtualFile file = taskDir.getVirtualFile().createChildData(this, answerFileName);
84             ProjectView.getInstance(project).select(file, file, false);
85             FileEditorManager.getInstance(project).openFile(file, true);
86           }
87           else {
88             final PsiElement taskFile = FileTemplateUtil.createFromTemplate(taskTemplate, answerFileName, null, taskDir);
89             ApplicationManager.getApplication().invokeLater(new Runnable() {
90               @Override
91               public void run() {
92                 EditorHelper.openInEditor(taskFile, false);
93                 view.selectElement(taskFile);
94               }
95             });
96           }
97           task.addTaskFile(taskFileName + "." + extension, index);
98         }
99         catch (Exception ignored) {
100         }
101       }
102     });
103   }
104
105   @Override
106   public void update(@NotNull AnActionEvent event) {
107     if (!CCProjectService.setCCActionAvailable(event)) {
108       return;
109     }
110     final Presentation presentation = event.getPresentation();
111     final Project project = event.getData(CommonDataKeys.PROJECT);
112     if (project == null) {
113       presentation.setVisible(false);
114       presentation.setEnabled(false);
115       return;
116     }
117
118     final IdeView view = event.getData(LangDataKeys.IDE_VIEW);
119     if (view == null) {
120       presentation.setVisible(false);
121       presentation.setEnabled(false);
122       return;
123     }
124
125     final PsiDirectory[] directories = view.getDirectories();
126     if (directories.length == 0) {
127       presentation.setVisible(false);
128       presentation.setEnabled(false);
129       return;
130     }
131     final PsiDirectory directory = DirectoryChooserUtil.getOrChooseDirectory(view);
132     final CCProjectService service = CCProjectService.getInstance(project);
133     final Course course = service.getCourse();
134     if (course != null && directory != null && !directory.getName().contains("task")) {
135       presentation.setVisible(false);
136       presentation.setEnabled(false);
137       return;
138     }
139     presentation.setVisible(true);
140     presentation.setEnabled(true);
141   }
142 }