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  
21  /**
22   * Created by IntelliJ IDEA.
23   * User: lguminski
24   * Date: Jul 11, 2008
25   * Time: 1:31:05 PM
26   * To change this template use File | Settings | File Templates.
27   */
28  public class AtlassianTreeModel extends DefaultTreeModel {
29  
30  	public AtlassianTreeModel(AtlassianTreeNode root) {
31          super(root);
32  	}
33  
34  	public AtlassianTreeNode locateNode(NodeSearchAlgorithm alg) {
35  		return AtlassianTreeModel.locateNode(getRoot(), alg);
36  	}
37  
38  	public AtlassianTreeModel getFilteredModel(Filter filter) {
39  		AtlassianTreeNode root = getRoot();
40  		AtlassianTreeNode newRoot = root.filter(filter);
41  		if (newRoot == null) {
42  			newRoot = AtlassianTreeNode.EMPTY_NODE;
43  		}
44  		return new AtlassianTreeModel(newRoot);
45  	}
46  
47  	@Override
48  	public AtlassianTreeNode getRoot() {
49  		return (AtlassianTreeNode) super.getRoot();
50  	}
51  
52  
53  	private static AtlassianTreeNode locateNode(AtlassianTreeNode startingNode, NodeSearchAlgorithm alg) {
54  		if (alg.check(startingNode)) {
55  			return startingNode;
56  		}
57  		for (int i = 0; i < startingNode.getChildCount(); i++) {
58  			AtlassianTreeNode result = locateNode(startingNode.getChildAt(i), alg);
59  			if (result != null) {
60  				return result;
61  			}
62  		}
63  		return null;
64  	}
65  
66  
67  }