KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > outline > DefaultOutlineCellRenderer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.swing.outline;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.Insets JavaDoc;
25 import javax.swing.BorderFactory JavaDoc;
26 import javax.swing.Icon JavaDoc;
27 import javax.swing.JTable JavaDoc;
28 import javax.swing.UIManager JavaDoc;
29 import javax.swing.border.Border JavaDoc;
30 import javax.swing.table.DefaultTableCellRenderer JavaDoc;
31 import javax.swing.tree.AbstractLayoutCache JavaDoc;
32 import javax.swing.tree.TreePath JavaDoc;
33
34 /** An outline-aware TableCellRenderer which knows how to paint expansion
35  * handles and indent child nodes an appropriate amount.
36  *
37  * @author Tim Boudreau
38  */

39 public class DefaultOutlineCellRenderer extends DefaultTableCellRenderer JavaDoc {
40     private boolean expanded = false;
41     private boolean leaf = true;
42     private boolean showHandle = true;
43     private int nestingDepth = 0;
44     private static final Border JavaDoc expansionBorder = new ExpansionHandleBorder();
45     
46     /** Creates a new instance of DefaultOutlineTreeCellRenderer */
47     public DefaultOutlineCellRenderer() {
48     }
49     
50     /** Overridden to combine the expansion border (whose insets determine how
51      * much a child tree node is shifted to the right relative to the ancestor
52      * root node) with whatever border is set, as a CompoundBorder. The expansion
53      * border is also responsible for drawing the expansion icon. */

54     public final void setBorder (Border JavaDoc b) {
55         if (b == expansionBorder) {
56             super.setBorder(b);
57         } else {
58             super.setBorder(BorderFactory.createCompoundBorder (b, expansionBorder));
59         }
60     }
61     
62     private static Icon JavaDoc getDefaultOpenIcon() {
63     return UIManager.getIcon("Tree.openIcon"); //NOI18N
64
}
65
66     private static Icon JavaDoc getDefaultClosedIcon() {
67     return UIManager.getIcon("Tree.closedIcon"); //NOI18N
68
}
69
70     private static Icon JavaDoc getDefaultLeafIcon() {
71     return UIManager.getIcon("Tree.leafIcon"); //NOI18N
72
}
73     
74     private static Icon JavaDoc getExpandedIcon() {
75         return UIManager.getIcon ("Tree.collapsedIcon"); //NOI18N
76
}
77     
78     private static Icon JavaDoc getCollapsedIcon() {
79         return UIManager.getIcon ("Tree.expandedIcon"); //NOI18N
80
}
81     
82     static int getNestingWidth() {
83         return getExpansionHandleWidth();
84     }
85     
86     static int getExpansionHandleWidth() {
87         return getExpandedIcon().getIconWidth();
88     }
89     
90     static int getExpansionHandleHeight() {
91         return getExpandedIcon().getIconHeight();
92     }
93     
94     private void setNestingDepth (int i) {
95         nestingDepth = i;
96     }
97     
98     private void setExpanded (boolean val) {
99         expanded = val;
100     }
101     
102     private void setLeaf (boolean val) {
103         leaf = val;
104     }
105     
106     private void setShowHandle (boolean val) {
107         showHandle = val;
108     }
109     
110     private boolean isLeaf () {
111         return leaf;
112     }
113     
114     private boolean isExpanded () {
115         return expanded;
116     }
117     
118     private boolean isShowHandle() {
119         return showHandle;
120     }
121     
122     /** Set the nesting depth - the number of path elements below the root.
123      * This is set in getTableCellEditorComponent(), and retrieved by the
124      * expansion border to determine how far to the right to indent the current
125      * node. */

126     private int getNestingDepth() {
127         return nestingDepth;
128     }
129     
130     /** Get a component that can render cells in an Outline. If
131      * <code>((Outline) table).isTreeColumnIndex(column)</code> is true,
132      * it will paint as indented and with an expansion handle if the
133      * Outline's model returns false from <code>isLeaf</code> for the
134      * passed value.
135      * <p>
136      * If the column is not the tree column, its behavior is the same as
137      * DefaultTableCellRenderer.
138      */

139     public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc table, Object JavaDoc value,
140                           boolean isSelected, boolean hasFocus, int row,
141                           int column) {
142     
143         Component JavaDoc c = (DefaultOutlineCellRenderer) super.getTableCellRendererComponent(
144               table, value, isSelected, hasFocus, row, column);
145         Outline tbl = (Outline) table;
146         // IZ 57135 JTable will pass a null value here in response to a valueChanged
147
// event so null must be an acceptable parameter. We will return the default
148
// component in the case of a null value.
149
if (value != null && tbl.isTreeColumnIndex(column)) {
150             AbstractLayoutCache JavaDoc layout = tbl.getLayoutCache();
151             
152             boolean leaf = tbl.getOutlineModel().isLeaf(value);
153             setLeaf(leaf);
154             setShowHandle(true);
155             TreePath JavaDoc path = layout.getPathForRow(row);
156             boolean expanded = !layout.isExpanded(path);
157             setExpanded (expanded);
158             setNestingDepth (path.getPathCount() - 1);
159             RenderDataProvider rendata = tbl.getRenderDataProvider();
160             Icon JavaDoc icon = null;
161             if (rendata != null) {
162                 String JavaDoc displayName = rendata.getDisplayName(value);
163                 if (displayName != null) {
164                     setText (displayName);
165                 }
166                 setToolTipText (rendata.getTooltipText(value));
167                 Color JavaDoc bg = rendata.getBackground(value);
168                 Color JavaDoc fg = rendata.getForeground(value);
169                 if (bg != null && !isSelected) {
170                     setBackground (bg);
171                 } else {
172                     setBackground (isSelected ?
173                         tbl.getSelectionBackground() : tbl.getBackground());
174                 }
175                 if (fg != null && !isSelected) {
176                     setForeground (fg);
177                 } else {
178                     setForeground (isSelected ?
179                         tbl.getSelectionForeground() : tbl.getForeground());
180                 }
181                 icon = rendata.getIcon(value);
182             }
183             if (icon == null) {
184                 if (!leaf) {
185                     if (expanded) {
186                         setIcon (getDefaultClosedIcon());
187                     } else {
188                         setIcon (getDefaultOpenIcon());
189                     }
190                 } else {
191                     setIcon (getDefaultLeafIcon());
192                 }
193             }
194         
195         } else {
196             setIcon(null);
197             setShowHandle(false);
198         }
199         return this;
200     }
201     
202     private static class ExpansionHandleBorder implements Border JavaDoc {
203         private Insets JavaDoc insets = new Insets JavaDoc(0,0,0,0);
204         public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
205             DefaultOutlineCellRenderer ren = (DefaultOutlineCellRenderer) c;
206             if (ren.isShowHandle()) {
207                 insets.left = getExpansionHandleWidth() + (ren.getNestingDepth() *
208                     getNestingWidth());
209                 //Defensively adjust all the insets fields
210
insets.top = 1;
211                 insets.right = 1;
212                 insets.bottom = 1;
213             } else {
214                 //Defensively adjust all the insets fields
215
insets.left = 1;
216                 insets.top = 1;
217                 insets.right = 1;
218                 insets.bottom = 1;
219             }
220             return insets;
221         }
222         
223         public boolean isBorderOpaque() {
224             return false;
225         }
226         
227         public void paintBorder(Component JavaDoc c, java.awt.Graphics JavaDoc g, int x, int y, int width, int height) {
228             DefaultOutlineCellRenderer ren = (DefaultOutlineCellRenderer) c;
229             if (ren.isShowHandle() && !ren.isLeaf()) {
230                 Icon JavaDoc icon = ren.isExpanded() ? getExpandedIcon() : getCollapsedIcon();
231                 int iconY;
232                 int iconX = ren.getNestingDepth() * getNestingWidth();
233                 if (icon.getIconHeight() < height) {
234                     iconY = (height / 2) - (icon.getIconHeight() / 2);
235                 } else {
236                     iconY = 0;
237                 }
238                 icon.paintIcon(c, g, iconX, iconY);
239             }
240         }
241     }
242 }
243
Popular Tags