ef43699bc3162494ec5717ce70304e37cdaab09b
[idea/community.git] / plugins / groovy / src / org / jetbrains / plugins / groovy / lang / psi / impl / synthetic / GrDynamicImplicitProperty.java
1 /*
2  * Copyright 2000-2009 JetBrains s.r.o.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.jetbrains.plugins.groovy.lang.psi.impl.synthetic;
18
19 import com.intellij.openapi.editor.colors.TextAttributesKey;
20 import com.intellij.openapi.project.Project;
21 import com.intellij.openapi.wm.ToolWindowManager;
22 import com.intellij.psi.*;
23 import com.intellij.psi.javadoc.PsiDocComment;
24 import com.intellij.psi.search.GlobalSearchScope;
25 import com.intellij.psi.search.ProjectScope;
26 import com.intellij.psi.search.SearchScope;
27 import com.intellij.util.ui.tree.TreeUtil;
28 import com.intellij.ui.treeStructure.treetable.ListTreeTableModelOnColumns;
29 import com.intellij.ui.treeStructure.treetable.TreeTable;
30 import org.jetbrains.annotations.NonNls;
31 import org.jetbrains.annotations.NotNull;
32 import org.jetbrains.annotations.Nullable;
33 import org.jetbrains.plugins.groovy.GroovyIcons;
34 import org.jetbrains.plugins.groovy.annotator.intentions.dynamic.DynamicManager;
35 import org.jetbrains.plugins.groovy.annotator.intentions.dynamic.DynamicToolWindowWrapper;
36 import org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DClassElement;
37 import org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DPropertyElement;
38 import org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil;
39
40 import javax.swing.*;
41 import javax.swing.tree.DefaultMutableTreeNode;
42 import javax.swing.tree.TreePath;
43
44 /**
45  * @author ilyas
46  */
47 public class GrDynamicImplicitProperty extends GrImplicitVariableImpl implements GrDynamicImplicitElement, PsiField {
48   private final String myContainingClassName;
49   private final Project myProject;
50
51   public GrDynamicImplicitProperty(PsiManager manager, @NonNls String name, @NonNls String type, String containingClassName, LightModifierList modifierList) {
52     super(modifierList, manager, name, type, null);
53     myContainingClassName = containingClassName;
54     myProject = manager.getProject();
55   }
56
57   @Nullable
58   public PsiClass getContainingClassElement() {
59     final PsiClassType containingClassType = JavaPsiFacade.getInstance(getProject()).getElementFactory().
60         createTypeByFQClassName(myContainingClassName, ProjectScope.getAllScope(getProject()));
61
62     return containingClassType.resolve();
63   }
64
65   public String getContainingClassName() {
66     return myContainingClassName;
67   }
68
69   @Override
70   public PsiFile getContainingFile() {
71     final PsiClass psiClass = getContainingClassElement();
72     if (psiClass == null) return null;
73
74     return psiClass.getContainingFile();
75   }
76
77   public String getPresentableText() {
78     return getName();
79   }
80
81   @Nullable
82   public String getLocationString() {
83     return null;
84   }
85
86   @Nullable
87   public TextAttributesKey getTextAttributesKey() {
88     return null;
89   }
90
91   @NotNull
92   public SearchScope getUseScope() {
93     return GlobalSearchScope.projectScope(myProject);
94   }
95
96   public void navigate(boolean requestFocus) {
97     DynamicToolWindowWrapper.getInstance(myProject).getToolWindow().activate(new Runnable() {
98       public void run() {
99         DynamicToolWindowWrapper toolWindowWrapper = DynamicToolWindowWrapper.getInstance(myProject);
100         final TreeTable treeTable = toolWindowWrapper.getTreeTable();
101         final ListTreeTableModelOnColumns model = toolWindowWrapper.getTreeTableModel();
102
103         Object root = model.getRoot();
104
105         if (root == null || !(root instanceof DefaultMutableTreeNode)) return;
106
107         DefaultMutableTreeNode treeRoot = ((DefaultMutableTreeNode) root);
108         final PsiClass psiClass = getContainingClassElement();
109         if (psiClass == null) return;
110
111         final DefaultMutableTreeNode desiredNode;
112         DPropertyElement dynamicProperty = null;
113         PsiClass trueSuper = null;
114         for (PsiClass aSuper : PsiUtil.iterateSupers(psiClass, true)) {
115           dynamicProperty = DynamicManager.getInstance(myProject).findConcreteDynamicProperty(aSuper.getQualifiedName(), getName());
116
117           if (dynamicProperty != null) {
118             trueSuper = aSuper;
119             break;
120           }
121         }
122
123         if (trueSuper == null) return;
124
125         final DefaultMutableTreeNode classNode = TreeUtil.findNodeWithObject(treeRoot, new DClassElement(myProject, trueSuper.getQualifiedName()));
126         if (classNode == null) return;
127
128         desiredNode = TreeUtil.findNodeWithObject(classNode, dynamicProperty);
129
130         if (desiredNode == null) return;
131         final TreePath path = TreeUtil.getPathFromRoot(desiredNode);
132
133         treeTable.getTree().expandPath(path);
134         treeTable.getTree().setSelectionPath(path);
135         treeTable.getTree().fireTreeExpanded(path);
136
137         ToolWindowManager.getInstance(myProject).getFocusManager().requestFocus(treeTable, true);
138         treeTable.revalidate();
139         treeTable.repaint();
140
141       }
142     }, true);
143   }
144
145   public boolean canNavigateToSource() {
146     return false;
147   }
148
149   public boolean canNavigate() {
150     return true;
151   }
152
153   public boolean isWritable() {
154     return true;
155   }
156
157   @Nullable
158   public Icon getIcon(boolean open) {
159     return GroovyIcons.PROPERTY;
160   }
161
162   public boolean isValid() {
163     return true;
164   }
165
166   public PsiClass getContainingClass() {
167     return getContainingClassElement();
168   }
169
170     public PsiDocComment getDocComment() {
171         return null;
172     }
173
174     public boolean isDeprecated() {
175         return false;
176     }
177 }