9a1eb88859f280c2131e6028573090513d03dde6
[idea/community.git] / python / educational-core / student / src / com / jetbrains / edu / learning / editor / StudyEditorFactoryListener.java
1 package com.jetbrains.edu.learning.editor;
2
3
4 import com.intellij.openapi.application.ApplicationManager;
5 import com.intellij.openapi.editor.Document;
6 import com.intellij.openapi.editor.Editor;
7 import com.intellij.openapi.editor.LogicalPosition;
8 import com.intellij.openapi.editor.event.EditorFactoryEvent;
9 import com.intellij.openapi.editor.event.EditorFactoryListener;
10 import com.intellij.openapi.editor.event.EditorMouseAdapter;
11 import com.intellij.openapi.editor.event.EditorMouseEvent;
12 import com.intellij.openapi.fileEditor.FileDocumentManager;
13 import com.intellij.openapi.project.Project;
14 import com.intellij.openapi.vfs.VirtualFile;
15 import com.intellij.openapi.wm.ToolWindow;
16 import com.intellij.openapi.wm.ToolWindowManager;
17 import com.intellij.problems.WolfTheProblemSolver;
18 import com.jetbrains.edu.learning.core.EduDocumentListener;
19 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
20 import com.jetbrains.edu.learning.courseFormat.TaskFile;
21 import com.jetbrains.edu.learning.StudyUtils;
22 import com.jetbrains.edu.learning.navigation.StudyNavigator;
23 import com.jetbrains.edu.learning.ui.StudyToolWindowFactory;
24 import org.jetbrains.annotations.NotNull;
25
26 import java.awt.*;
27
28
29 public class StudyEditorFactoryListener implements EditorFactoryListener {
30
31   private static class WindowSelectionListener extends EditorMouseAdapter {
32     private final TaskFile myTaskFile;
33
34     WindowSelectionListener(@NotNull final TaskFile taskFile) {
35       myTaskFile = taskFile;
36     }
37
38     @Override
39     public void mouseClicked(EditorMouseEvent e) {
40       final Editor editor = e.getEditor();
41       final Point point = e.getMouseEvent().getPoint();
42       final LogicalPosition pos = editor.xyToLogicalPosition(point);
43       final AnswerPlaceholder answerPlaceholder = myTaskFile.getAnswerPlaceholder(editor.getDocument(), pos);
44       if (answerPlaceholder == null || answerPlaceholder.getSelected()) {
45         return;
46       }
47       int startOffset = answerPlaceholder.getRealStartOffset(editor.getDocument());
48       editor.getSelectionModel().setSelection(startOffset, startOffset + answerPlaceholder.getLength());
49       answerPlaceholder.setSelected(true);
50     }
51   }
52
53   @Override
54   public void editorCreated(@NotNull final EditorFactoryEvent event) {
55     final Editor editor = event.getEditor();
56
57     final Project project = editor.getProject();
58     if (project == null) {
59       return;
60     }
61     ApplicationManager.getApplication().invokeLater(
62       new Runnable() {
63         @Override
64         public void run() {
65           ApplicationManager.getApplication().runWriteAction(new Runnable() {
66             @Override
67             public void run() {
68               final Document document = editor.getDocument();
69               final VirtualFile openedFile = FileDocumentManager.getInstance().getFile(document);
70               if (openedFile != null) {
71                 final TaskFile taskFile = StudyUtils.getTaskFile(project, openedFile);
72                 if (taskFile != null) {
73                   WolfTheProblemSolver.getInstance(project).clearProblems(openedFile);
74                   final ToolWindow studyToolWindow = ToolWindowManager.getInstance(project).getToolWindow(StudyToolWindowFactory.STUDY_TOOL_WINDOW);
75                   if (studyToolWindow != null) {
76                     StudyUtils.updateToolWindows(project);
77                     studyToolWindow.show(null);
78                   }
79                   if (!taskFile.getAnswerPlaceholders().isEmpty()) {
80                     StudyNavigator.navigateToFirstAnswerPlaceholder(editor, taskFile);
81                     StudyEditor.addDocumentListener(document, new EduDocumentListener(taskFile));
82                     StudyUtils.drawAllWindows(editor, taskFile);
83                     editor.addEditorMouseListener(new WindowSelectionListener(taskFile));
84                   }
85                 }
86               }
87             }
88           });
89         }
90       }
91     );
92   }
93
94   @Override
95   public void editorReleased(@NotNull EditorFactoryEvent event) {
96     final Editor editor = event.getEditor();
97     final Document document = editor.getDocument();
98     StudyEditor.removeListener(document);
99     editor.getMarkupModel().removeAllHighlighters();
100     editor.getSelectionModel().removeSelection();
101   }
102 }