KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > MyTreeRenderer


1 /*
2  * Copyright (C) 2004 Sun Microsystems, Inc. All rights reserved. Use is
3  * subject to license terms.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the Lesser GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * 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
18  * USA.
19  */

20
21 import javax.swing.tree.DefaultTreeCellRenderer JavaDoc;
22 import java.awt.Component JavaDoc;
23 import javax.swing.JTree JavaDoc;
24 import java.io.File JavaDoc;
25
26
27 /**
28  * JDIC API demo class.
29  * <p>
30  * A redefined TreeCellRenderer class.
31  */

32
33 public class MyTreeRenderer extends DefaultTreeCellRenderer JavaDoc {
34     public Component JavaDoc getTreeCellRendererComponent(
35             JTree JavaDoc tree, Object JavaDoc value, boolean isSelected, boolean isExpanded,
36             boolean leaf, int row, boolean hasFocus) {
37         Component JavaDoc component = super.getTreeCellRendererComponent(tree, value,
38                 isSelected, isExpanded, leaf, row, hasFocus);
39
40         if (value != null && value instanceof MyTreeNode) {
41             MyTreeNode treeNode = (MyTreeNode) value;
42             // ===
43
// For Windows "My Computer" node only.
44
// ===
45
File JavaDoc selectedDir = (File JavaDoc) treeNode.getUserObject();
46
47             if (selectedDir.equals(new File JavaDoc(FileExplorer.MY_COMPUTER_FOLDER_PATH))) {
48                 setIcon(FileExplorer.computerIcon);
49             } else if (selectedDir.getParent() == null) {
50                 setIcon(FileExplorer.driverIcon);
51             } else {
52                 setIcon(FileExplorer.folderIcon);
53             }
54
55             return component;
56         }
57
58         return this;
59     }
60 }
61
Popular Tags