1 package com
.intellij
.util
.xml
.tree
;
3 import com
.intellij
.openapi
.actionSystem
.*;
4 import com
.intellij
.openapi
.actionSystem
.ex
.ActionManagerEx
;
5 import com
.intellij
.openapi
.util
.IconLoader
;
6 import com
.intellij
.ui
.components
.panels
.Wrapper
;
7 import com
.intellij
.util
.ui
.tree
.TreeUtil
;
8 import com
.intellij
.util
.xml
.DomElement
;
9 import com
.intellij
.util
.xml
.DomEventListener
;
10 import com
.intellij
.util
.xml
.DomManager
;
11 import com
.intellij
.util
.xml
.DomChangeListener
;
12 import com
.intellij
.util
.xml
.events
.*;
13 import jetbrains
.fabrique
.ui
.treeStructure
.*;
14 import jetbrains
.fabrique
.ui
.treeStructure
.actions
.CollapseAllAction
;
15 import jetbrains
.fabrique
.ui
.treeStructure
.actions
.ExpandAllAction
;
18 import javax
.swing
.event
.TreeExpansionListener
;
19 import javax
.swing
.event
.TreeExpansionEvent
;
20 import javax
.swing
.tree
.DefaultMutableTreeNode
;
21 import javax
.swing
.tree
.DefaultTreeModel
;
22 import javax
.swing
.tree
.TreePath
;
25 import java
.util
.List
;
27 import org
.jetbrains
.annotations
.Nullable
;
29 public class DomModelTreeView
extends Wrapper
implements DataProvider
{
30 public static String DOM_MODEL_TREE_VIEW_KEY
= "DOM_MODEL_TREE_VIEW_KEY";
31 public static String DOM_MODEL_TREE_VIEW_POPUP
= "DOM_MODEL_TREE_VIEW_POPUP";
33 private final SimpleTree myTree
;
34 private final SimpleTreeBuilder myBuilder
;
35 private DomManager myDomManager
;
36 private DomEventListener myDomEventListener
;
37 private DomElement myRootElement
;
39 public DomModelTreeView(DomElement rootElement
) {
40 this(rootElement
, false);
43 public DomModelTreeView(DomElement rootElement
, boolean isRootVisible
) {
44 myRootElement
= rootElement
;
45 myTree
= new SimpleTree(new DefaultTreeModel(new DefaultMutableTreeNode()));
46 myTree
.setRootVisible(isRootVisible
);
47 myTree
.setShowsRootHandles(true);
49 ToolTipManager
.sharedInstance().registerComponent(myTree
);
50 TreeUtil
.installActions(myTree
);
52 final SimpleTreeStructure treeStructure
= rootElement
!= null ?
getTreeStructure(rootElement
) : new SimpleTreeStructure() {
53 public Object
getRootElement() {
58 myBuilder
= new SimpleTreeBuilder(myTree
, (DefaultTreeModel
)myTree
.getModel(), treeStructure
, WeightBasedComparator
.INSTANCE
);
60 myBuilder
.setNodeDescriptorComparator(null);
63 add(myTree
, BorderLayout
.CENTER
);
65 myTree
.addTreeExpansionListener(new TreeExpansionListener() {
66 public void treeExpanded(TreeExpansionEvent event
) {
67 final SimpleNode simpleNode
= myTree
.getNodeFor(event
.getPath());
69 if(simpleNode
instanceof AbstractDomElementNode
) {
70 ((AbstractDomElementNode
)simpleNode
).setExpanded(true);
74 public void treeCollapsed(TreeExpansionEvent event
) {
75 final SimpleNode simpleNode
= myTree
.getNodeFor(event
.getPath());
77 if(simpleNode
instanceof AbstractDomElementNode
) {
78 ((AbstractDomElementNode
)simpleNode
).setExpanded(false);
84 myDomEventListener
= new DomChangeListener() {
85 protected void elementChanged(DomElement element
) {
86 myBuilder
.updateFromRoot(false);
90 myDomManager
= rootElement
.getManager();
91 myDomManager
.addDomEventListener(myDomEventListener
);
93 myTree
.setPopupGroup(getPopupActions(), DOM_MODEL_TREE_VIEW_POPUP
);
96 public DomElement
getRootElement() {
100 protected SimpleTreeStructure
getTreeStructure(final DomElement rootDomElement
) {
101 return new DomModelTreeStructure(rootDomElement
);
104 public SimpleTreeBuilder
getBuilder() {
108 public void dispose() {
109 myDomManager
.removeDomEventListener(myDomEventListener
);
112 public SimpleTree
getTree() {
116 protected ActionGroup
getPopupActions() {
117 DefaultActionGroup group
= new DefaultActionGroup();
119 group
.add(ActionManagerEx
.getInstance().getAction("DomElementsTreeView.TreePopup"));
120 group
.addSeparator();
122 group
.add(new ExpandAllAction(myTree
));
123 group
.add(new CollapseAllAction(myTree
));
129 public Object
getData(String dataId
) {
130 if (DOM_MODEL_TREE_VIEW_KEY
.equals(dataId
)) {
136 public void setSelectedDomElement(final DomElement domElement
) {
137 if(domElement
== null) return;
139 final List
<SimpleNode
> parentsNodes
= getNodesFor(domElement
);
142 if(parentsNodes
.size() > 0) {
143 getTree().setSelectedNode(getBuilder(), parentsNodes
.get(parentsNodes
.size()-1), true);
147 private List
<SimpleNode
> getNodesFor(final DomElement domElement
) {
148 final List
<SimpleNode
> parentsNodes
= new ArrayList
<SimpleNode
>();
150 SimpleNodeVisitor visitor
= new SimpleNodeVisitor() {
151 public boolean accept(SimpleNode simpleNode
) {
152 if(simpleNode
instanceof BaseDomElementNode
) {
153 final DomElement nodeElement
= ((AbstractDomElementNode
)simpleNode
).getDomElement();
154 if(isParent(nodeElement
, domElement
)) {
155 parentsNodes
.add(simpleNode
);
161 getTree().accept(getBuilder(), visitor
);
166 private boolean isParent(final DomElement potentialParent
, final DomElement domElement
) {
167 DomElement currParent
= domElement
;
168 while(currParent
!= null) {
169 if(currParent
.equals(potentialParent
)) return true;
171 currParent
= currParent
.getParent();