fix to support subtasks in java
[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     Task task = taskFile.getTask();
58     VirtualFile taskDir = task.getTaskDir(project);
59     if (taskDir == null) {
60       return;
61     }
62     createTestsForNewSubtask(project, task);
63     int num = task.getSubtaskNum();
64     createTaskDescriptionFile(project, taskDir, num);
65     task.setSubtaskNum(num + 1);
66     StudySubtaskUtils.switchStep(project, task, num);
67   }
68
69   private static void createTestsForNewSubtask(Project project, Task task) {
70     Course course = StudyTaskManager.getInstance(project).getCourse();
71     if (course == null) {
72       return;
73     }
74     CCLanguageManager manager = CCUtils.getStudyLanguageManager(course);
75     if (manager == null) {
76       return;
77     }
78     manager.createTestsForNewSubtask(project, task);
79   }
80
81   private static void createTaskDescriptionFile(Project project, VirtualFile taskDir, int index) {
82     String taskDescriptionFileName = StudyUtils.getTaskDescriptionFileName(CCSettings.getInstance().useHtmlAsDefaultTaskFormat());
83     FileTemplate taskTextTemplate = FileTemplateManager.getInstance(project).getInternalTemplate(taskDescriptionFileName);
84     PsiDirectory taskPsiDir = PsiManager.getInstance(project).findDirectory(taskDir);
85     if (taskTextTemplate != null && taskPsiDir != null) {
86       String nextTaskTextName = FileUtil.getNameWithoutExtension(taskDescriptionFileName) +
87                                 EduNames.SUBTASK_MARKER +
88                                 index + "." +
89                                 FileUtilRt.getExtension(taskDescriptionFileName);
90       try {
91         FileTemplateUtil.createFromTemplate(taskTextTemplate, nextTaskTextName, null, taskPsiDir);
92       }
93       catch (Exception e) {
94         LOG.error(e);
95       }
96     }
97   }
98
99   @Override
100   public void update(AnActionEvent e) {
101     DataContext dataContext = e.getDataContext();
102     Presentation presentation = e.getPresentation();
103     presentation.setEnabledAndVisible(false);
104     VirtualFile virtualFile = CommonDataKeys.VIRTUAL_FILE.getData(dataContext);
105     Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
106     Project project = CommonDataKeys.PROJECT.getData(dataContext);
107     if (virtualFile == null || project == null || editor == null) {
108       return;
109     }
110     if (CCUtils.isCourseCreator(project) && StudyUtils.getTaskForFile(project, virtualFile) != null) {
111       presentation.setEnabledAndVisible(true);
112     }
113   }
114 }