1 19 20 package org.netbeans.modules.java.ui; 21 22 import java.awt.Component ; 23 import javax.lang.model.element.TypeElement; 24 import javax.swing.Icon ; 25 import javax.swing.JComponent ; 26 import javax.swing.JLabel ; 27 import javax.swing.JList ; 28 import javax.swing.JTree ; 29 import javax.swing.ListCellRenderer ; 30 import javax.swing.tree.DefaultMutableTreeNode ; 31 import javax.swing.tree.TreeCellRenderer ; 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 42 public final class Renderers { 43 44 45 private Renderers() { 46 } 47 48 public static TreeCellRenderer declarationTreeRenderer() { 49 return new DeclarationTreeRenderer(); 50 } 51 52 public static ListCellRenderer declarationListRenderer() { 53 return new DeclarationTreeRenderer(); 54 } 55 56 58 private static class DeclarationTreeRenderer implements TreeCellRenderer , ListCellRenderer { 59 60 Renderer renderer; 61 62 63 private DeclarationTreeRenderer() { 64 this.renderer = HtmlRenderer.createRenderer(); 65 } 66 67 69 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 70 71 if ( value instanceof DefaultMutableTreeNode ) { 72 value = ((DefaultMutableTreeNode )value).getUserObject(); 73 } 74 75 String name; 76 String toolTip; 77 Icon 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 comp = (JLabel )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 return comp; 101 102 } 103 104 106 public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { 107 108 if ( value instanceof DefaultMutableTreeNode ) { 109 value = ((DefaultMutableTreeNode )value).getUserObject(); 110 } 111 112 String name; 113 String toolTip; 114 Icon 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 comp = (JLabel )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 138 private static String getDisplayName( TypeElement te ) { 139 boolean deprecated = false; String simpleName = te.getSimpleName().toString(); 141 String 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 getToolTipText( TypeElement value ) { 148 return value.getQualifiedName().toString(); 149 } 150 151 152 } 153 154 } 155 | Popular Tags |