remove usages of redundant CCProjectService
[idea/community.git] / python / educational-core / 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.learning.StudyTaskManager;
23 import com.jetbrains.edu.learning.core.EduNames;
24 import com.jetbrains.edu.learning.courseFormat.Course;
25 import com.jetbrains.edu.learning.courseFormat.Lesson;
26 import com.jetbrains.edu.learning.courseFormat.Task;
27 import com.jetbrains.edu.coursecreator.CCLanguageManager;
28 import com.jetbrains.edu.coursecreator.CCUtils;
29 import com.jetbrains.edu.coursecreator.ui.CreateTaskFileDialog;
30 import org.jetbrains.annotations.NotNull;
31
32 import static com.intellij.openapi.ui.DialogWrapper.OK_EXIT_CODE;
33
34 public class CCCreateTaskFile extends DumbAwareAction {
35
36   public CCCreateTaskFile() {
37     super("Task File", "Create new Task File", AllIcons.FileTypes.Text);
38   }
39
40   @Override
41   public void actionPerformed(final AnActionEvent e) {
42     final IdeView view = e.getData(LangDataKeys.IDE_VIEW);
43     final Project project = e.getData(CommonDataKeys.PROJECT);
44
45     if (view == null || project == null) {
46       return;
47     }
48     final PsiDirectory taskDir = DirectoryChooserUtil.getOrChooseDirectory(view);
49     if (taskDir == null) return;
50     PsiDirectory lessonDir = taskDir.getParent();
51     if (lessonDir == null) {
52       return;
53     }
54     final Course course = StudyTaskManager.getInstance(project).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 name = dialog.getFileName();
66     if (name == null) return;
67     FileType type = dialog.getFileType();
68     if (type == null) {
69       return;
70     }
71     final CCLanguageManager CCLanguageManager = CCUtils.getStudyLanguageManager(course);
72     if (CCLanguageManager == 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 = CCLanguageManager.getTaskFileTemplateForExtension(project, extension);
80         final String taskFileName = name + "." + extension;
81         try {
82           if (taskTemplate == null) {
83             VirtualFile file = taskDir.getVirtualFile().createChildData(this, taskFileName);
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, taskFileName, 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, index);
98         }
99         catch (Exception ignored) {
100         }
101       }
102     });
103   }
104
105   @Override
106   public void update(@NotNull AnActionEvent event) {
107     final Presentation presentation = event.getPresentation();
108     presentation.setEnabledAndVisible(false);
109     final Project project = event.getData(CommonDataKeys.PROJECT);
110     if (project == null) {
111       return;
112     }
113     final IdeView view = event.getData(LangDataKeys.IDE_VIEW);
114     if (view == null) {
115       return;
116     }
117
118     final PsiDirectory[] directories = view.getDirectories();
119     if (directories.length == 0) {
120       return;
121     }
122     final PsiDirectory directory = DirectoryChooserUtil.getOrChooseDirectory(view);
123     final Course course = StudyTaskManager.getInstance(project).getCourse();
124     if (course != null && directory != null && !directory.getName().contains(EduNames.TASK)) {
125       return;
126     }
127     presentation.setEnabledAndVisible(true);
128   }
129 }