KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > ui > Renderers


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.modules.java.ui;
21
22 import java.awt.Component JavaDoc;
23 import javax.lang.model.element.TypeElement;
24 import javax.swing.Icon JavaDoc;
25 import javax.swing.JComponent JavaDoc;
26 import javax.swing.JLabel JavaDoc;
27 import javax.swing.JList JavaDoc;
28 import javax.swing.JTree JavaDoc;
29 import javax.swing.ListCellRenderer JavaDoc;
30 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
31 import javax.swing.tree.TreeCellRenderer JavaDoc;
32 import org.netbeans.api.java.source.SourceUtils;
33 import org.openide.awt.HtmlRenderer;
34 import org.openide.awt.HtmlRenderer.Renderer;
35 import org.openide.filesystems.FileSystem.HtmlStatus;
36 import org.netbeans.api.java.source.UiUtils;
37
38 /** Contains implementation of various CellRenderers.
39  *
40  * @author Petr Hrebejk
41  */

42 public final class Renderers {
43     
44     /** Creates a new instance of Renderers */
45     private Renderers() {
46     }
47     
48     public static TreeCellRenderer JavaDoc declarationTreeRenderer() {
49         return new DeclarationTreeRenderer();
50     }
51     
52     public static ListCellRenderer JavaDoc declarationListRenderer() {
53         return new DeclarationTreeRenderer();
54     }
55         
56     // Innerclasses ------------------------------------------------------------
57

58     private static class DeclarationTreeRenderer implements TreeCellRenderer JavaDoc, ListCellRenderer JavaDoc {
59         
60         Renderer JavaDoc renderer;
61         
62         /** Creates a new instance of ClassesRenderer */
63         private DeclarationTreeRenderer() {
64             this.renderer = HtmlRenderer.createRenderer();
65         }
66         
67         // ListCellRenderer implementation -------------------------------------
68

69         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
70             
71             if ( value instanceof DefaultMutableTreeNode JavaDoc ) {
72                 value = ((DefaultMutableTreeNode JavaDoc)value).getUserObject();
73             }
74             
75             String JavaDoc name;
76             String JavaDoc toolTip;
77             Icon JavaDoc icon;
78             if (value instanceof TypeElement) {
79                 TypeElement te = (TypeElement) value;
80                 name = getDisplayName(te);
81                 toolTip = getToolTipText(te);
82                 icon = UiUtils.getElementIcon( te.getKind(), te.getModifiers() );
83             }
84             else {
85                 name = "??";
86                 toolTip = name = (value == null ? "NULL" : value.toString());
87                 icon = null;
88             }
89             
90             JLabel JavaDoc comp = (JLabel JavaDoc)renderer.getListCellRendererComponent( list, value, index, isSelected, cellHasFocus );
91             comp.setText( name );
92             comp.setToolTipText(toolTip);
93             if (icon != null) {
94                 comp.setIcon(icon);
95             }
96 // if ( index % 2 > 0 ) {
97
// comp.setBackground( list.getBackground().darker() ); // Too dark
98
// comp.setOpaque( true );
99
// }
100
return comp;
101             
102         }
103         
104         // TreeCellRenderer implementation -------------------------------------
105

106         public Component JavaDoc getTreeCellRendererComponent(JTree JavaDoc tree, Object JavaDoc value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
107             
108             if ( value instanceof DefaultMutableTreeNode JavaDoc ) {
109                 value = ((DefaultMutableTreeNode JavaDoc)value).getUserObject();
110             }
111             
112             String JavaDoc name;
113             String JavaDoc toolTip;
114             Icon JavaDoc icon;
115             if (value instanceof TypeElement) {
116                 TypeElement te = (TypeElement) value;
117                 name = getDisplayName(te);
118                 toolTip = getToolTipText(te);
119                 icon = UiUtils.getElementIcon( te.getKind(), te.getModifiers() );
120             }
121             else {
122                 name = "???";
123                 toolTip = name = (value == null ? "NULL" : value.toString());
124                 icon = null;
125             }
126             
127             JLabel JavaDoc comp = (JLabel JavaDoc)renderer.getTreeCellRendererComponent( tree, value, selected, expanded, leaf, row, hasFocus );
128             comp.setText( name );
129             comp.setToolTipText(toolTip);
130             if (icon != null) {
131                 comp.setIcon(icon);
132             }
133             return comp;
134         }
135         
136         // Private methods -----------------------------------------------------
137

138         private static String JavaDoc getDisplayName( TypeElement te ) {
139             boolean deprecated = false;//XXX: removing isDeprecated from SourceUtils, use Elements.isDeprecated instead
140
String JavaDoc simpleName = te.getSimpleName().toString();
141             String JavaDoc qualifiedName = te.getQualifiedName().toString();
142             int lastIndex = qualifiedName.length() - simpleName.length();
143             lastIndex = lastIndex == 0 ? lastIndex : lastIndex - 1;
144             return "<html><b>" + (deprecated ? "<s>" : "" ) + simpleName + (deprecated ? "</s></b>" : "</b>" ) + "<font color=\"#707070\"> (" + qualifiedName.substring( 0, lastIndex ) + ")</font></html>";
145         }
146
147         private static String JavaDoc getToolTipText( TypeElement value ) {
148             return value.getQualifiedName().toString();
149         }
150
151                
152     }
153             
154 }
155
Popular Tags