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.
16 package com
.intellij
.ide
.projectView
.impl
.nodes
;
18 import com
.intellij
.ide
.projectView
.PresentationData
;
19 import com
.intellij
.ide
.projectView
.PsiClassChildrenSource
;
20 import com
.intellij
.ide
.projectView
.ViewSettings
;
21 import com
.intellij
.ide
.util
.treeView
.AbstractTreeNode
;
22 import com
.intellij
.openapi
.project
.IndexNotReadyException
;
23 import com
.intellij
.openapi
.project
.Project
;
24 import com
.intellij
.openapi
.vfs
.VirtualFile
;
25 import com
.intellij
.psi
.*;
26 import com
.intellij
.psi
.impl
.ElementPresentationUtil
;
28 import java
.util
.ArrayList
;
29 import java
.util
.Collection
;
31 public class ClassTreeNode
extends BasePsiMemberNode
<PsiClass
>{
32 public ClassTreeNode(Project project
, PsiClass value
, ViewSettings viewSettings
) {
33 super(project
, value
, viewSettings
);
36 public Collection
<AbstractTreeNode
> getChildrenImpl() {
37 PsiClass parent
= getValue();
38 final ArrayList
<AbstractTreeNode
> treeNodes
= new ArrayList
<AbstractTreeNode
>();
40 if (getSettings().isShowMembers()) {
41 ArrayList
<PsiElement
> result
= new ArrayList
<PsiElement
>();
42 PsiClassChildrenSource
.DEFAULT_CHILDREN
.addChildren(parent
, result
);
43 for (PsiElement psiElement
: result
) {
44 psiElement
.accept(new JavaElementVisitor() {
45 @Override public void visitClass(PsiClass aClass
) {
46 treeNodes
.add(new ClassTreeNode(getProject(), aClass
, getSettings()));
49 @Override public void visitMethod(PsiMethod method
) {
50 treeNodes
.add(new PsiMethodNode(getProject(), method
, getSettings()));
53 @Override public void visitField(PsiField field
) {
54 treeNodes
.add(new PsiFieldNode(getProject(), field
, getSettings()));
57 @Override public void visitReferenceExpression(PsiReferenceExpression expression
) {
58 visitExpression(expression
);
66 public void updateImpl(PresentationData data
) {
67 final PsiClass aClass
= getValue();
69 data
.setPresentableText(aClass
.getName());
73 public boolean isTopLevel() {
74 return getValue() != null && getValue().getParent()instanceof PsiFile
;
78 public boolean expandOnDoubleClick() {
82 public PsiClass
getPsiClass() {
86 public boolean isAlwaysExpand() {
87 return getParentValue() instanceof PsiFile
;
90 public int getWeight() {
95 public String
getTitle() {
96 final PsiClass psiClass
= getValue();
97 if (psiClass
!= null) {
98 return psiClass
.getQualifiedName();
100 return super.getTitle();
104 protected boolean isMarkReadOnly() {
108 public int getTypeSortWeight(final boolean sortByType
) {
109 return sortByType ?
5 : 0;
112 public Comparable
getTypeSortKey() {
113 return new ClassNameSortKey();
116 public static int getClassPosition(final PsiClass aClass
) {
117 if (aClass
== null || !aClass
.isValid()) {
121 int pos
= ElementPresentationUtil
.getClassKind(aClass
);
122 //abstract class before concrete
123 if (pos
== ElementPresentationUtil
.CLASS_KIND_CLASS
|| pos
== ElementPresentationUtil
.CLASS_KIND_EXCEPTION
) {
124 boolean isAbstract
= aClass
.hasModifierProperty(PsiModifier
.ABSTRACT
) && !aClass
.isInterface();
131 catch (IndexNotReadyException e
) {
136 private class ClassNameSortKey
implements Comparable
{
137 public int compareTo(final Object o
) {
138 if (!(o
instanceof ClassNameSortKey
)) return 0;
139 ClassNameSortKey rhs
= (ClassNameSortKey
) o
;
140 return getPosition() - rhs
.getPosition();
144 return getClassPosition(getValue());
149 public boolean shouldDrillDownOnEmptyElement() {
153 public boolean canRepresent(final Object element
) {
154 if (super.canRepresent(element
)) return true;
155 if (getValue().getParent() instanceof PsiFile
) {
156 if (element
instanceof VirtualFile
) {
157 PsiFile parentFile
= (PsiFile
) getValue().getParent();
158 if (parentFile
.getVirtualFile() == element
) return true;
160 return getValue().getParent() == element
;