EDU-537 Empty task text with opened task files
[idea/community.git] / python / educational-core / student / src / com / jetbrains / edu / learning / StudyBasePluginConfigurator.java
1 package com.jetbrains.edu.learning;
2
3 import com.intellij.openapi.actionSystem.DefaultActionGroup;
4 import com.intellij.openapi.fileEditor.FileEditorManager;
5 import com.intellij.openapi.fileEditor.FileEditorManagerEvent;
6 import com.intellij.openapi.fileEditor.FileEditorManagerListener;
7 import com.intellij.openapi.project.Project;
8 import com.intellij.openapi.vfs.VirtualFile;
9 import com.jetbrains.edu.learning.actions.*;
10 import com.jetbrains.edu.learning.courseFormat.Task;
11 import com.jetbrains.edu.learning.courseFormat.TaskFile;
12 import com.jetbrains.edu.learning.ui.StudyToolWindow;
13 import org.jetbrains.annotations.NotNull;
14 import org.jetbrains.annotations.Nullable;
15
16 import javax.swing.*;
17 import java.util.Collections;
18 import java.util.Map;
19
20 public abstract class StudyBasePluginConfigurator implements StudyPluginConfigurator {
21   @NotNull
22   @Override
23   public DefaultActionGroup getActionGroup(Project project) {
24     return getDefaultActionGroup();
25   }
26
27   @NotNull
28   public static DefaultActionGroup getDefaultActionGroup() {
29     final DefaultActionGroup group = new DefaultActionGroup();
30     group.add(new StudyPreviousStudyTaskAction());
31     group.add(new StudyNextStudyTaskAction());
32     group.add(new StudyRefreshTaskFileAction());
33     group.add(new StudyShowHintAction());
34
35     group.add(new StudyRunAction());
36     group.add(new StudyEditInputAction());
37     return group;
38   }
39
40   @NotNull
41   @Override
42   public Map<String, JPanel> getAdditionalPanels(Project project) {
43     return Collections.emptyMap();
44   }
45
46   @NotNull
47   @Override
48   public FileEditorManagerListener getFileEditorManagerListener(@NotNull Project project, @NotNull StudyToolWindow toolWindow) {
49
50     return new FileEditorManagerListener() {
51       @Override
52       public void fileOpened(@NotNull FileEditorManager source, @NotNull VirtualFile file) {
53         Task task = getTask(file);
54         setTaskText(task, file.getParent());
55       }
56
57       @Override
58       public void fileClosed(@NotNull FileEditorManager source, @NotNull VirtualFile file) {
59         for (VirtualFile openedFile : source.getOpenFiles()) {
60           if (StudyUtils.getTaskFile(project, openedFile) != null) {
61             return;
62           }
63         }
64         toolWindow.setEmptyText(project);
65       }
66
67       @Override
68       public void selectionChanged(@NotNull FileEditorManagerEvent event) {
69         VirtualFile file = event.getNewFile();
70         if (file != null) {
71           Task task = getTask(file);
72           setTaskText(task, file.getParent());
73         }
74         toolWindow.setBottomComponent(null);
75       }
76
77       @Nullable
78       private Task getTask(@NotNull VirtualFile file) {
79         TaskFile taskFile = StudyUtils.getTaskFile(project, file);
80         if (taskFile != null) {
81           return taskFile.getTask();
82         }
83         else {
84           return null;
85         }
86       }
87
88       private void setTaskText(@Nullable final Task task, @Nullable final VirtualFile taskDirectory) {
89         String text = StudyUtils.getTaskTextFromTask(taskDirectory, task);
90         if (text == null) {
91           toolWindow.setEmptyText(project);
92           return;
93         }
94         toolWindow.setTaskText(text, taskDirectory, project);
95       }
96     };
97   }
98
99   @Nullable
100   @Override
101   public StudyAfterCheckAction[] getAfterCheckActions() {
102     return null;
103   }
104 }