Timer replaced with JobScheduler to reduce extra threads created
[idea/community.git] / python / educational-core / student / src / com / jetbrains / edu / learning / actions / StudyWindowNavigationAction.java
1 package com.jetbrains.edu.learning.actions;
2
3 import com.intellij.openapi.actionSystem.AnActionEvent;
4 import com.intellij.openapi.editor.Editor;
5 import com.intellij.openapi.editor.LogicalPosition;
6 import com.intellij.openapi.fileEditor.FileDocumentManager;
7 import com.intellij.openapi.project.DumbAware;
8 import com.intellij.openapi.project.Project;
9 import com.intellij.openapi.vfs.VirtualFile;
10 import com.jetbrains.edu.courseFormat.AnswerPlaceholder;
11 import com.jetbrains.edu.courseFormat.TaskFile;
12 import com.jetbrains.edu.learning.StudyUtils;
13 import com.jetbrains.edu.learning.navigation.StudyNavigator;
14 import org.jetbrains.annotations.NotNull;
15 import org.jetbrains.annotations.Nullable;
16
17 import javax.swing.*;
18
19 abstract public class StudyWindowNavigationAction extends StudyToolbarAction implements DumbAware {
20
21   protected StudyWindowNavigationAction(String actionId, String description, Icon icon) {
22     super(actionId, description, icon);
23   }
24
25   private void navigateToPlaceholder(@NotNull final Project project) {
26       final Editor selectedEditor = StudyUtils.getSelectedEditor(project);
27       if (selectedEditor != null) {
28         final FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
29         final VirtualFile openedFile = fileDocumentManager.getFile(selectedEditor.getDocument());
30         if (openedFile != null) {
31           final TaskFile selectedTaskFile = StudyUtils.getTaskFile(project, openedFile);
32           if (selectedTaskFile != null) {
33             final AnswerPlaceholder selectedAnswerPlaceholder = getSelectedAnswerPlaceholder(selectedEditor, selectedTaskFile);
34             if (selectedAnswerPlaceholder == null) {
35               return;
36             }
37             final AnswerPlaceholder nextAnswerPlaceholder = getNextAnswerPlaceholder(selectedAnswerPlaceholder);
38             if (nextAnswerPlaceholder == null) {
39               return;
40             }
41             StudyNavigator.navigateToAnswerPlaceholder(selectedEditor, nextAnswerPlaceholder, selectedTaskFile);
42             selectedEditor.getSelectionModel().removeSelection();
43             }
44           }
45         }
46       }
47
48   @Nullable
49   private static AnswerPlaceholder getSelectedAnswerPlaceholder(@NotNull final Editor editor, @NotNull final TaskFile file) {
50     LogicalPosition position = editor.getCaretModel().getLogicalPosition();
51     return file.getAnswerPlaceholder(editor.getDocument(), position);
52   }
53
54   @Nullable
55   protected abstract AnswerPlaceholder getNextAnswerPlaceholder(@NotNull final AnswerPlaceholder window);
56
57   @Override
58   public void actionPerformed(@NotNull AnActionEvent e) {
59     final Project project = e.getProject();
60     if (project == null) {
61       return;
62     }
63     navigateToPlaceholder(project);
64   }
65
66   @Override
67   public void update(@NotNull AnActionEvent e) {
68     StudyUtils.updateAction(e);
69   }
70 }