/platform/lang-impl/src/com/intellij/codeInsight/intention/impl/config/IntentionUsagePanel.java

https://bitbucket.org/nbargnesi/idea · Java · 152 lines · 118 code · 16 blank · 18 comment · 6 complexity · 0ed8a316d13fbe391f490e1ac4b7790e MD5 · raw file

  1. /*
  2. * Copyright 2000-2009 JetBrains s.r.o.
  3. *
  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
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  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.
  15. */
  16. /**
  17. * @author cdr
  18. */
  19. package com.intellij.codeInsight.intention.impl.config;
  20. import com.intellij.openapi.application.ApplicationManager;
  21. import com.intellij.openapi.command.CommandProcessor;
  22. import com.intellij.openapi.editor.*;
  23. import com.intellij.openapi.editor.colors.CodeInsightColors;
  24. import com.intellij.openapi.editor.colors.EditorColorsManager;
  25. import com.intellij.openapi.editor.colors.EditorColorsScheme;
  26. import com.intellij.openapi.editor.ex.EditorEx;
  27. import com.intellij.openapi.editor.highlighter.EditorHighlighterFactory;
  28. import com.intellij.openapi.editor.markup.TextAttributes;
  29. import com.intellij.openapi.fileTypes.FileType;
  30. import com.intellij.openapi.util.Segment;
  31. import com.intellij.openapi.util.text.StringUtil;
  32. import com.intellij.util.ui.RangeBlinker;
  33. import org.jetbrains.annotations.NonNls;
  34. import javax.swing.*;
  35. import java.awt.*;
  36. import java.util.ArrayList;
  37. import java.util.List;
  38. class IntentionUsagePanel extends JPanel{
  39. private final EditorEx myEditor;
  40. @NonNls private static final String SPOT_MARKER = "spot";
  41. private final RangeBlinker myRangeBlinker;
  42. public IntentionUsagePanel() {
  43. myEditor = (EditorEx)createEditor("", 10, 3, -1);
  44. setLayout(new BorderLayout());
  45. add(myEditor.getComponent(), BorderLayout.CENTER);
  46. TextAttributes blinkAttributes = EditorColorsManager.getInstance().getGlobalScheme().getAttributes(CodeInsightColors.BLINKING_HIGHLIGHTS_ATTRIBUTES);
  47. myRangeBlinker = new RangeBlinker(myEditor, blinkAttributes, Integer.MAX_VALUE);
  48. }
  49. public void reset(final String usageText, final FileType fileType) {
  50. reinitViews();
  51. SwingUtilities.invokeLater(new Runnable() {
  52. @Override
  53. public void run() {
  54. if (myEditor.isDisposed()) return;
  55. CommandProcessor.getInstance().runUndoTransparentAction(new Runnable() {
  56. @Override
  57. public void run() {
  58. ApplicationManager.getApplication().runWriteAction(new Runnable() {
  59. @Override
  60. public void run() {
  61. configureByText(usageText, fileType);
  62. }
  63. });
  64. }
  65. });
  66. }
  67. });
  68. }
  69. private void configureByText(final String usageText, FileType fileType) {
  70. Document document = myEditor.getDocument();
  71. String text = StringUtil.convertLineSeparators(usageText);
  72. document.replaceString(0, document.getTextLength(), text);
  73. final EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
  74. myEditor.setHighlighter(EditorHighlighterFactory.getInstance().createEditorHighlighter(fileType, scheme, null));
  75. setupSpots(document);
  76. }
  77. private void setupSpots(Document document) {
  78. List<Segment> markers = new ArrayList<Segment>();
  79. while (true) {
  80. String text = document.getText();
  81. final int spotStart = text.indexOf("<" + SPOT_MARKER + ">");
  82. if (spotStart < 0) break;
  83. final int spotEnd = text.indexOf("</" + SPOT_MARKER + ">", spotStart);
  84. if (spotEnd < 0) break;
  85. document.deleteString(spotEnd, spotEnd + SPOT_MARKER.length() + 3);
  86. document.deleteString(spotStart, spotStart + SPOT_MARKER.length() + 2);
  87. Segment spotMarker = new Segment() {
  88. @Override
  89. public int getStartOffset() {
  90. return spotStart;
  91. }
  92. @Override
  93. public int getEndOffset() {
  94. return spotEnd - SPOT_MARKER.length() - 2;
  95. }
  96. };
  97. markers.add(spotMarker);
  98. }
  99. myRangeBlinker.resetMarkers(markers);
  100. if (!markers.isEmpty()) {
  101. myRangeBlinker.startBlinking();
  102. }
  103. }
  104. public void dispose() {
  105. myRangeBlinker.stopBlinking();
  106. EditorFactory editorFactory = EditorFactory.getInstance();
  107. editorFactory.releaseEditor(myEditor);
  108. }
  109. private void reinitViews() {
  110. myEditor.reinitSettings();
  111. myEditor.getMarkupModel().removeAllHighlighters();
  112. }
  113. private static Editor createEditor(String text, int column, int line, int selectedLine) {
  114. EditorFactory editorFactory = EditorFactory.getInstance();
  115. Document editorDocument = editorFactory.createDocument(text);
  116. EditorEx editor = (EditorEx)editorFactory.createViewer(editorDocument);
  117. EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
  118. editor.setColorsScheme(scheme);
  119. EditorSettings settings = editor.getSettings();
  120. settings.setWhitespacesShown(true);
  121. settings.setLineMarkerAreaShown(false);
  122. settings.setIndentGuidesShown(false);
  123. settings.setFoldingOutlineShown(false);
  124. settings.setAdditionalColumnsCount(0);
  125. settings.setAdditionalLinesCount(0);
  126. settings.setRightMarginShown(true);
  127. settings.setRightMargin(60);
  128. LogicalPosition pos = new LogicalPosition(line, column);
  129. editor.getCaretModel().moveToLogicalPosition(pos);
  130. if (selectedLine >= 0) {
  131. editor.getSelectionModel().setSelection(editorDocument.getLineStartOffset(selectedLine),
  132. editorDocument.getLineEndOffset(selectedLine));
  133. }
  134. return editor;
  135. }
  136. }