ui for switching between subtasks and simple subtask switcher
[idea/community.git] / python / educational-core / course-creator / src / com / jetbrains / edu / coursecreator / actions / CCNewSubtaskAction.java
1 package com.jetbrains.edu.coursecreator.actions;
2
3 import com.intellij.ide.fileTemplates.FileTemplate;
4 import com.intellij.ide.fileTemplates.FileTemplateManager;
5 import com.intellij.ide.fileTemplates.FileTemplateUtil;
6 import com.intellij.openapi.actionSystem.AnActionEvent;
7 import com.intellij.openapi.actionSystem.CommonDataKeys;
8 import com.intellij.openapi.actionSystem.DataContext;
9 import com.intellij.openapi.actionSystem.Presentation;
10 import com.intellij.openapi.diagnostic.Logger;
11 import com.intellij.openapi.editor.Editor;
12 import com.intellij.openapi.project.DumbAwareAction;
13 import com.intellij.openapi.project.Project;
14 import com.intellij.openapi.util.io.FileUtil;
15 import com.intellij.openapi.util.io.FileUtilRt;
16 import com.intellij.openapi.vfs.VirtualFile;
17 import com.intellij.psi.PsiDirectory;
18 import com.intellij.psi.PsiManager;
19 import com.jetbrains.edu.coursecreator.CCLanguageManager;
20 import com.jetbrains.edu.coursecreator.CCUtils;
21 import com.jetbrains.edu.coursecreator.settings.CCSettings;
22 import com.jetbrains.edu.learning.StudySubtaskUtils;
23 import com.jetbrains.edu.learning.StudyTaskManager;
24 import com.jetbrains.edu.learning.StudyUtils;
25 import com.jetbrains.edu.learning.core.EduNames;
26 import com.jetbrains.edu.learning.courseFormat.Course;
27 import com.jetbrains.edu.learning.courseFormat.Task;
28 import com.jetbrains.edu.learning.courseFormat.TaskFile;
29 import org.jetbrains.annotations.NotNull;
30
31
32 public class CCNewSubtaskAction extends DumbAwareAction {
33   private static final Logger LOG = Logger.getInstance(CCNewSubtaskAction.class);
34   public static final String NEW_SUBTASK = "New Subtask";
35
36   public CCNewSubtaskAction() {
37     super(NEW_SUBTASK);
38   }
39
40   @Override
41   public void actionPerformed(AnActionEvent e) {
42     DataContext dataContext = e.getDataContext();
43     VirtualFile virtualFile = CommonDataKeys.VIRTUAL_FILE.getData(dataContext);
44     Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
45     Project project = CommonDataKeys.PROJECT.getData(dataContext);
46     if (virtualFile == null || project == null || editor == null) {
47       return;
48     }
49     addSubtask(virtualFile, project);
50   }
51
52   public static void addSubtask(@NotNull VirtualFile virtualFile, @NotNull Project project) {
53     TaskFile taskFile = StudyUtils.getTaskFile(project, virtualFile);
54     if (taskFile == null) {
55       return;
56     }
57     VirtualFile taskDir = StudyUtils.getTaskDir(virtualFile);
58     if (taskDir == null) {
59       return;
60     }
61     Task task = taskFile.getTask();
62     createTestsForNewSubtask(project, task);
63     int num = task.getSubtaskNum();
64     createTaskDescriptionFile(project, taskDir, num);
65     task.setSubtaskNum(num + 1);
66     task.setActiveSubtaskIndex(num);
67     StudySubtaskUtils.switchStep(project, task, num);
68   }
69
70   private static void createTestsForNewSubtask(Project project, Task task) {
71     Course course = StudyTaskManager.getInstance(project).getCourse();
72     if (course == null) {
73       return;
74     }
75     CCLanguageManager manager = CCUtils.getStudyLanguageManager(course);
76     if (manager == null) {
77       return;
78     }
79     manager.createTestsForNewSubtask(project, task);
80   }
81
82   private static void createTaskDescriptionFile(Project project, VirtualFile taskDir, int index) {
83     String taskDescriptionFileName = StudyUtils.getTaskDescriptionFileName(CCSettings.getInstance().useHtmlAsDefaultTaskFormat());
84     FileTemplate taskTextTemplate = FileTemplateManager.getInstance(project).getInternalTemplate(taskDescriptionFileName);
85     PsiDirectory taskPsiDir = PsiManager.getInstance(project).findDirectory(taskDir);
86     if (taskTextTemplate != null && taskPsiDir != null) {
87       String nextTaskTextName = FileUtil.getNameWithoutExtension(taskDescriptionFileName) +
88                                 EduNames.SUBTASK_MARKER +
89                                 index + "." +
90                                 FileUtilRt.getExtension(taskDescriptionFileName);
91       try {
92         FileTemplateUtil.createFromTemplate(taskTextTemplate, nextTaskTextName, null, taskPsiDir);
93       }
94       catch (Exception e) {
95         LOG.error(e);
96       }
97     }
98   }
99
100   @Override
101   public void update(AnActionEvent e) {
102     DataContext dataContext = e.getDataContext();
103     Presentation presentation = e.getPresentation();
104     presentation.setEnabledAndVisible(false);
105     VirtualFile virtualFile = CommonDataKeys.VIRTUAL_FILE.getData(dataContext);
106     Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
107     Project project = CommonDataKeys.PROJECT.getData(dataContext);
108     if (virtualFile == null || project == null || editor == null) {
109       return;
110     }
111     if (CCUtils.isCourseCreator(project) && StudyUtils.getTaskForFile(project, virtualFile) != null) {
112       presentation.setEnabledAndVisible(true);
113     }
114   }
115 }