/platform/platform-impl/src/com/intellij/ide/impl/SelectInEditorManagerImpl.java

https://bitbucket.org/nbargnesi/idea · Java · 158 lines · 123 code · 16 blank · 19 comment · 20 complexity · cc2c4bc7a9b190c1138c537c8e0f7672 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. package com.intellij.ide.impl;
  17. import com.intellij.ide.SelectInEditorManager;
  18. import com.intellij.injected.editor.DocumentWindow;
  19. import com.intellij.injected.editor.VirtualFileWindow;
  20. import com.intellij.openapi.Disposable;
  21. import com.intellij.openapi.editor.Editor;
  22. import com.intellij.openapi.editor.colors.EditorColors;
  23. import com.intellij.openapi.editor.colors.EditorColorsManager;
  24. import com.intellij.openapi.editor.event.CaretEvent;
  25. import com.intellij.openapi.editor.event.CaretListener;
  26. import com.intellij.openapi.editor.ex.DocumentEx;
  27. import com.intellij.openapi.editor.markup.HighlighterLayer;
  28. import com.intellij.openapi.editor.markup.HighlighterTargetArea;
  29. import com.intellij.openapi.editor.markup.RangeHighlighter;
  30. import com.intellij.openapi.editor.markup.TextAttributes;
  31. import com.intellij.openapi.fileEditor.FileEditorManager;
  32. import com.intellij.openapi.fileEditor.OpenFileDescriptor;
  33. import com.intellij.openapi.project.Project;
  34. import com.intellij.openapi.util.ProperTextRange;
  35. import com.intellij.openapi.util.TextRange;
  36. import com.intellij.openapi.vfs.VirtualFile;
  37. import org.jetbrains.annotations.NotNull;
  38. import org.jetbrains.annotations.Nullable;
  39. import javax.swing.*;
  40. import java.awt.event.FocusEvent;
  41. import java.awt.event.FocusListener;
  42. /**
  43. * @author MYakovlev
  44. * Date: Jul 1, 2002
  45. */
  46. public class SelectInEditorManagerImpl extends SelectInEditorManager implements Disposable, FocusListener, CaretListener{
  47. private final Project myProject;
  48. private RangeHighlighter mySegmentHighlighter;
  49. private Editor myEditor;
  50. public SelectInEditorManagerImpl(Project project){
  51. myProject = project;
  52. }
  53. public void dispose() {
  54. releaseAll();
  55. }
  56. public void selectInEditor(VirtualFile file, final int startOffset, final int endOffset, final boolean toSelectLine, final boolean toUseNormalSelection){
  57. releaseAll();
  58. final TextRange textRange;
  59. if (file instanceof VirtualFileWindow) {
  60. DocumentWindow documentWindow = ((VirtualFileWindow)file).getDocumentWindow();
  61. textRange = documentWindow.injectedToHost(new TextRange(startOffset, endOffset));
  62. file = ((VirtualFileWindow)file).getDelegate();
  63. }
  64. else {
  65. textRange = new ProperTextRange(startOffset, endOffset);
  66. }
  67. openEditor(file, endOffset);
  68. final Editor editor = openEditor(file, textRange.getStartOffset());
  69. SwingUtilities.invokeLater(new Runnable(){ // later to let focus listener chance to handle events
  70. public void run() {
  71. if (editor != null && !editor.isDisposed()) {
  72. doSelect(toUseNormalSelection, editor, toSelectLine, textRange);
  73. }
  74. }
  75. });
  76. }
  77. private void doSelect(final boolean toUseNormalSelection, @NotNull final Editor editor,
  78. final boolean toSelectLine,
  79. final TextRange textRange) {
  80. int startOffset = textRange.getStartOffset();
  81. int endOffset = textRange.getEndOffset();
  82. if (toUseNormalSelection) {
  83. DocumentEx doc = (DocumentEx) editor.getDocument();
  84. if (toSelectLine){
  85. int lineNumber = doc.getLineNumber(startOffset);
  86. if (lineNumber >= 0 && lineNumber < doc.getLineCount()) {
  87. editor.getSelectionModel().setSelection(doc.getLineStartOffset(lineNumber), doc.getLineEndOffset(lineNumber) + doc.getLineSeparatorLength(lineNumber));
  88. }
  89. }
  90. else {
  91. editor.getSelectionModel().setSelection(startOffset, endOffset);
  92. }
  93. return;
  94. }
  95. TextAttributes selectionAttributes = EditorColorsManager.getInstance().getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
  96. releaseAll();
  97. if (toSelectLine){
  98. DocumentEx doc = (DocumentEx) editor.getDocument();
  99. int lineNumber = doc.getLineNumber(startOffset);
  100. if (lineNumber >= 0 && lineNumber < doc.getLineCount()){
  101. mySegmentHighlighter = editor.getMarkupModel().addRangeHighlighter(doc.getLineStartOffset(lineNumber),
  102. doc.getLineEndOffset(lineNumber) + doc.getLineSeparatorLength(lineNumber),
  103. HighlighterLayer.LAST + 1,
  104. selectionAttributes, HighlighterTargetArea.EXACT_RANGE);
  105. }
  106. }
  107. else{
  108. mySegmentHighlighter = editor.getMarkupModel().addRangeHighlighter(startOffset,
  109. endOffset,
  110. HighlighterLayer.LAST + 1,
  111. selectionAttributes, HighlighterTargetArea.EXACT_RANGE);
  112. }
  113. myEditor = editor;
  114. myEditor.getContentComponent().addFocusListener(this);
  115. myEditor.getCaretModel().addCaretListener(this);
  116. }
  117. public void focusGained(FocusEvent e) {
  118. releaseAll();
  119. }
  120. public void focusLost(FocusEvent e) {
  121. }
  122. public void caretPositionChanged(CaretEvent e) {
  123. releaseAll();
  124. }
  125. private void releaseAll() {
  126. if (mySegmentHighlighter != null && myEditor != null){
  127. mySegmentHighlighter.dispose();
  128. myEditor.getContentComponent().removeFocusListener(this);
  129. myEditor.getCaretModel().removeCaretListener(this);
  130. mySegmentHighlighter = null;
  131. myEditor = null;
  132. }
  133. }
  134. @Nullable
  135. private Editor openEditor(VirtualFile file, int textOffset){
  136. if (file == null || !file.isValid()){
  137. return null;
  138. }
  139. OpenFileDescriptor descriptor = new OpenFileDescriptor(myProject, file, textOffset);
  140. return FileEditorManager.getInstance(myProject).openTextEditor(descriptor, false);
  141. }
  142. }