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.*;
20  import javax.swing.tree.DefaultMutableTreeNode;
21  import javax.swing.tree.TreeCellRenderer;
22  import java.awt.*;
23  
24  public abstract class AtlassianTreeNode extends DefaultMutableTreeNode implements Comparable {
25  	private AtlassianClickAction action;
26  
27  	protected AtlassianTreeNode(AtlassianClickAction action) {
28  		this.action = action;
29  	}
30  
31  	public void addNode(AtlassianTreeNode newChild) {
32  		super.add(newChild);
33  	}
34  
35  	@Override
36  	public AtlassianTreeNode getChildAt(final int i) {
37  		return (AtlassianTreeNode) super.getChildAt(i);
38  	}
39  
40  	public abstract TreeCellRenderer getTreeCellRenderer();
41  
42  	public AtlassianClickAction getAtlassianClickAction() {
43  		return action;
44  	}
45  
46  	public abstract AtlassianTreeNode getClone();
47  
48  	public AtlassianTreeNode filter(final Filter filter) {
49  		AtlassianTreeNode result = null;
50  		if (filter.isValid(this)) {
51  			AtlassianTreeNode tempResult = getClone();
52  			boolean foundValidChild = false;
53  			for (int i = 0; i < getChildCount(); i++) {
54  				AtlassianTreeNode child = getChildAt(i).filter(filter);
55  				if (child != null) {
56  					foundValidChild = true;
57  					tempResult.addNode(child);
58  				}
59  			}
60  			if (foundValidChild || getChildCount() == 0) {
61  				result = tempResult;
62  			}
63  		}
64  		return result;
65  	}
66  
67  	public static final AtlassianTreeNode EMPTY_NODE = new AtlassianTreeNode(AtlassianClickAction.EMPTY_ACTION) {
68  		@Override
69  		public TreeCellRenderer getTreeCellRenderer() {
70  			return new TreeCellRenderer() {
71  				public Component getTreeCellRendererComponent(final JTree jTree, final Object o, final boolean b,
72  						final boolean b1, final boolean b2, final int i, final boolean b3) {
73  					return new JLabel("<Empty>");
74  				}
75  			};
76  		}
77  
78  		@Override
79  		public AtlassianTreeNode getClone() {
80  			return AtlassianTreeNode.EMPTY_NODE;
81  		}
82  
83  		public int compareTo(Object o) {
84  			// hmm, is it ok?
85  			return -1;
86  		}
87  	};
88  
89  }