1 19 20 package org.netbeans.modules.java.tools; 21 22 import java.awt.*; 23 import javax.swing.*; 24 import java.lang.reflect.Modifier ; 25 26 import org.openide.src.*; 27 import org.openide.util.Utilities; 28 29 33 public class ElementRenderer extends DefaultListCellRenderer { 34 36 private static ImageIcon elementIcons[]; 37 38 private static final int PUBLIC_OFFSET = 0; 39 private static final int PROTECTED_OFFSET = 1; 40 private static final int PRIVATE_OFFSET = 2; 41 private static final int PACKAGE_OFFSET = 3; 42 43 private static final int METHOD_BASE = 0; 44 private static final int CLASS_BASE = 4; 45 46 static { 47 elementIcons = new ImageIcon[8]; 48 49 elementIcons[ METHOD_BASE + PUBLIC_OFFSET ] = new ImageIcon (Utilities.loadImage ("org/openide/src/resources/methodPublic.gif") ); elementIcons[ METHOD_BASE + PROTECTED_OFFSET ] = new ImageIcon (Utilities.loadImage ("org/openide/src/resources/methodProtected.gif") ); elementIcons[ METHOD_BASE + PRIVATE_OFFSET ] = new ImageIcon (Utilities.loadImage ("org/openide/src/resources/methodPrivate.gif") ); elementIcons[ METHOD_BASE + PACKAGE_OFFSET ] = new ImageIcon (Utilities.loadImage ("org/openide/src/resources/methodPackage.gif") ); 54 elementIcons[ CLASS_BASE + PUBLIC_OFFSET ] = new ImageIcon (Utilities.loadImage ("org/openide/src/resources/class.gif") ); elementIcons[ CLASS_BASE + PROTECTED_OFFSET ] = elementIcons[ CLASS_BASE + PUBLIC_OFFSET ]; 57 elementIcons[ CLASS_BASE + PRIVATE_OFFSET ] = elementIcons[ CLASS_BASE + PUBLIC_OFFSET ]; 58 elementIcons[ CLASS_BASE + PACKAGE_OFFSET ] = elementIcons[ CLASS_BASE + PUBLIC_OFFSET ]; 59 } 60 61 63 private ElementFormat elementFormat; 64 65 68 public ElementRenderer() { 69 } 70 71 73 public ElementRenderer(ElementFormat fmt) { 74 this(); 75 setFormat(fmt); 76 } 77 78 public void setFormat(ElementFormat fmt) { 79 this.elementFormat = fmt; 80 } 81 82 public java.awt.Component getListCellRendererComponent(final javax.swing.JList list, 83 final java.lang.Object value,int index,boolean selected,boolean focused) { 84 Component c = super.getListCellRendererComponent(list, null, index, selected, focused); 85 86 if (!(c instanceof JLabel)) 87 return c; 88 JLabel label = (JLabel)c; 89 label.setIcon(getIcon(value)); 90 label.setText(elementFormat.format((MethodElement)value)); 91 return c; 92 } 93 94 protected Icon getIcon(Object element) { 95 int modifiers; 96 int base; 97 Element el; 98 99 if (element instanceof MemberElement) { 100 modifiers = ((MemberElement)element).getModifiers(); 101 el = (MemberElement)element; 102 if (el instanceof ClassElement) { 103 base = CLASS_BASE; 104 } else if (el instanceof MethodElement) { 105 base = METHOD_BASE; 106 } else 107 throw new IllegalArgumentException (element.getClass().toString()); 108 } else { 109 throw new IllegalArgumentException (element.getClass().toString()); 110 } 111 112 int offset; 113 114 if (Modifier.isPublic(modifiers)) 115 offset = PUBLIC_OFFSET; 116 else if (Modifier.isProtected(modifiers)) 117 offset = PROTECTED_OFFSET; 118 else if (Modifier.isPrivate(modifiers)) 119 offset = PRIVATE_OFFSET; 120 else 121 offset = PACKAGE_OFFSET; 122 123 return elementIcons[base + offset]; 124 } 125 } 126 | Popular Tags |