KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > bluej > ui > window > HackedNodeRenderer


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 package org.netbeans.bluej.ui.window;
20
21 import java.awt.Component JavaDoc;
22 import java.awt.Container JavaDoc;
23 import java.awt.Image JavaDoc;
24 import java.beans.BeanInfo JavaDoc;
25 import javax.swing.Icon JavaDoc;
26 import javax.swing.ImageIcon JavaDoc;
27 import javax.swing.JList JavaDoc;
28 import org.openide.awt.HtmlRenderer;
29 import org.openide.explorer.view.NodeRenderer;
30 import org.openide.explorer.view.Visualizer;
31 import org.openide.nodes.Node;
32
33 /**
34  *
35  * @author mkleint
36  */

37 public class HackedNodeRenderer extends NodeRenderer {
38
39     private HtmlRenderer.Renderer renderer = HtmlRenderer.createRenderer();
40
41     /** Creates a new instance of HackedNodeRenderer */
42     public HackedNodeRenderer() {
43     }
44     
45     /** This is the only method defined by <code>ListCellRenderer</code>. We just
46      * reconfigure the <code>Jlabel</code> each time we're called.
47      */

48     public Component JavaDoc getListCellRendererComponent(
49         JList JavaDoc list, Object JavaDoc value, int index, boolean sel, boolean cellHasFocus
50     ) {
51         Node vis = findVisualizerNode(value);
52
53         String JavaDoc text = vis.getHtmlDisplayName();
54         boolean isHtml = text != null;
55
56         if (!isHtml) {
57             text = vis.getDisplayName();
58         }
59
60         //Get our result value - really it is ren, but this call causes
61
//it to configure itself with the passed values
62
Component JavaDoc result = renderer.getListCellRendererComponent(
63                 list, text, index, sel, cellHasFocus
64             );
65         renderer.setHtml(isHtml);
66         result.setEnabled(list.isEnabled());
67
68         //Do our additional configuration - set up the icon and possibly
69
//do some hacks to make it look focused for TreeTableView
70
configureFrom(renderer, list, false, sel, vis);
71 //
72
// //Indent elements in a ListView/ChoiceView relative to their position
73
// //in the node tree. Only does anything if you've subclassed and
74
// //overridden createModel(). Does anybody do that?
75
// if (list.getModel() instanceof NodeListModel && (((NodeListModel) list.getModel()).getDepth() > 1)) {
76
// int indent = iconWidth * NodeListModel.findVisualizerDepth(list.getModel(), vis);
77
//
78
// renderer.setIndent(indent);
79
// }
80

81         return result;
82     }
83     
84     /** Utility method which performs configuration which is common to all of the renderer
85      * implementations - sets the icon and focus properties on the renderer
86      * from the VisualizerNode.
87      *
88      */

89     private int configureFrom(
90         HtmlRenderer.Renderer ren, Container JavaDoc target, boolean useOpenedIcon, boolean sel, Node vis
91     ) { //NOPMD
92
int iconType = BeanInfo.ICON_COLOR_16x16;//large ? BeanInfo.ICON_COLOR_32x32 : BeanInfo.ICON_COLOR_16x16;
93

94         Image JavaDoc image = useOpenedIcon ? vis.getOpenedIcon(iconType) : vis.getIcon(iconType);
95         Icon JavaDoc icon = new ImageIcon JavaDoc(image);
96         if (icon.getIconWidth() == 55) {
97             ren.setIconTextGap(4);
98         } else if (icon.getIconWidth() > 0) {
99             //Max annotated icon width is 24, so to have all the text and all
100
//the icons come out aligned, set the icon text gap to the difference
101
//plus a two pixel margin
102
ren.setIconTextGap(55 - icon.getIconWidth() + 4);
103         } else {
104             //If the icon width is 0, fill the space and add in
105
//the extra two pixels so the node names are aligned (btw, this
106
//does seem to waste a frightful amount of horizontal space in
107
//a tree that can use all it can get)
108
ren.setIndent(55 + 4);
109         }
110
111         ren.setIcon(icon);
112
113         return (icon.getIconWidth() == 0) ? 55 : icon.getIconWidth();
114     }
115
116     /** Utility method to find a visualizer node for the object passed to
117      * any of the cell renderer methods as the value */

118     private static final Node findVisualizerNode(Object JavaDoc value) {
119         return Visualizer.findNode(value);
120     }
121     
122 }
123
Popular Tags