2 * Copyright 2007 Bas Leijdekkers
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.
16 package com
.siyeh
.ig
.psiutils
;
18 import com
.intellij
.codeInsight
.highlighting
.HighlightManager
;
19 import com
.intellij
.find
.FindManager
;
20 import com
.intellij
.find
.FindModel
;
21 import com
.intellij
.openapi
.application
.Application
;
22 import com
.intellij
.openapi
.application
.ApplicationManager
;
23 import com
.intellij
.openapi
.editor
.Editor
;
24 import com
.intellij
.openapi
.editor
.colors
.EditorColors
;
25 import com
.intellij
.openapi
.editor
.colors
.EditorColorsManager
;
26 import com
.intellij
.openapi
.editor
.colors
.EditorColorsScheme
;
27 import com
.intellij
.openapi
.editor
.markup
.TextAttributes
;
28 import com
.intellij
.openapi
.fileEditor
.FileEditorManager
;
29 import com
.intellij
.openapi
.project
.Project
;
30 import com
.intellij
.openapi
.wm
.StatusBar
;
31 import com
.intellij
.openapi
.wm
.WindowManager
;
32 import com
.intellij
.psi
.PsiElement
;
33 import com
.siyeh
.InspectionGadgetsBundle
;
34 import org
.jetbrains
.annotations
.NotNull
;
36 import java
.util
.Collection
;
37 import java
.util
.Collections
;
39 public class HighlightUtil
{
41 private HighlightUtil() {
44 public static void highlightElement(PsiElement element
) {
45 highlightElements(Collections
.singleton(element
));
48 public static void highlightElements(
49 @NotNull final Collection
<?
extends PsiElement
> elementCollection
) {
50 if (elementCollection
.isEmpty()) {
53 final Application application
= ApplicationManager
.getApplication();
54 application
.invokeLater(new Runnable() {
56 final PsiElement
[] elements
=
57 elementCollection
.toArray(
58 new PsiElement
[elementCollection
.size()]);
59 final Project project
= elements
[0].getProject();
60 final FileEditorManager editorManager
=
61 FileEditorManager
.getInstance(project
);
62 final EditorColorsManager editorColorsManager
=
63 EditorColorsManager
.getInstance();
64 final Editor editor
= editorManager
.getSelectedTextEditor();
68 final EditorColorsScheme globalScheme
=
69 editorColorsManager
.getGlobalScheme();
70 final TextAttributes textattributes
=
71 globalScheme
.getAttributes(
72 EditorColors
.SEARCH_RESULT_ATTRIBUTES
);
73 final HighlightManager highlightManager
=
74 HighlightManager
.getInstance(project
);
75 highlightManager
.addOccurrenceHighlights(
76 editor
, elements
, textattributes
, true, null);
77 final WindowManager windowManager
=
78 WindowManager
.getInstance();
79 final StatusBar statusBar
=
80 windowManager
.getStatusBar(project
);
81 statusBar
.setInfo(InspectionGadgetsBundle
.message(
82 "press.escape.to.remove.highlighting.message"));
83 final FindManager findmanager
=
84 FindManager
.getInstance(project
);
85 FindModel findmodel
= findmanager
.getFindNextModel();
86 if(findmodel
== null) {
87 findmodel
= findmanager
.getFindInFileModel();
89 findmodel
.setSearchHighlighters(true);
90 findmanager
.setFindWasPerformed();
91 findmanager
.setFindNextModel(findmodel
);