KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > schlichtherle > io > swing > tree > FileTreeCellRenderer


1 /*
2  * FileTreeCellRenderer.java
3  *
4  * Created on 13. Februar 2006, 15:45
5  */

6 /*
7  * Copyright 2006 Schlichtherle IT Services
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package de.schlichtherle.io.swing.tree;
23
24 import de.schlichtherle.io.swing.FileSystemView;
25 import de.schlichtherle.io.swing.JFileTree;
26
27 import java.awt.Component JavaDoc;
28 import javax.swing.Icon JavaDoc;
29 import javax.swing.JTree JavaDoc;
30 import javax.swing.tree.DefaultTreeCellRenderer JavaDoc;
31
32 /**
33  * A {@link javax.swing.tree.TreeCellRenderer} which uses an instance of
34  * {@link FileSystemView} to display the system icon for each node in a
35  * {@link JFileTree} wherever possible.
36  *
37  * @author Christian Schlichtherle
38  * @version @version@
39  * @since TrueZIP 5.1
40  */

41 public class FileTreeCellRenderer extends DefaultTreeCellRenderer JavaDoc {
42
43     private final JFileTree fileTree;
44     
45     public FileTreeCellRenderer(final JFileTree fileTree) {
46         this.fileTree = fileTree;
47     }
48
49     public Icon JavaDoc getOpenIcon() {
50         // This is a hack: When editing a node, the edited node is the ONLY
51
// one for which the BasicTreeUI somehow BYPASSES the call to
52
// getTreeCellRendererComponent(*) and calls this method directly in
53
// order to determine the icon to draw for the edited node.
54
// So we simply return the icon for the edited node for ALL nodes
55
// while editing and rely on getTreeCellRendererComponent(*) to fix
56
// the icon for all other nodes than the edited one.
57
final java.io.File JavaDoc node = fileTree.getEditedNode();
58         if (node != null)
59             return FileSystemView.getFileSystemView().getSystemIcon(node);
60         else
61             return super.getOpenIcon();
62     }
63
64     public Icon JavaDoc getClosedIcon() {
65         // This is a hack: When editing a node, the edited node is the ONLY
66
// one for which the BasicTreeUI somehow BYPASSES the call to
67
// getTreeCellRendererComponent(*) and calls this method directly in
68
// order to determine the icon to draw for the edited node.
69
// So we simply return the icon for the edited node for ALL nodes
70
// while editing and rely on getTreeCellRendererComponent(*) to fix
71
// the icon for all other nodes than the edited one.
72
final java.io.File JavaDoc node = fileTree.getEditedNode();
73         if (node != null)
74             return FileSystemView.getFileSystemView().getSystemIcon(node);
75         else
76             return super.getClosedIcon();
77     }
78
79     public Icon JavaDoc getLeafIcon() {
80         // This is a hack: When editing a node, the edited node is the ONLY
81
// one for which the BasicTreeUI somehow BYPASSES the call to
82
// getTreeCellRendererComponent(*) and calls this method directly in
83
// order to determine the icon to draw for the edited node.
84
// So we simply return the icon for the edited node for ALL nodes
85
// while editing and rely on getTreeCellRendererComponent(*) to fix
86
// the icon for all other nodes than the edited one.
87
final java.io.File JavaDoc node = fileTree.getEditedNode();
88         if (node != null)
89             return FileSystemView.getFileSystemView().getSystemIcon(node);
90         else
91             return super.getLeafIcon();
92     }
93
94     public Component JavaDoc getTreeCellRendererComponent(
95             final JTree JavaDoc tree,
96             final Object JavaDoc value,
97             final boolean selected,
98             final boolean expanded,
99             final boolean leaf,
100             final int row,
101             final boolean hasFocus) {
102         super.getTreeCellRendererComponent(
103                 tree, value, selected, expanded, leaf, row, hasFocus);
104
105         setIcon(FileSystemView.getFileSystemView().getSystemIcon((java.io.File JavaDoc) value));
106
107         return this;
108     }
109 }
110
Popular Tags