update copyrights
[fedora-idea.git] / platform / lang-impl / src / com / intellij / codeInsight / intention / impl / config / IntentionUsagePanel.java
blob1346a0ede87de6044807b59cea07e6c48e93996f
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /**
18 * @author cdr
20 package com.intellij.codeInsight.intention.impl.config;
22 import com.intellij.openapi.application.ApplicationManager;
23 import com.intellij.openapi.command.CommandProcessor;
24 import com.intellij.openapi.editor.*;
25 import com.intellij.openapi.editor.colors.CodeInsightColors;
26 import com.intellij.openapi.editor.colors.EditorColorsManager;
27 import com.intellij.openapi.editor.colors.EditorColorsScheme;
28 import com.intellij.openapi.editor.ex.EditorEx;
29 import com.intellij.openapi.editor.highlighter.EditorHighlighterFactory;
30 import com.intellij.openapi.editor.markup.TextAttributes;
31 import com.intellij.openapi.fileTypes.FileType;
32 import com.intellij.openapi.util.text.StringUtil;
33 import com.intellij.util.ui.RangeBlinker;
34 import org.jetbrains.annotations.NonNls;
36 import javax.swing.*;
37 import java.awt.*;
38 import java.util.ArrayList;
39 import java.util.List;
41 class IntentionUsagePanel extends JPanel{
42 private final EditorEx myEditor;
43 @NonNls private static final String SPOT_MARKER = "spot";
44 private final RangeBlinker myRangeBlinker;
46 public IntentionUsagePanel() {
47 myEditor = (EditorEx)createEditor("", 10, 3, -1);
48 setLayout(new BorderLayout());
49 add(myEditor.getComponent(), BorderLayout.CENTER);
50 TextAttributes blinkAttributes = EditorColorsManager.getInstance().getGlobalScheme().getAttributes(CodeInsightColors.BLINKING_HIGHLIGHTS_ATTRIBUTES);
51 myRangeBlinker = new RangeBlinker(myEditor, blinkAttributes, Integer.MAX_VALUE);
54 public void reset(final String usageText, final FileType fileType) {
55 reinitViews();
56 SwingUtilities.invokeLater(new Runnable() {
57 public void run() {
58 CommandProcessor.getInstance().runUndoTransparentAction(new Runnable() {
59 public void run() {
60 ApplicationManager.getApplication().runWriteAction(new Runnable() {
61 public void run() {
62 configureByText(usageText, fileType);
64 });
66 });
68 });
71 private void configureByText(final String usageText, FileType fileType) {
72 Document document = myEditor.getDocument();
73 String text = StringUtil.convertLineSeparators(usageText);
74 document.replaceString(0, document.getTextLength(), text);
75 final EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
76 myEditor.setHighlighter(EditorHighlighterFactory.getInstance().createEditorHighlighter(fileType, scheme, null));
77 setupSpots(document);
80 private void setupSpots(Document document) {
81 List<RangeMarker> markers = new ArrayList<RangeMarker>();
82 while (true) {
83 String text = document.getText();
84 int spotStart = text.indexOf("<" + SPOT_MARKER + ">");
85 if (spotStart < 0) break;
86 int spotEnd = text.indexOf("</" + SPOT_MARKER + ">", spotStart);
87 if (spotEnd < 0) break;
89 document.deleteString(spotEnd, spotEnd + SPOT_MARKER.length() + 3);
90 document.deleteString(spotStart, spotStart + SPOT_MARKER.length() + 2);
91 final RangeMarker spotMarker = document.createRangeMarker(spotStart, spotEnd - SPOT_MARKER.length() - 2);
92 if (spotMarker == null) {
93 break;
95 else {
96 markers.add(spotMarker);
99 myRangeBlinker.resetMarkers(markers);
100 if (!markers.isEmpty()) {
101 myRangeBlinker.startBlinking();
105 public void dispose() {
106 myRangeBlinker.stopBlinking();
107 EditorFactory editorFactory = EditorFactory.getInstance();
108 editorFactory.releaseEditor(myEditor);
111 private void reinitViews() {
112 myEditor.reinitSettings();
113 myEditor.getMarkupModel().removeAllHighlighters();
116 private static Editor createEditor(String text, int column, int line, int selectedLine) {
117 EditorFactory editorFactory = EditorFactory.getInstance();
118 Document editorDocument = editorFactory.createDocument(text);
119 EditorEx editor = (EditorEx)editorFactory.createViewer(editorDocument);
120 EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
121 editor.setColorsScheme(scheme);
122 EditorSettings settings = editor.getSettings();
123 settings.setWhitespacesShown(true);
124 settings.setLineMarkerAreaShown(false);
125 settings.setFoldingOutlineShown(false);
126 settings.setAdditionalColumnsCount(0);
127 settings.setAdditionalLinesCount(0);
128 settings.setRightMarginShown(true);
129 settings.setRightMargin(60);
131 LogicalPosition pos = new LogicalPosition(line, column);
132 editor.getCaretModel().moveToLogicalPosition(pos);
133 if (selectedLine >= 0) {
134 editor.getSelectionModel().setSelection(editorDocument.getLineStartOffset(selectedLine),
135 editorDocument.getLineEndOffset(selectedLine));
138 return editor;