ui for switching between subtasks and simple subtask switcher
[idea/community.git] / python / educational-core / student / src / com / jetbrains / edu / learning / StudySubtaskUtils.java
1 package com.jetbrains.edu.learning;
2
3 import com.intellij.ide.projectView.ProjectView;
4 import com.intellij.openapi.project.Project;
5 import com.intellij.openapi.vfs.VirtualFile;
6 import com.intellij.ui.EditorNotifications;
7 import com.jetbrains.edu.learning.checker.StudyCheckUtils;
8 import com.jetbrains.edu.learning.core.EduNames;
9 import com.jetbrains.edu.learning.courseFormat.Task;
10 import com.jetbrains.edu.learning.courseFormat.TaskFile;
11 import com.jetbrains.edu.learning.ui.StudyToolWindow;
12 import org.jetbrains.annotations.NotNull;
13
14 import java.util.Map;
15
16 public class StudySubtaskUtils {
17   private StudySubtaskUtils() {
18   }
19
20   /***
21    * @param toSubtaskIndex from 0 to subtaskNum - 1
22    */
23   public static void switchStep(@NotNull Project project, @NotNull Task task, int toSubtaskIndex) {
24     VirtualFile taskDir = task.getTaskDir(project);
25     if (taskDir == null) {
26       return;
27     }
28     VirtualFile srcDir = taskDir.findChild(EduNames.SRC);
29     if (srcDir != null) {
30       taskDir = srcDir;
31     }
32     for (Map.Entry<String, TaskFile> entry : task.getTaskFiles().entrySet()) {
33       String name = entry.getKey();
34       VirtualFile virtualFile = taskDir.findChild(name);
35       if (virtualFile == null) {
36         continue;
37       }
38       EditorNotifications.getInstance(project).updateNotifications(virtualFile);
39     }
40     task.setActiveSubtaskIndex(toSubtaskIndex);
41     update(project, task, taskDir);
42   }
43
44   private static void update(@NotNull Project project, @NotNull Task task, VirtualFile taskDir) {
45     StudyCheckUtils.drawAllPlaceholders(project, task, taskDir);
46     ProjectView.getInstance(project).refresh();
47     StudyToolWindow toolWindow = StudyUtils.getStudyToolWindow(project);
48     if (toolWindow != null) {
49       String text = StudyUtils.getTaskTextFromTask(taskDir, task);
50       if (text == null) {
51         toolWindow.setEmptyText(project);
52         return;
53       }
54       toolWindow.setTaskText(text, taskDir, project);
55     }
56   }
57 }