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.
21 package com
.intellij
.ide
.favoritesTreeView
;
23 import com
.intellij
.ide
.favoritesTreeView
.smartPointerPsiNodes
.ClassSmartPointerNode
;
24 import com
.intellij
.ide
.projectView
.ViewSettings
;
25 import com
.intellij
.ide
.projectView
.impl
.nodes
.ClassTreeNode
;
26 import com
.intellij
.ide
.util
.treeView
.AbstractTreeNode
;
27 import com
.intellij
.openapi
.actionSystem
.DataContext
;
28 import com
.intellij
.openapi
.actionSystem
.LangDataKeys
;
29 import com
.intellij
.openapi
.actionSystem
.PlatformDataKeys
;
30 import com
.intellij
.openapi
.module
.Module
;
31 import com
.intellij
.openapi
.module
.ModuleManager
;
32 import com
.intellij
.openapi
.module
.ModuleUtil
;
33 import com
.intellij
.openapi
.project
.Project
;
34 import com
.intellij
.openapi
.vfs
.VirtualFile
;
35 import com
.intellij
.psi
.JavaPsiFacade
;
36 import com
.intellij
.psi
.PsiClass
;
37 import com
.intellij
.psi
.PsiElement
;
38 import com
.intellij
.psi
.PsiFile
;
39 import com
.intellij
.psi
.presentation
.java
.ClassPresentationUtil
;
40 import com
.intellij
.psi
.search
.GlobalSearchScope
;
41 import org
.jetbrains
.annotations
.NotNull
;
43 import java
.util
.ArrayList
;
44 import java
.util
.Collection
;
46 public class PsiClassFavoriteNodeProvider
extends FavoriteNodeProvider
{
47 public Collection
<AbstractTreeNode
> getFavoriteNodes(final DataContext context
, final ViewSettings viewSettings
) {
48 final Project project
= PlatformDataKeys
.PROJECT
.getData(context
);
49 if (project
== null) return null;
50 PsiElement
[] elements
= LangDataKeys
.PSI_ELEMENT_ARRAY
.getData(context
);
51 if (elements
== null) {
52 final PsiElement element
= LangDataKeys
.PSI_ELEMENT
.getData(context
);
53 if (element
!= null) {
54 elements
= new PsiElement
[]{element
};
57 if (elements
!= null) {
58 final Collection
<AbstractTreeNode
> result
= new ArrayList
<AbstractTreeNode
>();
59 for (PsiElement element
: elements
) {
60 if (element
instanceof PsiClass
) {
61 result
.add(new ClassSmartPointerNode(project
, element
, viewSettings
));
64 return result
.isEmpty() ?
null : result
;
70 public AbstractTreeNode
createNode(final Project project
, final Object element
, final ViewSettings viewSettings
) {
71 if (element
instanceof PsiClass
) {
72 return new ClassSmartPointerNode(project
, element
, viewSettings
);
74 return super.createNode(project
, element
, viewSettings
);
77 public boolean elementContainsFile(final Object element
, final VirtualFile vFile
) {
78 if (element
instanceof PsiClass
) {
79 final PsiFile file
= ((PsiClass
)element
).getContainingFile();
80 if (file
!= null && file
.getVirtualFile() == vFile
) return true;
85 public int getElementWeight(final Object value
, final boolean isSortByType
) {
86 if (value
instanceof PsiClass
){
87 return isSortByType ? ClassTreeNode
.getClassPosition((PsiClass
)value
) : 3;
93 public String
getElementLocation(final Object element
) {
94 if (element
instanceof PsiClass
) {
95 return ClassPresentationUtil
.getNameForClass((PsiClass
)element
, true);
100 public boolean isInvalidElement(final Object element
) {
101 return element
instanceof PsiClass
&& !((PsiClass
)element
).isValid();
105 public String
getFavoriteTypeId() {
109 public String
getElementUrl(final Object element
) {
110 if (element
instanceof PsiClass
) {
111 PsiClass aClass
= (PsiClass
)element
;
112 return aClass
.getQualifiedName();
117 public String
getElementModuleName(final Object element
) {
118 if (element
instanceof PsiClass
) {
119 PsiClass aClass
= (PsiClass
)element
;
120 Module module
= ModuleUtil
.findModuleForPsiElement(aClass
);
121 return module
!= null ? module
.getName() : null;
126 public Object
[] createPathFromUrl(final Project project
, final String url
, final String moduleName
) {
127 GlobalSearchScope scope
= null;
128 if (moduleName
!= null) {
129 final Module module
= ModuleManager
.getInstance(project
).findModuleByName(moduleName
);
130 if (module
!= null) {
131 scope
= GlobalSearchScope
.moduleScope(module
);
135 scope
= GlobalSearchScope
.allScope(project
);
137 final PsiClass aClass
= JavaPsiFacade
.getInstance(project
).findClass(url
, scope
);
138 if (aClass
== null) return null;
139 return new Object
[]{aClass
};