KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > view > 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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.openide.explorer.view;
20
21 import org.openide.awt.HtmlRenderer;
22 import org.openide.awt.ListPane;
23 import org.openide.nodes.Node;
24 import java.awt.Component JavaDoc;
25 import java.awt.Container JavaDoc;
26 import java.util.logging.Level JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28
29 import javax.swing.*;
30 import javax.swing.tree.TreeCellRenderer JavaDoc;
31
32
33 /** Default renderer for nodes. Can paint either Nodes directly or
34  * can be used to paint objects produced by NodeTreeModel, etc.
35  *
36  * @see org.openide.nodes.Node
37  *
38  * @author Jaroslav Tulach, Tim Boudreau
39  */

40 public class NodeRenderer extends Object JavaDoc implements TreeCellRenderer JavaDoc, ListCellRenderer {
41     private static NodeRenderer instance = null;
42
43     // ********************
44
// Support for dragging
45
// ********************
46

47     /** Value of the cell with 'drag under' visual feedback */
48     private static VisualizerNode draggedOver;
49
50     /** Flag indicating if to use big icons. */
51     private boolean bigIcons = false;
52     private HtmlRenderer.Renderer renderer = HtmlRenderer.createRenderer();
53
54     /** Creates default renderer. */
55     public NodeRenderer() {
56     }
57
58     /** Creates renderer.
59      * @param bigIcons use big icons if possible
60      * @deprecated bigIcons was only used by IconView, and not used by that
61      * anymore. NodeRenderer will automatically detect if the view it's
62      * rendering for is an instance of IconView.
63      */

64     public @Deprecated JavaDoc NodeRenderer(boolean bigIcons) {
65         this.bigIcons = bigIcons;
66     }
67
68     /** Get the singleton instance used by all explorer views.
69      *
70      * @deprecated This method no longer returns a shared instance, as it
71      * caused problems with one view setting properties (such as enabled
72      * state) on the renderer and the renderer then being used in its altered
73      * state by a different view. Views should create their own instance of
74      * NodeRenderer instead.
75      */

76     public static @Deprecated JavaDoc NodeRenderer sharedInstance() {
77         if (instance == null) {
78             instance = new NodeRenderer();
79         }
80
81         IllegalStateException JavaDoc ise = new IllegalStateException JavaDoc(
82                 "NodeRenderer." + "sharedInstance() is deprecated. Create an instance of NodeRenderer" + "instead"
83             );
84         Logger.getLogger(NodeRenderer.class.getName()).log(Level.WARNING, null, ise);
85
86         return instance;
87     }
88
89     /** Finds the component that is capable of drawing the cell in a tree.
90      * @param value value can be either <code>Node</code>
91      * or a <code>VisualizerNode</code>.
92      * @return component to draw the value
93      */

94     public Component JavaDoc getTreeCellRendererComponent(
95         JTree tree, Object JavaDoc value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus
96     ) {
97         VisualizerNode vis = findVisualizerNode(value);
98
99         if (vis == draggedOver) {
100             sel = true;
101         }
102
103         String JavaDoc text = vis.getHtmlDisplayName();
104         boolean isHtml = text != null;
105
106         if (!isHtml) {
107             text = vis.getDisplayName();
108         }
109
110         //Get our result value - really it is ren, but this call causes
111
//it to configure itself with the passed values
112
Component JavaDoc result = renderer.getTreeCellRendererComponent(tree, text, sel, expanded, leaf, row, hasFocus);
113
114         result.setEnabled(tree.isEnabled());
115         renderer.setHtml(isHtml);
116
117         //Do our additional configuration - set up the icon and possibly
118
//do some hacks to make it look focused for TreeTableView
119
configureFrom(renderer, tree, expanded, sel, vis);
120
121         return result;
122     }
123
124     /** This is the only method defined by <code>ListCellRenderer</code>. We just
125      * reconfigure the <code>Jlabel</code> each time we're called.
126      */

127     public Component JavaDoc getListCellRendererComponent(
128         JList list, Object JavaDoc value, int index, boolean sel, boolean cellHasFocus
129     ) {
130         VisualizerNode vis = findVisualizerNode(value);
131
132         if (vis == draggedOver) {
133             sel = true;
134         }
135
136         String JavaDoc text = vis.getHtmlDisplayName();
137         boolean isHtml = text != null;
138
139         if (!isHtml) {
140             text = vis.getDisplayName();
141         }
142
143         //Get our result value - really it is ren, but this call causes
144
//it to configure itself with the passed values
145
Component JavaDoc result = renderer.getListCellRendererComponent(
146                 list, text, index, sel, cellHasFocus || (value == draggedOver)
147             );
148         renderer.setHtml(isHtml);
149         result.setEnabled(list.isEnabled());
150
151         //Do our additional configuration - set up the icon and possibly
152
//do some hacks to make it look focused for TreeTableView
153
int iconWidth = configureFrom(renderer, list, false, sel, vis);
154
155         boolean bigIcons = this.bigIcons || list instanceof ListPane;
156
157         if (bigIcons) {
158             renderer.setCentered(true);
159         } else {
160             //Indent elements in a ListView/ChoiceView relative to their position
161
//in the node tree. Only does anything if you've subclassed and
162
//overridden createModel(). Does anybody do that?
163
if (list.getModel() instanceof NodeListModel && (((NodeListModel) list.getModel()).getDepth() > 1)) {
164                 int indent = iconWidth * NodeListModel.findVisualizerDepth(list.getModel(), vis);
165
166                 renderer.setIndent(indent);
167             }
168         }
169
170         return result;
171     }
172
173     /** Utility method which performs configuration which is common to all of the renderer
174      * implementations - sets the icon and focus properties on the renderer
175      * from the VisualizerNode.
176      *
177      */

178     private int configureFrom(
179         HtmlRenderer.Renderer ren, Container JavaDoc target, boolean useOpenedIcon, boolean sel, VisualizerNode vis
180     ) {
181         Icon icon = vis.getIcon(useOpenedIcon, bigIcons);
182
183         if (icon.getIconWidth() > 0) {
184             //Max annotated icon width is 24, so to have all the text and all
185
//the icons come out aligned, set the icon text gap to the difference
186
//plus a two pixel margin
187
ren.setIconTextGap(24 - icon.getIconWidth());
188         } else {
189             //If the icon width is 0, fill the space and add in
190
//the extra two pixels so the node names are aligned (btw, this
191
//does seem to waste a frightful amount of horizontal space in
192
//a tree that can use all it can get)
193
ren.setIndent(26);
194         }
195
196         ren.setIcon(icon);
197
198         if (target instanceof TreeTable.TreeTableCellRenderer) {
199             TreeTable tt = ((TreeTable.TreeTableCellRenderer) target).getTreeTable();
200             ren.setParentFocused(tt.hasFocus() || tt.isEditing());
201         }
202
203         return (icon.getIconWidth() == 0) ? 24 : icon.getIconWidth();
204     }
205
206     /** Utility method to find a visualizer node for the object passed to
207      * any of the cell renderer methods as the value */

208     private static final VisualizerNode findVisualizerNode(Object JavaDoc value) {
209         VisualizerNode vis = (value instanceof Node) ? VisualizerNode.getVisualizer(null, (Node) value)
210                                                      : (VisualizerNode) value;
211
212         if (vis == null) {
213             vis = VisualizerNode.EMPTY;
214         }
215
216         return vis;
217     }
218
219     /** DnD operation enters. Update look and feel to the 'drag under' state.
220      * @param dragged the value of cell which should have 'drag under' visual feedback
221          */

222     static void dragEnter(Object JavaDoc dragged) {
223         draggedOver = (VisualizerNode) dragged;
224     }
225
226     /** DnD operation exits. Revert to the normal look and feel. */
227     static void dragExit() {
228         draggedOver = null;
229     }
230 }
231
Popular Tags