blob: b781e7da88492e14728faba4597c1e24027631ec (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
package com.jetbrains.python.edu.actions;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.ui.popup.Balloon;
import com.intellij.openapi.ui.popup.BalloonBuilder;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.vfs.VirtualFile;
import com.jetbrains.python.edu.StudyTaskManager;
import com.jetbrains.python.edu.course.Lesson;
import com.jetbrains.python.edu.course.Task;
import com.jetbrains.python.edu.course.TaskFile;
import com.jetbrains.python.edu.editor.StudyEditor;
import javax.swing.*;
import java.util.Map;
/**
* author: liana
* data: 7/21/14.
*/
abstract public class StudyTaskNavigationAction extends DumbAwareAction {
public void navigateTask(Project project) {
Editor selectedEditor = StudyEditor.getSelectedEditor(project);
FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
assert selectedEditor != null;
VirtualFile openedFile = fileDocumentManager.getFile(selectedEditor.getDocument());
StudyTaskManager taskManager = StudyTaskManager.getInstance(project);
assert openedFile != null;
TaskFile selectedTaskFile = taskManager.getTaskFile(openedFile);
assert selectedTaskFile != null;
Task currentTask = selectedTaskFile.getTask();
Task nextTask = getTargetTask(currentTask);
if (nextTask == null) {
BalloonBuilder balloonBuilder =
JBPopupFactory.getInstance().createHtmlTextBalloonBuilder(getNavigationFinishedMessage(), MessageType.INFO, null);
Balloon balloon = balloonBuilder.createBalloon();
StudyEditor selectedStudyEditor = StudyEditor.getSelectedStudyEditor(project);
balloon.showInCenterOf(getButton(selectedStudyEditor));
return;
}
for (VirtualFile file : FileEditorManager.getInstance(project).getOpenFiles()) {
FileEditorManager.getInstance(project).closeFile(file);
}
int nextTaskIndex = nextTask.getIndex();
int lessonIndex = nextTask.getLesson().getIndex();
Map<String, TaskFile> nextTaskFiles = nextTask.getTaskFiles();
if (nextTaskFiles.isEmpty()) {
return;
}
VirtualFile projectDir = project.getBaseDir();
String lessonDirName = Lesson.LESSON_DIR + String.valueOf(lessonIndex + 1);
if (projectDir == null) {
return;
}
VirtualFile lessonDir = projectDir.findChild(lessonDirName);
if (lessonDir == null) {
return;
}
String taskDirName = Task.TASK_DIR + String.valueOf(nextTaskIndex + 1);
VirtualFile taskDir = lessonDir.findChild(taskDirName);
if (taskDir == null) {
return;
}
VirtualFile shouldBeActive = null;
for (Map.Entry<String, TaskFile> entry : nextTaskFiles.entrySet()) {
String name = entry.getKey();
TaskFile taskFile = entry.getValue();
VirtualFile vf = taskDir.findChild(name);
if (vf != null) {
FileEditorManager.getInstance(project).openFile(vf, true);
if (!taskFile.getTaskWindows().isEmpty()) {
shouldBeActive = vf;
}
}
}
if (shouldBeActive != null) {
FileEditorManager.getInstance(project).openFile(shouldBeActive, true);
}
}
protected abstract JButton getButton(StudyEditor selectedStudyEditor);
@Override
public void actionPerformed(AnActionEvent e) {
navigateTask(e.getProject());
}
protected abstract String getNavigationFinishedMessage();
protected abstract Task getTargetTask(Task sourceTask);
}
|