KickJava   Java API By Example, From Geeks To Geeks.

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


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.Image JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import javax.lang.model.element.ElementKind;
26 import javax.lang.model.element.Modifier;
27 import javax.swing.Icon JavaDoc;
28 import javax.swing.ImageIcon JavaDoc;
29 import org.openide.util.Utilities;
30
31 /**
32  *
33  * @author Petr Hrebejk
34  */

35 public final class Icons {
36
37     private static final String JavaDoc ICON_BASE = "org/netbeans/modules/java/source/resources/icons/";
38     private static final String JavaDoc GIF_EXTENSION = ".gif";
39     private static final String JavaDoc PNG_EXTENSION = ".png";
40     private static final String JavaDoc WAIT = ICON_BASE + "wait" + PNG_EXTENSION;
41         
42     /** Creates a new instance of Icons */
43     private Icons() {
44     }
45     
46     public static Icon JavaDoc getBusyIcon () {
47         Image JavaDoc img = Utilities.loadImage (WAIT);
48         if (img == null) {
49             return null;
50         }
51         else {
52             return new ImageIcon JavaDoc (img);
53         }
54     }
55             
56     
57     public static Icon JavaDoc getElementIcon( ElementKind elementKind, Collection JavaDoc<Modifier> modifiers ) {
58         
59         if ( modifiers == null ) {
60             modifiers = Collections.<Modifier>emptyList();
61         }
62         
63         Image JavaDoc img = null;
64     
65     switch( elementKind ) {
66         case PACKAGE:
67         img = Utilities.loadImage( ICON_BASE + "package" + GIF_EXTENSION );
68         break;
69         case ENUM:
70         img = Utilities.loadImage( ICON_BASE + "enum" + PNG_EXTENSION );
71         break;
72         case ANNOTATION_TYPE:
73         img = Utilities.loadImage( ICON_BASE + "annotation" + PNG_EXTENSION );
74         break;
75         case CLASS:
76         img = Utilities.loadImage( ICON_BASE + "class" + PNG_EXTENSION );
77         break;
78         case INTERFACE:
79         img = Utilities.loadImage( ICON_BASE + "interface" + PNG_EXTENSION );
80         break;
81         case FIELD:
82         img = Utilities.loadImage( getIconName( ICON_BASE + "field", PNG_EXTENSION, modifiers ) );
83         break;
84         case ENUM_CONSTANT:
85         img = Utilities.loadImage( ICON_BASE + "constant" + PNG_EXTENSION );
86         break;
87         case CONSTRUCTOR:
88         img = Utilities.loadImage( getIconName( ICON_BASE + "constructor", PNG_EXTENSION, modifiers ) );
89         break;
90         case STATIC_INIT:
91         img = Utilities.loadImage( getIconName( ICON_BASE + "initializer", PNG_EXTENSION, modifiers ) );
92         break;
93         case METHOD:
94         img = Utilities.loadImage( getIconName( ICON_BASE + "method", PNG_EXTENSION, modifiers ) );
95         break;
96         default:
97             img = null;
98         }
99     
100     return img == null ? null : new ImageIcon JavaDoc (img);
101         
102     }
103     
104     // Private Methods ---------------------------------------------------------
105

106     private static String JavaDoc getIconName( String JavaDoc typeName, String JavaDoc extension, Collection JavaDoc<Modifier> modifiers ) {
107         
108         StringBuffer JavaDoc fileName = new StringBuffer JavaDoc( typeName );
109         
110         if ( modifiers.contains( Modifier.STATIC ) ) {
111             fileName.append( "Static" );
112         }
113         if ( modifiers.contains( Modifier.PUBLIC ) ) {
114             return fileName.append( "Public" ).append( extension ).toString();
115         }
116         if ( modifiers.contains( Modifier.PROTECTED ) ) {
117             return fileName.append( "Protected" ).append( extension ).toString();
118         }
119         if ( modifiers.contains( Modifier.PRIVATE ) ) {
120             return fileName.append( "Private" ).append( extension ).toString();
121         }
122         return fileName.append( "Package" ).append( extension ).toString();
123                         
124     }
125     
126 }
127
Popular Tags