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.
18 * class ViewerTreeStructure
19 * created Aug 25, 2001
22 package com
.intellij
.internal
.psiView
;
24 import com
.intellij
.ide
.util
.treeView
.AbstractTreeStructure
;
25 import com
.intellij
.ide
.util
.treeView
.NodeDescriptor
;
26 import com
.intellij
.lang
.ASTNode
;
27 import com
.intellij
.openapi
.application
.ApplicationManager
;
28 import com
.intellij
.openapi
.project
.Project
;
29 import com
.intellij
.psi
.PsiElement
;
30 import com
.intellij
.psi
.PsiFile
;
31 import com
.intellij
.psi
.PsiWhiteSpace
;
32 import com
.intellij
.psi
.TokenType
;
33 import com
.intellij
.psi
.impl
.source
.SourceTreeToPsiMap
;
34 import com
.intellij
.util
.ArrayUtil
;
35 import org
.jetbrains
.annotations
.NotNull
;
37 import java
.util
.ArrayList
;
38 import java
.util
.List
;
40 public class ViewerTreeStructure
extends AbstractTreeStructure
{
42 private boolean myShowWhiteSpaces
= true;
43 private boolean myShowTreeNodes
= true;
45 private final Project myProject
;
46 private PsiElement myRootPsiElement
= null;
47 private final Object myRootElement
= new Object();
49 public ViewerTreeStructure(Project project
) {
53 public void setRootPsiElement(PsiElement rootPsiElement
) {
54 myRootPsiElement
= rootPsiElement
;
57 public PsiElement
getRootPsiElement() {
58 return myRootPsiElement
;
61 public Object
getRootElement() {
65 public Object
[] getChildElements(final Object element
) {
66 if (myRootElement
== element
) {
67 if (myRootPsiElement
== null) {
68 return ArrayUtil
.EMPTY_OBJECT_ARRAY
;
70 return myRootPsiElement
instanceof PsiFile ?
((PsiFile
)myRootPsiElement
).getPsiRoots() : new Object
[]{myRootPsiElement
};
72 final Object
[][] children
= new Object
[1][];
73 children
[0] = ArrayUtil
.EMPTY_OBJECT_ARRAY
;
74 ApplicationManager
.getApplication().runReadAction(new Runnable() {
76 final Object
[] result
;
77 if (myShowTreeNodes
) {
78 final ArrayList
<Object
> list
= new ArrayList
<Object
>();
79 final ASTNode root
= element
instanceof PsiElement? SourceTreeToPsiMap
.psiElementToTree((PsiElement
)element
) :
80 element
instanceof ASTNode?
(ASTNode
)element
: null;
82 ASTNode child
= root
.getFirstChildNode();
83 while (child
!= null) {
84 if (myShowWhiteSpaces
|| child
.getElementType() != TokenType
.WHITE_SPACE
) {
85 final PsiElement childElement
= child
.getPsi();
86 list
.add(childElement
== null ? child
: childElement
);
88 child
= child
.getTreeNext();
91 result
= ArrayUtil
.toObjectArray(list
);
94 final PsiElement
[] elementChildren
= ((PsiElement
)element
).getChildren();
95 if (!myShowWhiteSpaces
) {
96 final List
<PsiElement
> childrenList
= new ArrayList
<PsiElement
>(elementChildren
.length
);
97 for (PsiElement psiElement
: elementChildren
) {
98 if (!myShowWhiteSpaces
&& psiElement
instanceof PsiWhiteSpace
) {
101 childrenList
.add(psiElement
);
103 result
= childrenList
.toArray(new PsiElement
[childrenList
.size()]);
106 result
= elementChildren
;
109 children
[0] = result
;
115 public Object
getParentElement(Object element
) {
116 if (element
== myRootElement
) {
119 if (element
== myRootPsiElement
) {
120 return myRootElement
;
122 return ((PsiElement
)element
).getParent();
125 public void commit() {
128 public boolean hasSomethingToCommit() {
133 public NodeDescriptor
createDescriptor(Object element
, NodeDescriptor parentDescriptor
) {
134 if (element
== myRootElement
) {
135 return new NodeDescriptor(myProject
, null) {
136 public boolean update() {
139 public Object
getElement() {
140 return myRootElement
;
144 return new ViewerNodeDescriptor(myProject
, element
, parentDescriptor
);
147 public boolean isShowWhiteSpaces() {
148 return myShowWhiteSpaces
;
151 public void setShowWhiteSpaces(boolean showWhiteSpaces
) {
152 myShowWhiteSpaces
= showWhiteSpaces
;
155 public boolean isShowTreeNodes() {
156 return myShowTreeNodes
;
159 public void setShowTreeNodes(final boolean showTreeNodes
) {
160 myShowTreeNodes
= showTreeNodes
;