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 package com
.intellij
.util
.xml
.model
.gotosymbol
;
19 import com
.intellij
.navigation
.ChooseByNameContributor
;
20 import com
.intellij
.navigation
.ItemPresentation
;
21 import com
.intellij
.navigation
.NavigationItem
;
22 import com
.intellij
.openapi
.editor
.colors
.TextAttributesKey
;
23 import com
.intellij
.openapi
.module
.Module
;
24 import com
.intellij
.openapi
.module
.ModuleManager
;
25 import com
.intellij
.openapi
.project
.Project
;
26 import com
.intellij
.psi
.PsiElement
;
27 import com
.intellij
.psi
.impl
.FakePsiElement
;
28 import com
.intellij
.psi
.xml
.XmlElement
;
29 import com
.intellij
.util
.ArrayUtil
;
30 import com
.intellij
.util
.xml
.DomElement
;
31 import com
.intellij
.util
.xml
.ElementPresentationManager
;
32 import com
.intellij
.util
.xml
.GenericDomValue
;
33 import org
.jetbrains
.annotations
.NonNls
;
34 import org
.jetbrains
.annotations
.NotNull
;
35 import org
.jetbrains
.annotations
.Nullable
;
38 import java
.util
.ArrayList
;
39 import java
.util
.HashSet
;
40 import java
.util
.List
;
44 * Base class for "Go To Symbol" contributors.
46 public abstract class GoToSymbolProvider
implements ChooseByNameContributor
{
48 protected abstract void addNames(@NotNull Module module
, Set
<String
> result
);
50 protected abstract void addItems(@NotNull Module module
, String name
, List
<NavigationItem
> result
);
52 protected abstract boolean acceptModule(final Module module
);
54 protected static void addNewNames(@NotNull final List
<?
extends DomElement
> elements
, final Set
<String
> existingNames
) {
55 for (DomElement name
: elements
) {
56 existingNames
.add(name
.getGenericInfo().getElementName(name
));
60 public String
[] getNames(final Project project
, boolean includeNonProjectItems
) {
61 Set
<String
> result
= new HashSet
<String
>();
62 Module
[] modules
= ModuleManager
.getInstance(project
).getModules();
63 for (Module module
: modules
) {
64 if (acceptModule(module
)) {
65 addNames(module
, result
);
69 return ArrayUtil
.toStringArray(result
);
72 public NavigationItem
[] getItemsByName(final String name
, final String pattern
, final Project project
, boolean includeNonProjectItems
) {
73 List
<NavigationItem
> result
= new ArrayList
<NavigationItem
>();
74 Module
[] modules
= ModuleManager
.getInstance(project
).getModules();
75 for (Module module
: modules
) {
76 if (acceptModule(module
)) {
77 addItems(module
, name
, result
);
81 return result
.toArray(new NavigationItem
[result
.size()]);
85 protected static NavigationItem
createNavigationItem(final DomElement domElement
) {
86 final GenericDomValue name
= domElement
.getGenericInfo().getNameDomElement(domElement
);
88 final XmlElement psiElement
= name
.getXmlElement();
89 final String value
= name
.getStringValue();
90 if (psiElement
== null || value
== null) {
93 final Icon icon
= ElementPresentationManager
.getIcon(domElement
);
94 return createNavigationItem(psiElement
, value
, icon
);
98 protected static NavigationItem
createNavigationItem(@NotNull final PsiElement element
,
99 @NotNull @NonNls final String text
,
100 @Nullable final Icon icon
) {
101 return new BaseNavigationItem(element
, text
, icon
);
106 * Wraps one entry to display in "Go To Symbol" dialog.
108 protected static class BaseNavigationItem
extends FakePsiElement
{
110 private final PsiElement myPsiElement
;
111 private final String myText
;
112 private final Icon myIcon
;
115 * Creates a new display item.
117 * @param psiElement The PsiElement to navigate to.
118 * @param text Text to show for this element.
119 * @param icon Icon to show for this element.
121 protected BaseNavigationItem(@NotNull PsiElement psiElement
, @NotNull @NonNls String text
, @Nullable Icon icon
) {
122 myPsiElement
= psiElement
;
127 public PsiElement
getNavigationElement() {
131 public Icon
getIcon(boolean flags
) {
135 public ItemPresentation
getPresentation() {
136 return new ItemPresentation() {
138 public String
getPresentableText() {
143 public String
getLocationString() {
144 return '(' + myPsiElement
.getContainingFile().getName() + ')';
148 public Icon
getIcon(boolean open
) {
153 public TextAttributesKey
getTextAttributesKey() {
159 public PsiElement
getParent() {
160 return myPsiElement
.getParent();
163 public boolean equals(final Object o
) {
164 if (this == o
) return true;
165 if (o
== null || getClass() != o
.getClass()) return false;
167 final BaseNavigationItem that
= (BaseNavigationItem
)o
;
169 if (myPsiElement
!= null ?
!myPsiElement
.equals(that
.myPsiElement
) : that
.myPsiElement
!= null) return false;
170 if (myText
!= null ?
!myText
.equals(that
.myText
) : that
.myText
!= null) return false;
175 public int hashCode() {
177 result
= (myPsiElement
!= null ? myPsiElement
.hashCode() : 0);
178 result
= 31 * result
+ (myText
!= null ? myText
.hashCode() : 0);