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