View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.gui.tree;
21  
22  import java.awt.Component;
23  import java.io.File;
24  
25  import javax.swing.Icon;
26  import javax.swing.JTree;
27  import javax.swing.tree.DefaultTreeCellRenderer;
28  
29  import org.xnap.gui.util.IconHelper;
30  
31  /***
32   * Renders tree nodes of type {@link File} and {@link String}.
33   * 
34   * Files are displayed as green folders, Strings as yellow folders.
35   */
36  public class FileCellRenderer extends DefaultTreeCellRenderer
37  {
38  
39      public static final Icon[] folders = {
40  	IconHelper.getMenuIcon("folder.png"), 
41  	IconHelper.getMenuIcon("folder_open.png"),
42  	IconHelper.getMenuIcon("folder_yellow.png"),
43  	IconHelper.getMenuIcon("folder_yellow_open.png")
44      };
45  
46      // --- Constructor(s) ---
47  
48      public FileCellRenderer()
49      {
50      }
51  
52      // --- Method(s) ---
53  
54      public Component getTreeCellRendererComponent(JTree tree, Object node, 
55  						  boolean sel,
56  						  boolean expanded,
57  						  boolean leaf, int row, 
58  						  boolean hasFocus) 
59      {
60  	super.getTreeCellRendererComponent(tree, node, sel, expanded, leaf, 
61  					   row, hasFocus);
62  	
63  	if (node instanceof File || (node instanceof SearchPathNode)) {
64  	    if (expanded) {
65  		setIcon(folders[1]);
66  	    }
67  	    else {
68  		setIcon(folders[0]);
69  	    }
70  	}
71  	else if (node instanceof String) {
72  	    if (expanded) {
73  		setIcon(folders[3]);
74  	    }
75  	    else {
76  		setIcon(folders[2]);
77  	    }
78  	}
79  	return this;
80      }
81  }