3902b942af009246346dd399b2cd6de49dedf35b
[idea/community.git] / python / educational-core / student / src / com / jetbrains / edu / learning / core / EduAnswerPlaceholderPainter.java
1 package com.jetbrains.edu.learning.core;
2
3 import com.intellij.openapi.editor.Document;
4 import com.intellij.openapi.editor.Editor;
5 import com.intellij.openapi.editor.RangeMarker;
6 import com.intellij.openapi.editor.colors.EditorColorsManager;
7 import com.intellij.openapi.editor.colors.EditorColorsScheme;
8 import com.intellij.openapi.editor.impl.DocumentImpl;
9 import com.intellij.openapi.editor.markup.*;
10 import com.intellij.openapi.project.Project;
11 import com.intellij.ui.JBColor;
12 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
13 import com.jetbrains.edu.learning.courseFormat.TaskFile;
14 import org.jetbrains.annotations.NotNull;
15
16 import java.awt.*;
17 import java.util.List;
18
19 public class EduAnswerPlaceholderPainter {
20
21   //it should be the lowest highlighting layer, otherwise selection and other effects are not visible
22   public static final int PLACEHOLDERS_LAYER = 0;
23
24   private EduAnswerPlaceholderPainter() {
25
26   }
27
28   public static void drawAnswerPlaceholder(@NotNull final Editor editor, @NotNull final AnswerPlaceholder placeholder,
29                                            @NotNull final JBColor color) {
30     final Document document = editor.getDocument();
31     if (!placeholder.isValid(document)) {
32       return;
33     }
34     EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
35     final TextAttributes textAttributes = new TextAttributes(scheme.getDefaultForeground(), scheme.getDefaultBackground(), null,
36                                                                     EffectType.BOXED, Font.PLAIN);
37     final Project project = editor.getProject();
38     assert project != null;
39     final int startOffset = placeholder.getOffset();
40     if (startOffset == - 1) {
41       return;
42     }
43     final int length = placeholder.getRealLength();
44     final int endOffset = startOffset + length;
45     textAttributes.setEffectColor(color);
46     RangeHighlighter
47       highlighter = editor.getMarkupModel().addRangeHighlighter(startOffset, endOffset, PLACEHOLDERS_LAYER,
48                                                                 textAttributes, HighlighterTargetArea.EXACT_RANGE);
49     highlighter.setGreedyToLeft(true);
50     highlighter.setGreedyToRight(true);
51   }
52
53   public static void createGuardedBlock(Editor editor, List<RangeMarker> blocks, int start, int end) {
54     RangeHighlighter rh = editor.getMarkupModel()
55       .addRangeHighlighter(start, end, PLACEHOLDERS_LAYER, null, HighlighterTargetArea.EXACT_RANGE);
56     blocks.add(rh);
57   }
58
59
60   public static void createGuardedBlocks(@NotNull final Editor editor, TaskFile taskFile) {
61     for (AnswerPlaceholder answerPlaceholder : taskFile.getAnswerPlaceholders()) {
62       createGuardedBlocks(editor, answerPlaceholder);
63     }
64   }
65
66   public static void createGuardedBlocks(@NotNull final Editor editor, AnswerPlaceholder placeholder) {
67     Document document = editor.getDocument();
68     if (document instanceof DocumentImpl) {
69       DocumentImpl documentImpl = (DocumentImpl)document;
70       List<RangeMarker> blocks = documentImpl.getGuardedBlocks();
71       if (!placeholder.isValid(document)) return;
72       int start = placeholder.getOffset();
73       final int length = placeholder.getRealLength();
74       int end = start + length;
75       if (start != 0) {
76         createGuardedBlock(editor, blocks, start - 1, start);
77       }
78       if (end != document.getTextLength()) {
79         createGuardedBlock(editor, blocks, end, end + 1);
80       }
81     }
82   }
83
84 }