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
.java
;
18 import com
.intellij
.ide
.util
.treeView
.WeighedItem
;
19 import com
.intellij
.ide
.util
.treeView
.smartTree
.Group
;
20 import com
.intellij
.ide
.util
.treeView
.smartTree
.TreeElement
;
21 import com
.intellij
.navigation
.ItemPresentation
;
22 import com
.intellij
.openapi
.application
.Application
;
23 import com
.intellij
.openapi
.application
.ApplicationManager
;
24 import com
.intellij
.openapi
.editor
.colors
.CodeInsightColors
;
25 import com
.intellij
.openapi
.editor
.colors
.TextAttributesKey
;
26 import com
.intellij
.openapi
.project
.Project
;
27 import com
.intellij
.openapi
.util
.IconLoader
;
28 import com
.intellij
.psi
.*;
29 import com
.intellij
.psi
.util
.PropertyUtil
;
30 import com
.intellij
.psi
.util
.PsiUtil
;
31 import org
.jetbrains
.annotations
.NonNls
;
32 import org
.jetbrains
.annotations
.NotNull
;
35 import java
.util
.ArrayList
;
36 import java
.util
.Collection
;
38 public class PropertyGroup
implements Group
, ItemPresentation
, AccessLevelProvider
, WeighedItem
{
39 private final String myPropertyName
;
40 private final PsiType myPropertyType
;
42 private SmartPsiElementPointer myFieldPointer
;
43 private SmartPsiElementPointer myGetterPointer
;
44 private SmartPsiElementPointer mySetterPointer
;
45 private boolean myIsStatic
;
46 public static final Icon PROPERTY_READ_ICON
= loadIcon("/nodes/propertyRead.png");
47 public static final Icon PROPERTY_READ_STATIC_ICON
= loadIcon("/nodes/propertyReadStatic.png");
48 public static final Icon PROPERTY_WRITE_ICON
= loadIcon("/nodes/propertyWrite.png");
49 public static final Icon PROPERTY_WRITE_STATIC_ICON
= loadIcon("/nodes/propertyWriteStatic.png");
50 public static final Icon PROPERTY_READ_WRITE_ICON
= loadIcon("/nodes/propertyReadWrite.png");
51 public static final Icon PROPERTY_READ_WRITE_STATIC_ICON
= loadIcon("/nodes/propertyReadWriteStatic.png");
52 private final Project myProject
;
53 private final Collection
<TreeElement
> myChildren
= new ArrayList
<TreeElement
>();
55 private PropertyGroup(String propertyName
, PsiType propertyType
, boolean isStatic
, @NotNull Project project
) {
56 myPropertyName
= propertyName
;
57 myPropertyType
= propertyType
;
58 myIsStatic
= isStatic
;
62 public static PropertyGroup
createOn(PsiElement object
, final TreeElement treeElement
) {
63 if (object
instanceof PsiField
) {
64 PsiField field
= (PsiField
)object
;
65 PropertyGroup group
= new PropertyGroup(PropertyUtil
.suggestPropertyName(field
.getProject(), field
), field
.getType(),
66 field
.hasModifierProperty(PsiModifier
.STATIC
), object
.getProject());
67 group
.setField(field
);
68 group
.myChildren
.add(treeElement
);
71 else if (object
instanceof PsiMethod
) {
72 PsiMethod method
= (PsiMethod
)object
;
73 if (PropertyUtil
.isSimplePropertyGetter(method
)) {
74 PropertyGroup group
= new PropertyGroup(PropertyUtil
.getPropertyNameByGetter(method
), method
.getReturnType(),
75 method
.hasModifierProperty(PsiModifier
.STATIC
), object
.getProject());
76 group
.setGetter(method
);
77 group
.myChildren
.add(treeElement
);
80 else if (PropertyUtil
.isSimplePropertySetter(method
)) {
82 new PropertyGroup(PropertyUtil
.getPropertyNameBySetter(method
), method
.getParameterList().getParameters()[0].getType(),
83 method
.hasModifierProperty(PsiModifier
.STATIC
), object
.getProject());
84 group
.setSetter(method
);
85 group
.myChildren
.add(treeElement
);
92 public Collection
<TreeElement
> getChildren() {
96 public ItemPresentation
getPresentation() {
100 public Icon
getIcon(boolean open
) {
102 if (getGetter() != null && getSetter() != null) {
103 return PROPERTY_READ_WRITE_STATIC_ICON
;
105 else if (getGetter() != null) {
106 return PROPERTY_READ_STATIC_ICON
;
109 return PROPERTY_WRITE_STATIC_ICON
;
113 if (getGetter() != null && getSetter() != null) {
114 return PROPERTY_READ_WRITE_ICON
;
116 else if (getGetter() != null) {
117 return PROPERTY_READ_ICON
;
120 return PROPERTY_WRITE_ICON
;
126 private boolean isStatic() {
130 public String
getLocationString() {
134 public String
getPresentableText() {
135 return myPropertyName
+ ":" + myPropertyType
.getPresentableText();
138 public String
toString() {
139 return myPropertyName
;
143 public boolean equals(Object o
) {
144 if (this == o
) return true;
145 if (!(o
instanceof PropertyGroup
)) return false;
147 final PropertyGroup propertyGroup
= (PropertyGroup
)o
;
149 if (myPropertyName
!= null ?
!myPropertyName
.equals(propertyGroup
.myPropertyName
) : propertyGroup
.myPropertyName
!= null) return false;
151 if (myPropertyType
!= null && !myPropertyType
.isValid()) return false;
152 if (propertyGroup
.myPropertyType
!= null && !propertyGroup
.myPropertyType
.isValid()) return false;
154 if (myPropertyType
!= null && myPropertyType
.isValid()
155 ?
!myPropertyType
.equals(propertyGroup
.myPropertyType
)
156 : propertyGroup
.myPropertyType
!= null) {
164 public int hashCode() {
166 result
= myPropertyName
!= null?myPropertyName
.hashCode():0;
167 result
= 29 * result
+ (myPropertyType
!= null ? myPropertyType
.hashCode() : 0);
172 public String
getGetterName() {
173 return PropertyUtil
.suggestGetterName(myPropertyName
, myPropertyType
);
176 public int getAccessLevel() {
177 int result
= PsiUtil
.ACCESS_LEVEL_PRIVATE
;
178 if (getGetter() != null) {
179 result
= Math
.max(result
, PsiUtil
.getAccessLevel(getGetter().getModifierList()));
181 if (getSetter() != null) {
182 result
= Math
.max(result
, PsiUtil
.getAccessLevel(getSetter().getModifierList()));
184 if (getField() != null) {
185 result
= Math
.max(result
, PsiUtil
.getAccessLevel(getField().getModifierList()));
190 public int getSubLevel() {
194 public void setField(PsiField field
) {
195 myFieldPointer
= SmartPointerManager
.getInstance(myProject
).createSmartPsiElementPointer(field
);
196 myIsStatic
&= field
.hasModifierProperty(PsiModifier
.STATIC
);
199 public void setGetter(PsiMethod getter
) {
200 myGetterPointer
= SmartPointerManager
.getInstance(myProject
).createSmartPsiElementPointer(getter
);
201 myIsStatic
&= getter
.hasModifierProperty(PsiModifier
.STATIC
);
204 public void setSetter(PsiMethod setter
) {
205 mySetterPointer
= SmartPointerManager
.getInstance(myProject
).createSmartPsiElementPointer(setter
);
206 myIsStatic
&= setter
.hasModifierProperty(PsiModifier
.STATIC
);
209 public PsiField
getField() {
210 return (PsiField
)(myFieldPointer
== null ?
null : myFieldPointer
.getElement());
213 public PsiMethod
getGetter() {
214 return (PsiMethod
)(myGetterPointer
== null ?
null : myGetterPointer
.getElement());
217 public PsiMethod
getSetter() {
218 return (PsiMethod
)(mySetterPointer
== null ?
null : mySetterPointer
.getElement());
221 void copyAccessorsFrom(PropertyGroup group
) {
222 if (group
.getGetter() != null) setGetter(group
.getGetter());
223 if (group
.getSetter() != null) setSetter(group
.getSetter());
224 if (group
.getField() != null) setField(group
.getField());
225 myChildren
.addAll(group
.myChildren
);
228 private static Icon
loadIcon(@NonNls String resourceName
) {
229 Icon icon
= IconLoader
.findIcon(resourceName
);
230 Application application
= ApplicationManager
.getApplication();
231 if (icon
== null && application
!= null && application
.isUnitTestMode()) {
232 return new ImageIcon();
237 public TextAttributesKey
getTextAttributesKey() {
238 return isDeprecated() ? CodeInsightColors
.DEPRECATED_ATTRIBUTES
: null;
241 private boolean isDeprecated() {
242 return isDeprecated(getField()) && isDeprecated(getGetter()) && isDeprecated(getSetter());
245 private static boolean isDeprecated(final PsiElement element
) {
246 if (element
== null) return false;
247 if (!element
.isValid()) return false;
248 if (!(element
instanceof PsiDocCommentOwner
)) return false;
249 return ((PsiDocCommentOwner
)element
).isDeprecated();
252 public boolean isComplete() {
253 return getGetter() != null || getSetter() != null;
256 public Object
getValue() {
260 public int getWeight() {