update copyright
[fedora-idea.git] / xml / dom-openapi / src / com / intellij / util / xml / model / gotosymbol / GoToSymbolProvider.java
blob96dffcc8437d04ac0902a0d58329107117941165
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 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;
37 import javax.swing.*;
38 import java.util.ArrayList;
39 import java.util.HashSet;
40 import java.util.List;
41 import java.util.Set;
43 /**
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()]);
84 @Nullable
85 protected static NavigationItem createNavigationItem(final DomElement domElement) {
86 final GenericDomValue name = domElement.getGenericInfo().getNameDomElement(domElement);
87 assert name != null;
88 final XmlElement psiElement = name.getXmlElement();
89 final String value = name.getStringValue();
90 if (psiElement == null || value == null) {
91 return null;
93 final Icon icon = ElementPresentationManager.getIcon(domElement);
94 return createNavigationItem(psiElement, value, icon);
97 @NotNull
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;
123 myText = text;
124 myIcon = icon;
127 public PsiElement getNavigationElement() {
128 return myPsiElement;
131 public Icon getIcon(boolean flags) {
132 return myIcon;
135 public ItemPresentation getPresentation() {
136 return new ItemPresentation() {
138 public String getPresentableText() {
139 return myText;
142 @Nullable
143 public String getLocationString() {
144 return '(' + myPsiElement.getContainingFile().getName() + ')';
147 @Nullable
148 public Icon getIcon(boolean open) {
149 return myIcon;
152 @Nullable
153 public TextAttributesKey getTextAttributesKey() {
154 return null;
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;
172 return true;
175 public int hashCode() {
176 int result;
177 result = (myPsiElement != null ? myPsiElement.hashCode() : 0);
178 result = 31 * result + (myText != null ? myText.hashCode() : 0);
179 return result;