KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SampleTreeCellRenderer


1 /*
2  * @(#)SampleTreeCellRenderer.java 1.20 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)SampleTreeCellRenderer.java 1.20 05/11/17
39  */

40
41 import javax.swing.Icon JavaDoc;
42 import javax.swing.ImageIcon JavaDoc;
43 import javax.swing.JLabel JavaDoc;
44 import javax.swing.JTree JavaDoc;
45 import javax.swing.tree.TreeCellRenderer JavaDoc;
46 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
47 import java.awt.Component JavaDoc;
48 import java.awt.Color JavaDoc;
49 import java.awt.Font JavaDoc;
50 import java.awt.Graphics JavaDoc;
51
52 public class SampleTreeCellRenderer extends JLabel JavaDoc implements TreeCellRenderer JavaDoc
53 {
54     /** Font used if the string to be displayed isn't a font. */
55     static protected Font JavaDoc defaultFont;
56     /** Icon to use when the item is collapsed. */
57     static protected ImageIcon JavaDoc collapsedIcon;
58     /** Icon to use when the item is expanded. */
59     static protected ImageIcon JavaDoc expandedIcon;
60
61     /** Color to use for the background when selected. */
62     static protected final Color JavaDoc SelectedBackgroundColor = Color.yellow;//new Color(0, 0, 128);
63

64     static
65     {
66     try {
67         defaultFont = new Font JavaDoc("SansSerif", 0, 12);
68     } catch (Exception JavaDoc e) {}
69     try {
70         collapsedIcon = new ImageIcon JavaDoc(SampleTreeCellRenderer.class.getResource("/resources/images/collapsed.gif"));
71             expandedIcon = new ImageIcon JavaDoc(SampleTreeCellRenderer.class.getResource("/resources/images/expanded.gif"));
72     } catch (Exception JavaDoc e) {
73         System.out.println("Couldn't load images: " + e);
74     }
75     }
76
77     /** Whether or not the item that was last configured is selected. */
78     protected boolean selected;
79
80     /**
81       * This is messaged from JTree whenever it needs to get the size
82       * of the component or it wants to draw it.
83       * This attempts to set the font based on value, which will be
84       * a TreeNode.
85       */

86     public Component JavaDoc getTreeCellRendererComponent(JTree JavaDoc tree, Object JavaDoc value,
87                       boolean selected, boolean expanded,
88                       boolean leaf, int row,
89                           boolean hasFocus) {
90     Font JavaDoc font;
91     String JavaDoc stringValue = tree.convertValueToText(value, selected,
92                        expanded, leaf, row, hasFocus);
93
94     /* Set the text. */
95     setText(stringValue);
96     /* Tooltips used by the tree. */
97     setToolTipText(stringValue);
98
99     /* Set the image. */
100     if(expanded)
101         setIcon(expandedIcon);
102     else if(!leaf)
103         setIcon(collapsedIcon);
104     else
105         setIcon(null);
106
107     /* Set the color and the font based on the SampleData userObject. */
108     SampleData userObject = (SampleData)((DefaultMutableTreeNode JavaDoc)value)
109                                     .getUserObject();
110     if(hasFocus)
111         setForeground(Color.cyan);
112     else
113         setForeground(userObject.getColor());
114     if(userObject.getFont() == null)
115         setFont(defaultFont);
116     else
117         setFont(userObject.getFont());
118
119     /* Update the selected flag for the next paint. */
120     this.selected = selected;
121
122     return this;
123     }
124
125     /**
126       * paint is subclassed to draw the background correctly. JLabel
127       * currently does not allow backgrounds other than white, and it
128       * will also fill behind the icon. Something that isn't desirable.
129       */

130     public void paint(Graphics JavaDoc g) {
131     Color JavaDoc bColor;
132     Icon JavaDoc currentI = getIcon();
133
134     if(selected)
135         bColor = SelectedBackgroundColor;
136     else if(getParent() != null)
137         /* Pick background color up from parent (which will come from
138            the JTree we're contained in). */

139         bColor = getParent().getBackground();
140     else
141         bColor = getBackground();
142     g.setColor(bColor);
143     if(currentI != null && getText() != null) {
144         int offset = (currentI.getIconWidth() + getIconTextGap());
145
146             if (getComponentOrientation().isLeftToRight()) {
147                 g.fillRect(offset, 0, getWidth() - 1 - offset,
148                            getHeight() - 1);
149             }
150             else {
151                 g.fillRect(0, 0, getWidth() - 1 - offset, getHeight() - 1);
152             }
153     }
154     else
155         g.fillRect(0, 0, getWidth()-1, getHeight()-1);
156     super.paint(g);
157     }
158 }
159
Popular Tags