update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / ide / favoritesTreeView / PsiMethodFavoriteNodeProvider.java
blob2527dc861d5888352fda0c3dc223d07f89fbc75b
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.
18 * User: anna
19 * Date: 21-Jan-2008
21 package com.intellij.ide.favoritesTreeView;
23 import com.intellij.codeInspection.reference.RefMethodImpl;
24 import com.intellij.ide.favoritesTreeView.smartPointerPsiNodes.MethodSmartPointerNode;
25 import com.intellij.ide.projectView.ViewSettings;
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.ModuleUtil;
32 import com.intellij.openapi.project.Project;
33 import com.intellij.openapi.vfs.VirtualFile;
34 import com.intellij.psi.PsiClass;
35 import com.intellij.psi.PsiElement;
36 import com.intellij.psi.PsiManager;
37 import com.intellij.psi.PsiMethod;
38 import com.intellij.psi.presentation.java.ClassPresentationUtil;
39 import com.intellij.psi.util.PsiFormatUtil;
40 import org.jetbrains.annotations.NotNull;
42 import java.util.ArrayList;
43 import java.util.Collection;
45 public class PsiMethodFavoriteNodeProvider extends FavoriteNodeProvider {
46 public Collection<AbstractTreeNode> getFavoriteNodes(final DataContext context, final ViewSettings viewSettings) {
47 final Project project = PlatformDataKeys.PROJECT.getData(context);
48 if (project == null) return null;
49 PsiElement[] elements = LangDataKeys.PSI_ELEMENT_ARRAY.getData(context);
50 if (elements == null) {
51 final PsiElement element = LangDataKeys.PSI_ELEMENT.getData(context);
52 if (element != null) {
53 elements = new PsiElement[]{element};
56 if (elements != null) {
57 final Collection<AbstractTreeNode> result = new ArrayList<AbstractTreeNode>();
58 for (PsiElement element : elements) {
59 if (element instanceof PsiMethod) {
60 result.add(new MethodSmartPointerNode(project, element, viewSettings));
63 return result.isEmpty() ? null : result;
65 return null;
68 @Override
69 public AbstractTreeNode createNode(final Project project, final Object element, final ViewSettings viewSettings) {
70 if (element instanceof PsiMethod) {
71 return new MethodSmartPointerNode(project, element, viewSettings);
73 return super.createNode(project, element, viewSettings);
76 public boolean elementContainsFile(final Object element, final VirtualFile vFile) {
77 return false;
80 public int getElementWeight(final Object value, final boolean isSortByType) {
81 if (value instanceof PsiMethod){
82 return 5;
84 return -1;
87 public String getElementLocation(final Object element) {
88 if (element instanceof PsiMethod) {
89 final PsiClass parent = ((PsiMethod)element).getContainingClass();
90 if (parent != null) {
91 return ClassPresentationUtil.getNameForClass(parent, true);
94 return null;
97 public boolean isInvalidElement(final Object element) {
98 return element instanceof PsiMethod && !((PsiMethod)element).isValid();
101 @NotNull
102 public String getFavoriteTypeId() {
103 return "method";
106 public String getElementUrl(final Object element) {
107 if (element instanceof PsiMethod) {
108 PsiMethod aMethod = (PsiMethod)element;
109 return PsiFormatUtil.getExternalName(aMethod);
111 return null;
114 public String getElementModuleName(final Object element) {
115 if (element instanceof PsiMethod) {
116 PsiMethod aMethod = (PsiMethod)element;
117 Module module = ModuleUtil.findModuleForPsiElement(aMethod);
118 return module != null ? module.getName() : null;
120 return null;
123 public Object[] createPathFromUrl(final Project project, final String url, final String moduleName) {
124 final PsiMethod method = RefMethodImpl.findPsiMethod(PsiManager.getInstance(project), url);
125 if (method == null) return null;
126 return new Object[]{method};