View Javadoc

1   /**
2    * Copyright (C) 2008 Atlassian
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 com.atlassian.theplugin.idea.ui.tree;
18  
19  import javax.swing.tree.DefaultTreeModel;
20  import javax.swing.tree.TreeNode;
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.List;
24  
25  public class AtlassianTreeModel extends DefaultTreeModel {
26  
27  	public AtlassianTreeModel(AtlassianTreeNode root) {
28          super(root);
29  	}
30  
31  //	public AtlassianTreeNode locateNode(NodeSearchAlgorithm alg) {
32  //		return AtlassianTreeModel.locateNode(getRoot(), alg);
33  //	}
34  
35  	public AtlassianTreeModel getFilteredModel(Filter filter) {
36  		AtlassianTreeNode root = getRoot();
37  		AtlassianTreeNode newRoot = root.filter(filter);
38  		if (newRoot == null) {
39  			newRoot = AtlassianTreeNode.EMPTY_NODE;
40  		}
41  		return new AtlassianTreeModel(newRoot);
42  	}
43  
44  	@Override
45  	public AtlassianTreeNode getRoot() {
46  		return (AtlassianTreeNode) super.getRoot();
47  	}
48  
49  	public void insertNode(AtlassianTreeNode node, AtlassianTreeNode parent) {
50  		List<AtlassianTreeNode> children = createChildrenList(parent);
51  		removeAndSignal(parent, children);
52  //		parent.removeAllChildren();
53  		children.add(node);
54  		fillAndSignal(parent, children);
55  
56  		int[] idx = {parent.getIndex(node)};
57  //		nodesWereInserted(parent, idx);
58  
59  	}
60  
61  //	public void removeNode(AtlassianTreeNode node, AtlassianTreeNode parent) {
62  ////		int[] idx = {parent.getIndex(node)};
63  //		List<AtlassianTreeNode> children = createChildrenList(parent);
64  //		removeAndSignal(parent, children);
65  ////		parent.removeAllChildren();
66  //		children.remove(node);
67  //		fillAndSignal(parent, children);
68  //
69  //
70  //
71  ////		nodesWereRemoved(parent, idx, new Object[] { node });
72  //	}
73  
74  //	public void replaceNode(AtlassianTreeNode node, AtlassianTreeNode replaceWith) {
75  //
76  //		AtlassianTreeNode parent = (AtlassianTreeNode) node.getParent();
77  //
78  //		int[] idxOld = {parent.getIndex(node)};
79  //
80  //		List<AtlassianTreeNode> children = createChildrenList(parent);
81  //		removeAndSignal(parent, children);
82  ////		parent.removeAllChildren();
83  //		children.remove(node);
84  //		children.add(replaceWith);
85  //		fillAndSignal(parent, children);
86  //
87  ////		int[] idxNew = {parent.getIndex(replaceWith)};
88  ////
89  ////		nodesWereRemoved(parent, idxOld, new Object[] { node });
90  ////		nodesWereInserted(parent, idxNew);
91  //	}
92  
93  	private List<AtlassianTreeNode> createChildrenList(AtlassianTreeNode parent) {
94  		List<AtlassianTreeNode> children = new ArrayList<AtlassianTreeNode>();
95  		for (int i = 0; i < parent.getChildCount(); ++i) {
96  			children.add(parent.getChildAt(i));
97  		}
98  		return children;
99  	}
100 
101 	private void removeAndSignal(AtlassianTreeNode parent, List<AtlassianTreeNode> children) {
102 		TreeNode[] nodes = new TreeNode[children.size()];
103 		int[] indexes = new int[children.size()];
104 		for (int i = 0; i < children.size(); ++i) {
105 			indexes[i] = i;
106 		}
107 		children.toArray(nodes);
108 		parent.removeAllChildren();
109 		nodesWereRemoved(parent, indexes, nodes);
110 	}
111 
112 	private void fillAndSignal(AtlassianTreeNode parent, List<AtlassianTreeNode> children) {
113 		Collections.sort(children);
114 		for (AtlassianTreeNode ch : children) {
115 			parent.addNode(ch);
116 		}
117 
118 		int[] indexes = new int[children.size()];
119 		for (int i = 0; i < children.size(); ++i) {
120 			indexes[i] = i;
121 		}
122 
123 		nodesWereInserted(parent, indexes);
124 	}
125 //
126 //	private static AtlassianTreeNode locateNode(AtlassianTreeNode startingNode, NodeSearchAlgorithm alg) {
127 //		if (alg.check(startingNode)) {
128 //			return startingNode;
129 //		}
130 //		for (int i = 0; i < startingNode.getChildCount(); i++) {
131 //			AtlassianTreeNode result = locateNode(startingNode.getChildAt(i), alg);
132 //			if (result != null) {
133 //				return result;
134 //			}
135 //		}
136 //		return null;
137 //	}
138 
139 
140 }