fixed EDU-328 Answer placeholder ranges are removable after first check
[idea/community.git] / python / edu / learn-python / src / com / jetbrains / python / edu / StudyEditorFactoryListener.java
1 package com.jetbrains.python.edu;
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.jetbrains.python.edu.course.TaskFile;
16 import com.jetbrains.python.edu.course.TaskWindow;
17 import com.jetbrains.python.edu.editor.StudyEditor;
18 import org.jetbrains.annotations.NotNull;
19
20 import java.awt.*;
21
22
23 class StudyEditorFactoryListener implements EditorFactoryListener {
24
25   /**
26    * draws selected task window if there is one located in mouse position
27    */
28   private static class WindowSelectionListener extends EditorMouseAdapter {
29     private final TaskFile myTaskFile;
30
31     WindowSelectionListener(TaskFile taskFile) {
32       myTaskFile = taskFile;
33     }
34
35     @Override
36     public void mouseClicked(EditorMouseEvent e) {
37       Editor editor = e.getEditor();
38       Point point = e.getMouseEvent().getPoint();
39       LogicalPosition pos = editor.xyToLogicalPosition(point);
40       TaskWindow taskWindow = myTaskFile.getTaskWindow(editor.getDocument(), pos);
41       if (taskWindow != null) {
42         myTaskFile.setSelectedTaskWindow(taskWindow);
43         taskWindow.draw(editor, false, false);
44       }
45       else {
46         myTaskFile.drawAllWindows(editor);
47       }
48     }
49   }
50
51   @Override
52   public void editorCreated(@NotNull final EditorFactoryEvent event) {
53     final Editor editor = event.getEditor();
54
55     final Project project = editor.getProject();
56     if (project == null) {
57       return;
58     }
59     ApplicationManager.getApplication().invokeLater(
60       new Runnable() {
61         @Override
62         public void run() {
63           ApplicationManager.getApplication().runWriteAction(new Runnable() {
64             @Override
65             public void run() {
66               Document document = editor.getDocument();
67               VirtualFile openedFile = FileDocumentManager.getInstance().getFile(document);
68               if (openedFile != null) {
69                 StudyTaskManager taskManager = StudyTaskManager.getInstance(project);
70                 TaskFile taskFile = taskManager.getTaskFile(openedFile);
71                 if (taskFile != null) {
72                   taskFile.navigateToFirstTaskWindow(editor);
73                   editor.addEditorMouseListener(new WindowSelectionListener(taskFile));
74                   StudyDocumentListener listener = new StudyDocumentListener(taskFile);
75                   StudyEditor.addDocumentListener(document, listener);
76                   document.addDocumentListener(listener);
77                   taskFile.drawAllWindows(editor);
78                 }
79               }
80             }
81           });
82         }
83       }
84     );
85   }
86
87   @Override
88   public void editorReleased(@NotNull EditorFactoryEvent event) {
89     Editor editor = event.getEditor();
90     Document document = editor.getDocument();
91     StudyDocumentListener listener = StudyEditor.getListener(document);
92     if (listener != null) {
93       document.removeDocumentListener(listener);
94       StudyEditor.removeListener(document);
95     }
96     editor.getMarkupModel().removeAllHighlighters();
97     editor.getSelectionModel().removeSelection();
98   }
99 }