KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > search > NodeRenderer


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.search;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.Dimension JavaDoc;
25 import java.awt.Graphics JavaDoc;
26 import java.awt.Image JavaDoc;
27 import java.awt.Rectangle JavaDoc;
28 import java.beans.BeanInfo JavaDoc;
29 import javax.swing.ImageIcon JavaDoc;
30 import javax.swing.JCheckBox JavaDoc;
31 import javax.swing.JComponent JavaDoc;
32 import javax.swing.JLabel JavaDoc;
33 import javax.swing.JTree JavaDoc;
34 import javax.swing.UIManager JavaDoc;
35 import javax.swing.tree.TreeCellRenderer JavaDoc;
36 import javax.swing.tree.TreePath JavaDoc;
37 import org.openide.awt.HtmlRenderer;
38 import org.openide.loaders.DataObject;
39 import org.openide.nodes.Node;
40 import org.openide.util.NbBundle;
41 import org.openide.util.Utilities;
42
43 /**
44  *
45  * @author Marian Petras
46  */

47 final class NodeRenderer extends JComponent JavaDoc implements TreeCellRenderer JavaDoc {
48
49     /** */
50     private static Rectangle JavaDoc checkBounds;
51     
52     /** */
53     private final HtmlRenderer.Renderer nodeRenderer;
54     /** */
55     private final Image JavaDoc rootIconImage;
56     /** */
57     private final JCheckBox JavaDoc checkBox;
58
59     /** */
60     private Image JavaDoc deletedObjectIconImage;
61     /** */
62     private String JavaDoc deletedObjectHtmlSuffix;
63     /**
64      * the component returned by
65      * {@link HtmlRenderer.Renderer#getTreeCellRendererComponent() getTreeCellRendererComponent()}
66      */

67     private Component JavaDoc stringDisplayer = new JLabel JavaDoc(" "); //NOI18N
68

69     /**
70      *
71      */

72     NodeRenderer(final boolean withCheckBox) {
73         nodeRenderer = HtmlRenderer.createRenderer();
74         rootIconImage = Utilities.loadImage(
75                             "org/netbeans/modules/search/res/find.gif", //NOI18N
76
true); //localized
77

78         setLayout(null);
79         if (!withCheckBox) {
80             checkBox = null;
81         } else {
82             checkBox = new JCheckBox JavaDoc();
83             checkBox.setBorderPaintedFlat(true);
84             
85             Color JavaDoc c = UIManager.getColor("Tree.textBackground"); //NOI18N
86
if (c == null) {
87                 //May be null on GTK L&F
88
c = Color.WHITE;
89             }
90             checkBox.setBackground(c);
91             
92             Dimension JavaDoc dim = checkBox.getPreferredSize();
93             checkBox.setPreferredSize(new Dimension JavaDoc(dim.width, dim.height - 5));
94         }
95     }
96     
97     /**
98      */

99     public Component JavaDoc getTreeCellRendererComponent(JTree JavaDoc tree,
100                                                   Object JavaDoc value,
101                                                   boolean selected,
102                                                   boolean expanded,
103                                                   boolean leaf,
104                                                   int row,
105                                                   boolean hasFocus) {
106         final boolean isRoot = (row == 0);
107         
108         String JavaDoc text;
109         boolean isHtml;
110         Image JavaDoc iconImage;
111         boolean checked;
112         
113         if (isRoot) {
114             assert value instanceof ResultTreeModel;
115             
116             final ResultTreeModel resultTreeModel = (ResultTreeModel) value;
117             text = resultTreeModel.getRootDisplayName();
118             isHtml = false;
119             iconImage = rootIconImage;
120             checked = (checkBox != null) ? resultTreeModel.isSelected()
121                                          : false;
122             
123         } else if (value.getClass() == MatchingObject.class) {
124             final MatchingObject matchingObj = (MatchingObject) value;
125             final DataObject dataObj = (DataObject) matchingObj.object;
126             final boolean valid = matchingObj.isObjectValid();
127             if (valid) {
128                 final Node node = dataObj.getNodeDelegate();
129                 text = node.getHtmlDisplayName();
130                 isHtml = (text != null);
131                 if (!isHtml) {
132                     text = node.getDisplayName();
133                 }
134                 iconImage = node.getIcon(BeanInfo.ICON_COLOR_16x16);
135             } else {
136                 text = dataObj.getName() + getDeletedObjectHtmlSuffix();
137                 isHtml = true;
138                 iconImage = getDeletedObjectIconImage();
139             }
140             checked = (checkBox != null) ? matchingObj.isSelected()
141                                          : false;
142             
143         } else {
144             assert (value instanceof Node);
145             
146             final Node node = (Node) value;
147             text = node.getHtmlDisplayName();
148             isHtml = (text != null);
149             if (!isHtml) {
150                 text = node.getDisplayName();
151             }
152             iconImage = node.getIcon(BeanInfo.ICON_COLOR_16x16);
153             if (checkBox == null) {
154                 checked = false;
155             } else {
156                 TreePath JavaDoc path = tree.getPathForRow(row);
157                 if (path == null) { //surprisingly, this happens
158
checked = true;
159                 } else {
160                     MatchingObject matchingObj
161                             = (MatchingObject) path.getPathComponent(1);
162                     if (matchingObj.isUniformSelection()) {
163                         checked = matchingObj.isSelected();
164                     } else {
165                         int parentPathRow
166                                 = tree.getRowForPath(path.getParentPath());
167                         int index = row - parentPathRow - 1;
168                         checked = matchingObj.isSubnodeSelected(index);
169                     }
170                 }
171             }
172         }
173         
174         stringDisplayer = nodeRenderer.getTreeCellRendererComponent(
175                 tree,
176                 text,
177                 selected,
178                 expanded,
179                 leaf,
180                 row,
181                 hasFocus);
182
183         nodeRenderer.setHtml(isHtml);
184         nodeRenderer.setIcon(new ImageIcon JavaDoc(iconImage));
185         
186         //HtmlRenderer does not tolerate null colors - real ones are needed to
187
//ensure fg/bg always diverge enough to be readable
188
if (stringDisplayer.getBackground() == null) {
189             stringDisplayer.setBackground(tree.getBackground());
190         }
191         if (stringDisplayer.getForeground() == null) {
192             stringDisplayer.setForeground(tree.getForeground());
193         }
194
195         if (checkBox != null) {
196             checkBox.setSelected(checked);
197             return this;
198         } else {
199             return stringDisplayer;
200         }
201     }
202     
203     @Override JavaDoc
204     public void paint(Graphics JavaDoc g) {
205         Dimension JavaDoc checkDim = checkBox.getSize();
206         Dimension JavaDoc labelDim = stringDisplayer.getPreferredSize();
207
208         int labelY = (checkDim.height >= labelDim.height)
209                      ? (checkDim.height - labelDim.height) / 2
210                      : 0;
211         checkBox.paint(g);
212
213         /*
214          * The stringDisplayer's bounds are set to (0, 0, 0, 0), although
215          * they have been set to reasonable values by doLayout().
216          * To work-around this, we translate the Graphics' origin
217          * to the desired location, paint the stringDisplayer and then
218          * return the Graphics' origin back.
219          */

220         assert stringDisplayer.getBounds().x == 0
221                && stringDisplayer.getBounds().y == 0;
222         g.translate(checkDim.width, labelY);
223         stringDisplayer.paint(g);
224         g.translate(-checkDim.width, -labelY);
225     }
226     
227     @Override JavaDoc
228     public Dimension JavaDoc getPreferredSize() {
229         stringDisplayer.setFont(getFont());
230         
231         Dimension JavaDoc prefSize = new Dimension JavaDoc(stringDisplayer.getPreferredSize());
232         Dimension JavaDoc checkDim = checkBox.getPreferredSize();
233         prefSize.width += checkDim.width;
234         prefSize.height = Math.max(prefSize.height, checkDim.height);
235         return prefSize;
236     }
237     
238     @Override JavaDoc
239     public void doLayout() {
240         Dimension JavaDoc checkDim = checkBox.getPreferredSize();
241         Dimension JavaDoc labelDim = stringDisplayer.getPreferredSize();
242         int checkWidth = checkDim.width;
243         int checkHeight = checkDim.height;
244         int labelWidth = labelDim.width;
245         int labelHeight = labelDim.height;
246         
247         int heightDif = labelHeight - checkHeight;
248         int checkY = (heightDif > 2) ? heightDif / 2 - 1
249                                      : 0;
250         int labelY = (heightDif < 0) ? -heightDif / 2
251                                      : 0;
252
253         checkBox.setBounds(0, checkY, checkWidth, checkHeight);
254         stringDisplayer.setBounds(checkWidth, labelY, labelWidth, labelHeight);
255         if (checkBounds == null) {
256             checkBounds = checkBox.getBounds();
257         }
258     }
259     
260     private Image JavaDoc getDeletedObjectIconImage() {
261         if (deletedObjectIconImage == null) {
262             deletedObjectIconImage = Utilities.loadImage(
263                     "org/netbeans/modules/search/res/invalid.png", //NOI18N
264
true); //localized
265
}
266         return deletedObjectIconImage;
267     }
268     
269     private String JavaDoc getDeletedObjectHtmlSuffix() {
270         if (deletedObjectHtmlSuffix == null) {
271             deletedObjectHtmlSuffix
272                     = "&nbsp;&nbsp;<font color=\"#ff0000\">" //NOI18N
273
+ NbBundle.getMessage(getClass(),
274                                             "LBL_InvalidFile") //NOI18N
275
+ "</font>"; //NOI18N
276
}
277         return deletedObjectHtmlSuffix;
278     }
279
280     static Rectangle JavaDoc getCheckBoxRectangle() {
281         return (Rectangle JavaDoc) checkBounds.clone();
282     }
283 }
284
Popular Tags