001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.java.ui;
043:
044: import java.awt.Component;
045: import javax.lang.model.element.TypeElement;
046: import javax.swing.Icon;
047: import javax.swing.JComponent;
048: import javax.swing.JLabel;
049: import javax.swing.JList;
050: import javax.swing.JTree;
051: import javax.swing.ListCellRenderer;
052: import javax.swing.tree.DefaultMutableTreeNode;
053: import javax.swing.tree.TreeCellRenderer;
054: import org.netbeans.api.java.source.SourceUtils;
055: import org.openide.awt.HtmlRenderer;
056: import org.openide.awt.HtmlRenderer.Renderer;
057: import org.openide.filesystems.FileSystem.HtmlStatus;
058: import org.netbeans.api.java.source.UiUtils;
059:
060: /** Contains implementation of various CellRenderers.
061: *
062: * @author Petr Hrebejk
063: */
064: public final class Renderers {
065:
066: /** Creates a new instance of Renderers */
067: private Renderers() {
068: }
069:
070: public static TreeCellRenderer declarationTreeRenderer() {
071: return new DeclarationTreeRenderer();
072: }
073:
074: public static ListCellRenderer declarationListRenderer() {
075: return new DeclarationTreeRenderer();
076: }
077:
078: // Innerclasses ------------------------------------------------------------
079:
080: private static class DeclarationTreeRenderer implements
081: TreeCellRenderer, ListCellRenderer {
082:
083: Renderer renderer;
084:
085: /** Creates a new instance of ClassesRenderer */
086: private DeclarationTreeRenderer() {
087: this .renderer = HtmlRenderer.createRenderer();
088: }
089:
090: // ListCellRenderer implementation -------------------------------------
091:
092: public Component getListCellRendererComponent(JList list,
093: Object value, int index, boolean isSelected,
094: boolean cellHasFocus) {
095:
096: if (value instanceof DefaultMutableTreeNode) {
097: value = ((DefaultMutableTreeNode) value)
098: .getUserObject();
099: }
100:
101: String name;
102: String toolTip;
103: Icon icon;
104: if (value instanceof TypeElement) {
105: TypeElement te = (TypeElement) value;
106: name = getDisplayName(te);
107: toolTip = getToolTipText(te);
108: icon = UiUtils.getElementIcon(te.getKind(), te
109: .getModifiers());
110: } else {
111: name = "??";
112: toolTip = name = (value == null ? "NULL" : value
113: .toString());
114: icon = null;
115: }
116:
117: JLabel comp = (JLabel) renderer
118: .getListCellRendererComponent(list, value, index,
119: isSelected, cellHasFocus);
120: comp.setText(name);
121: comp.setToolTipText(toolTip);
122: if (icon != null) {
123: comp.setIcon(icon);
124: }
125: // if ( index % 2 > 0 ) {
126: // comp.setBackground( list.getBackground().darker() ); // Too dark
127: // comp.setOpaque( true );
128: // }
129: return comp;
130:
131: }
132:
133: // TreeCellRenderer implementation -------------------------------------
134:
135: public Component getTreeCellRendererComponent(JTree tree,
136: Object value, boolean selected, boolean expanded,
137: boolean leaf, int row, boolean hasFocus) {
138:
139: if (value instanceof DefaultMutableTreeNode) {
140: value = ((DefaultMutableTreeNode) value)
141: .getUserObject();
142: }
143:
144: String name;
145: String toolTip;
146: Icon icon;
147: if (value instanceof TypeElement) {
148: TypeElement te = (TypeElement) value;
149: name = getDisplayName(te);
150: toolTip = getToolTipText(te);
151: icon = UiUtils.getElementIcon(te.getKind(), te
152: .getModifiers());
153: } else {
154: name = "???";
155: toolTip = name = (value == null ? "NULL" : value
156: .toString());
157: icon = null;
158: }
159:
160: JLabel comp = (JLabel) renderer
161: .getTreeCellRendererComponent(tree, value,
162: selected, expanded, leaf, row, hasFocus);
163: comp.setText(name);
164: comp.setToolTipText(toolTip);
165: if (icon != null) {
166: comp.setIcon(icon);
167: }
168: return comp;
169: }
170:
171: // Private methods -----------------------------------------------------
172:
173: private static String getDisplayName(TypeElement te) {
174: boolean deprecated = false;//XXX: removing isDeprecated from SourceUtils, use Elements.isDeprecated instead
175: String simpleName = te.getSimpleName().toString();
176: String qualifiedName = te.getQualifiedName().toString();
177: int lastIndex = qualifiedName.length()
178: - simpleName.length();
179: lastIndex = lastIndex == 0 ? lastIndex : lastIndex - 1;
180: return "<html><b>" + (deprecated ? "<s>" : "") + simpleName
181: + (deprecated ? "</s></b>" : "</b>")
182: + "<font color=\"#707070\"> ("
183: + qualifiedName.substring(0, lastIndex)
184: + ")</font></html>";
185: }
186:
187: private static String getToolTipText(TypeElement value) {
188: return value.getQualifiedName().toString();
189: }
190:
191: }
192:
193: }
|