KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > tools > ElementRenderer


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.tools;
21
22 import java.awt.*;
23 import javax.swing.*;
24 import java.lang.reflect.Modifier JavaDoc;
25
26 import org.openide.src.*;
27 import org.openide.util.Utilities;
28
29 /**
30  * ListCellRenderer capable of displaying some Java elements according to the
31  * element format
32  */

33 public class ElementRenderer extends DefaultListCellRenderer {
34     /** Icons for the displayed elements.
35      */

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") ); // NOI18N
50
elementIcons[ METHOD_BASE + PROTECTED_OFFSET ] = new ImageIcon (Utilities.loadImage ("org/openide/src/resources/methodProtected.gif") ); // NOI18N
51
elementIcons[ METHOD_BASE + PRIVATE_OFFSET ] = new ImageIcon (Utilities.loadImage ("org/openide/src/resources/methodPrivate.gif") ); // NOI18N
52
elementIcons[ METHOD_BASE + PACKAGE_OFFSET ] = new ImageIcon (Utilities.loadImage ("org/openide/src/resources/methodPackage.gif") ); // NOI18N
53

54         elementIcons[ CLASS_BASE + PUBLIC_OFFSET ] = new ImageIcon (Utilities.loadImage ("org/openide/src/resources/class.gif") ); // NOI18N
55
// No special icons for diffent access type for classes.
56
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     /** Formatting object to use with this Renderer.
62      */

63     private ElementFormat elementFormat;
64
65     /** Creates new element renderer.
66      * Note that you <b>must</b> call {@link #setFormat} before the first item is painted.
67      */

68     public ElementRenderer() {
69     }
70
71     /** Creates an renderer and sets the element format to use.
72      */

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 JavaDoc getListCellRendererComponent(final javax.swing.JList JavaDoc list,
83         final java.lang.Object JavaDoc 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 JavaDoc 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 JavaDoc(element.getClass().toString());
108         } else {
109             throw new IllegalArgumentException JavaDoc(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