fixed bugs after huge refactoring
[idea/community.git] / python / educational / interactive-learning / src / com / jetbrains / edu / learning / navigation / StudyNavigator.java
1 package com.jetbrains.edu.learning.navigation;
2
3 import com.intellij.openapi.editor.Editor;
4 import com.intellij.openapi.editor.LogicalPosition;
5 import com.intellij.openapi.project.Project;
6 import com.jetbrains.edu.courseFormat.AnswerPlaceholder;
7 import com.jetbrains.edu.courseFormat.Lesson;
8 import com.jetbrains.edu.courseFormat.Task;
9 import com.jetbrains.edu.courseFormat.TaskFile;
10 import com.jetbrains.edu.learning.StudyTaskManager;
11 import com.jetbrains.edu.learning.StudyUtils;
12 import com.jetbrains.edu.learning.courseFormat.StudyStatus;
13 import org.jetbrains.annotations.NotNull;
14
15 import java.util.List;
16
17 public class StudyNavigator {
18   private StudyNavigator() {
19
20   }
21
22   public static Task nextTask(@NotNull final Task task) {
23     Lesson currentLesson = task.getLesson();
24     List<Task> taskList = currentLesson.getTaskList();
25     if (task.getIndex() < taskList.size()) {
26       return taskList.get(task.getIndex());
27     }
28     Lesson nextLesson = nextLesson(currentLesson);
29     if (nextLesson == null) {
30       return null;
31     }
32     return StudyUtils.getFirst(nextLesson.getTaskList());
33   }
34
35   public static Task previousTask(@NotNull final Task task) {
36     Lesson currentLesson = task.getLesson();
37     int prevTaskIndex = task.getIndex() - 2;
38     if (prevTaskIndex >= 0) {
39       return currentLesson.getTaskList().get(prevTaskIndex);
40     }
41     Lesson prevLesson = previousLesson(currentLesson);
42     if (prevLesson == null) {
43       return null;
44     }
45     //getting last task in previous lesson
46     return prevLesson.getTaskList().get(prevLesson.getTaskList().size() - 1);
47   }
48
49   public static  Lesson nextLesson(@NotNull final Lesson lesson) {
50     List<Lesson> lessons = lesson.getCourse().getLessons();
51     int nextLessonIndex = lesson.getIndex();
52     if (nextLessonIndex >= lessons.size()) {
53       return null;
54     }
55     return lessons.get(nextLessonIndex);
56   }
57
58   public static  Lesson previousLesson(@NotNull final Lesson lesson) {
59     int prevLessonIndex = lesson.getIndex() - 2;
60     if (prevLessonIndex < 0) {
61       return null;
62     }
63     return lesson.getCourse().getLessons().get(prevLessonIndex);
64   }
65
66   public static void navigateToFirstFailedAnswerPlaceholder(@NotNull final Editor editor, @NotNull final TaskFile taskFile) {
67     final Project project = editor.getProject();
68     if (project == null) return;
69     for (AnswerPlaceholder answerPlaceholder : taskFile.getAnswerPlaceholders()) {
70       final StudyStatus status = StudyTaskManager.getInstance(project).getStatus(answerPlaceholder);
71       if (status != StudyStatus.Failed) {
72         continue;
73       }
74       navigateToAnswerPlaceholder(editor, answerPlaceholder, taskFile);
75       break;
76     }
77   }
78
79   public static  void navigateToAnswerPlaceholder(@NotNull final Editor editor, @NotNull final AnswerPlaceholder answerPlaceholder,
80                                                   @NotNull final TaskFile taskFile) {
81     if (!answerPlaceholder.isValid(editor.getDocument())) {
82       return;
83     }
84     taskFile.setSelectedAnswerPlaceholder(answerPlaceholder);
85     LogicalPosition placeholderStart = new LogicalPosition(answerPlaceholder.getLine(), answerPlaceholder.getStart());
86     editor.getCaretModel().moveToLogicalPosition(placeholderStart);
87   }
88
89
90   public static  void navigateToFirstAnswerPlaceholder(@NotNull final Editor editor, @NotNull final TaskFile taskFile) {
91     if (!taskFile.getAnswerPlaceholders().isEmpty()) {
92       AnswerPlaceholder firstAnswerPlaceholder = StudyUtils.getFirst(taskFile.getAnswerPlaceholders());
93       navigateToAnswerPlaceholder(editor, firstAnswerPlaceholder, taskFile);
94     }
95   }
96
97 }