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.file;
18  
19  import com.atlassian.theplugin.idea.ui.tree.AtlassianClickAction;
20  import com.atlassian.theplugin.idea.ui.tree.AtlassianTreeNode;
21  import com.atlassian.theplugin.idea.ui.tree.Filter;
22  import com.intellij.ui.ColoredTreeCellRenderer;
23  import com.intellij.ui.SimpleTextAttributes;
24  import com.intellij.util.Icons;
25  
26  import javax.swing.*;
27  import javax.swing.tree.TreeCellRenderer;
28  
29  /**
30   * Created by IntelliJ IDEA.
31   * User: pmaruszak
32   * Date: Aug 7, 2008
33   * Time: 11:09:57 AM
34   * To change this template use File | Settings | File Templates.
35   */
36  public class FolderNode extends FileNode {
37  	private static final ColoredTreeCellRenderer MY_RENDERER = new FileNodeRenderer();
38  	
39  	public FolderNode(String fullName, AtlassianClickAction action) {
40  		super(fullName, action);
41  	}
42  
43  	public FolderNode(final FileNode node) {
44  		super(node);
45  	}
46  
47  	public FolderNode(final String name) {
48  		this(name, AtlassianClickAction.EMPTY_ACTION);
49  	}
50  
51  	public AtlassianTreeNode getClone() {
52  		return new FolderNode(this);
53  	}
54  
55  	public AtlassianTreeNode filter(final Filter filter) {
56  		AtlassianTreeNode result = null;
57  		if (filter.isValid(this)) {
58  			result = getClone();
59  			for (int i = 0; i < getChildCount(); i++) {
60  				AtlassianTreeNode child = getChildAt(i).filter(filter);
61  				if (child != null) {
62  					result.addNode(child);
63  				}
64  			}
65  			if (result.getChildCount() <= 0) {
66  					result = null;
67  			}
68  
69  		}
70  		return result;
71  	}
72  
73  	public TreeCellRenderer getTreeCellRenderer() {
74  		return MY_RENDERER;
75  	}
76  
77  	private static class FileNodeRenderer extends ColoredTreeCellRenderer {
78  		public void customizeCellRenderer(JTree tree, Object value, boolean selected, boolean expanded,
79                  boolean leaf, int row, boolean hasFocus) {
80  			FileNode node = (FileNode) value;
81  			append(node.getName(), SimpleTextAttributes.REGULAR_ATTRIBUTES);
82  
83  			setIcon(expanded ? Icons.DIRECTORY_OPEN_ICON : Icons.DIRECTORY_CLOSED_ICON);
84  		}
85  	}
86  }