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
.structureView
.impl
;
18 import com
.intellij
.ide
.util
.treeView
.NodeDescriptor
;
19 import com
.intellij
.openapi
.roots
.ui
.util
.CellAppearance
;
20 import com
.intellij
.openapi
.roots
.ui
.util
.CellAppearanceUtils
;
21 import com
.intellij
.openapi
.roots
.ui
.util
.CompositeAppearance
;
22 import com
.intellij
.openapi
.roots
.ui
.util
.ModifiableCellAppearance
;
23 import com
.intellij
.openapi
.util
.Iconable
;
24 import com
.intellij
.psi
.*;
25 import com
.intellij
.psi
.util
.PsiFormatUtil
;
26 import com
.intellij
.ui
.ColoredTreeCellRenderer
;
27 import com
.intellij
.ui
.SimpleTextAttributes
;
30 import javax
.swing
.tree
.DefaultMutableTreeNode
;
31 import javax
.swing
.tree
.TreeNode
;
33 public class StructureNodeRenderer
extends ColoredTreeCellRenderer
{
34 public void customizeCellRenderer(
43 forNodeDescriptorInTree(value
, expanded
).customize(this);
46 public static CellAppearance
forNodeDescriptorInTree(Object node
, boolean expanded
) {
47 NodeDescriptor descriptor
= getNodeDescriptor(node
);
48 if (descriptor
== null) return CellAppearanceUtils
.EMPTY
;
49 String name
= descriptor
.toString();
50 Object psiElement
= descriptor
.getElement();
51 ModifiableCellAppearance result
;
52 if (psiElement
instanceof PsiElement
&& !((PsiElement
)psiElement
).isValid()) {
53 result
= CompositeAppearance
.single(name
);
55 PsiClass psiClass
= getContainingClass(psiElement
);
56 if (isInheritedMember(node
, psiClass
) && psiClass
!= null) {
57 CompositeAppearance
.DequeEnd ending
= new CompositeAppearance().getEnding();
58 ending
.addText(name
, applyDeprecation(psiElement
, SimpleTextAttributes
.DARK_TEXT
));
59 ending
.addComment(psiClass
.getName(), applyDeprecation(psiClass
, SimpleTextAttributes
.GRAY_ATTRIBUTES
));
60 result
= ending
.getAppearance();
63 SimpleTextAttributes textAttributes
= applyDeprecation(psiElement
, SimpleTextAttributes
.REGULAR_ATTRIBUTES
);
64 result
= CompositeAppearance
.single(name
, textAttributes
);
68 Icon icon
= expanded ? descriptor
.getOpenIcon() : descriptor
.getClosedIcon();
73 public static CellAppearance
forElementInClass(PsiMember psiMember
, PsiClass psiClass
) {
74 boolean isOwnMethod
= psiMember
.getContainingClass().getQualifiedName().equals(psiClass
.getQualifiedName());
75 String name
= getNameOf(psiMember
);
76 psiMember
.getIcon(Iconable
.ICON_FLAG_VISIBILITY
);
78 return CompositeAppearance
.single(name
, applyDeprecation(psiMember
, SimpleTextAttributes
.REGULAR_ATTRIBUTES
));
80 CompositeAppearance
.DequeEnd ending
= new CompositeAppearance().getEnding();
81 ending
.addText(name
, applyDeprecation(psiMember
, SimpleTextAttributes
.DARK_TEXT
));
82 ending
.addComment(psiClass
.getName(), applyDeprecation(psiClass
, SimpleTextAttributes
.GRAY_ATTRIBUTES
));
83 return ending
.getAppearance();
87 public static String
getNameOf(PsiElement psiElement
) {
88 if (psiElement
instanceof PsiMethod
)
89 return PsiFormatUtil
.formatMethod((PsiMethod
)psiElement
,
91 PsiFormatUtil
.SHOW_NAME
| PsiFormatUtil
.SHOW_TYPE
| PsiFormatUtil
.TYPE_AFTER
|
92 PsiFormatUtil
.SHOW_PARAMETERS
,
93 PsiFormatUtil
.SHOW_TYPE
95 return psiElement
.toString();
99 private static boolean isInheritedMember(Object node
, PsiClass psiClass
) {
100 PsiClass treeParentClass
= getTreeParentClass(node
);
101 return treeParentClass
!= psiClass
;
104 public static SimpleTextAttributes
applyDeprecation(Object value
, SimpleTextAttributes nameAttributes
) {
105 return isDeprecated(value
) ?
makeStrikeout(nameAttributes
) : nameAttributes
;
108 private static SimpleTextAttributes
makeStrikeout(SimpleTextAttributes nameAttributes
) {
109 return new SimpleTextAttributes(nameAttributes
.getStyle() | SimpleTextAttributes
.STYLE_STRIKEOUT
, nameAttributes
.getFgColor());
112 public static boolean isDeprecated(Object psiElement
) {
113 if (psiElement
instanceof PsiDocCommentOwner
)
114 return ((PsiDocCommentOwner
) psiElement
).isDeprecated();
118 public static PsiClass
getContainingClass(Object element
) {
119 if (element
instanceof PsiMember
)
120 return ((PsiMember
) element
).getContainingClass();
121 if (element
instanceof PsiClass
) {
122 PsiElement parent
= ((PsiClass
) element
).getParent();
123 return (PsiClass
) (parent
instanceof PsiClass ? parent
: null);
128 public static PsiClass
getTreeParentClass(Object value
) {
129 if (!(value
instanceof TreeNode
))
131 for (TreeNode treeNode
= ((TreeNode
) value
).getParent(); treeNode
!= null; treeNode
= treeNode
.getParent()) {
132 Object element
= getElement(treeNode
);
133 if (element
instanceof PsiClass
)
134 return (PsiClass
) element
;
139 private static NodeDescriptor
getNodeDescriptor(Object value
) {
140 if (value
instanceof DefaultMutableTreeNode
) {
141 DefaultMutableTreeNode node
= (DefaultMutableTreeNode
) value
;
142 Object userObject
= node
.getUserObject();
143 if (userObject
instanceof NodeDescriptor
) {
144 return (NodeDescriptor
)userObject
;
150 private static Object
getElement(Object node
) {
151 NodeDescriptor descriptor
= getNodeDescriptor(node
);
152 return descriptor
== null ?
null : descriptor
.getElement();