1 package com
.intellij
.ide
.projectView
.impl
.nodes
;
3 import com
.intellij
.ide
.projectView
.ProjectViewNode
;
4 import com
.intellij
.ide
.projectView
.ViewSettings
;
5 import com
.intellij
.ide
.projectView
.PresentationData
;
6 import com
.intellij
.ide
.util
.treeView
.AbstractTreeNode
;
7 import com
.intellij
.openapi
.project
.Project
;
8 import com
.intellij
.openapi
.module
.Module
;
9 import com
.intellij
.openapi
.vcs
.FileStatus
;
10 import com
.intellij
.openapi
.vcs
.FileStatusManager
;
11 import com
.intellij
.openapi
.vfs
.VirtualFile
;
12 import com
.intellij
.openapi
.util
.Iconable
;
13 import com
.intellij
.openapi
.editor
.colors
.CodeInsightColors
;
14 import com
.intellij
.openapi
.diagnostic
.Logger
;
15 import com
.intellij
.psi
.PsiElement
;
16 import com
.intellij
.psi
.PsiDirectory
;
17 import com
.intellij
.psi
.PsiFile
;
18 import com
.intellij
.psi
.util
.PsiUtilBase
;
19 import com
.intellij
.navigation
.NavigationItem
;
20 import org
.jetbrains
.annotations
.Nullable
;
21 import org
.jetbrains
.annotations
.NotNull
;
24 import java
.util
.Collection
;
25 import java
.util
.ArrayList
;
26 import java
.util
.Collections
;
29 * Class for node descriptors based on PsiElements. Subclasses should define
30 * method that extract PsiElement from Value.
31 * @param <Value> Value of node descriptor
33 public abstract class AbstractPsiBasedNode
<Value
> extends ProjectViewNode
<Value
> {
34 private static final Logger LOG
= Logger
.getInstance(AbstractPsiBasedNode
.class.getName());
36 protected AbstractPsiBasedNode(final Project project
,
38 final ViewSettings viewSettings
) {
39 super(project
, value
, viewSettings
);
43 protected abstract PsiElement
extractPsiFromValue();
45 protected abstract Collection
<AbstractTreeNode
> getChildrenImpl();
46 protected abstract void updateImpl(final PresentationData data
);
49 public final Collection
<AbstractTreeNode
> getChildren() {
50 final PsiElement psiElement
= extractPsiFromValue();
51 if (psiElement
== null) {
52 return new ArrayList
<AbstractTreeNode
>();
54 final boolean valid
= psiElement
.isValid();
55 if (!LOG
.assertTrue(valid
)) {
56 return Collections
.emptyList();
59 final Collection
<AbstractTreeNode
> children
= getChildrenImpl();
60 return children
!= null ? children
: Collections
.<AbstractTreeNode
>emptyList();
63 protected boolean isMarkReadOnly() {
64 final AbstractTreeNode parent
= getParent();
68 if (parent
instanceof AbstractPsiBasedNode
) {
69 final PsiElement psiElement
= ((AbstractPsiBasedNode
)parent
).extractPsiFromValue();
70 return psiElement
instanceof PsiDirectory
;
73 final Object parentValue
= parent
.getValue();
74 return parentValue
instanceof PsiDirectory
|| parentValue
instanceof Module
;
78 public FileStatus
getFileStatus() {
79 VirtualFile file
= getVirtualFileForValue();
81 return FileStatus
.NOT_CHANGED
;
84 return FileStatusManager
.getInstance(getProject()).getStatus(file
);
89 private VirtualFile
getVirtualFileForValue() {
90 PsiElement psiElement
= extractPsiFromValue();
91 if (psiElement
== null) {
94 return PsiUtilBase
.getVirtualFile(psiElement
);
97 // Should be called in atomic action
99 public void update(final PresentationData data
) {
104 final PsiElement value
= extractPsiFromValue();
105 LOG
.assertTrue(value
.isValid());
107 int flags
= Iconable
.ICON_FLAG_VISIBILITY
;
108 if (isMarkReadOnly()) {
109 flags
|= Iconable
.ICON_FLAG_READ_STATUS
;
112 Icon icon
= value
.getIcon(flags
);
113 data
.setClosedIcon(icon
);
114 data
.setOpenIcon(icon
);
115 data
.setLocationString(myLocationString
);
116 data
.setPresentableText(myName
);
117 data
.setTooltip(calcTooltip());
118 if (isDeprecated()) {
119 data
.setAttributesKey(CodeInsightColors
.DEPRECATED_ATTRIBUTES
);
124 protected boolean isDeprecated() {
128 public boolean contains(@NotNull final VirtualFile file
) {
129 final PsiElement psiElement
= extractPsiFromValue();
130 if (psiElement
== null || !psiElement
.isValid()) {
134 final PsiFile containingFile
= psiElement
.getContainingFile();
135 if (containingFile
== null) {
138 final VirtualFile valueFile
= containingFile
.getVirtualFile();
139 return valueFile
!= null && file
.equals(valueFile
);
143 public NavigationItem
getNavigationItem() {
144 final PsiElement psiElement
= extractPsiFromValue();
145 return (psiElement
instanceof NavigationItem
) ?
(NavigationItem
) psiElement
: null;
148 public void navigate(boolean requestFocus
) {
150 getNavigationItem().navigate(requestFocus
);
154 public boolean canNavigate() {
155 final NavigationItem item
= getNavigationItem();
156 return item
!= null && item
.canNavigate();
159 public boolean canNavigateToSource() {
160 final NavigationItem item
= getNavigationItem();
161 return item
!= null && item
.canNavigateToSource();
165 protected String
calcTooltip() {
170 public boolean validate() {
171 final PsiElement psiElement
= extractPsiFromValue();
172 if (psiElement
== null || !psiElement
.isValid()) {
176 return getValue() != null;