KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > enode > NbIcon


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 Nokia. Portions Copyright 2003-2004 Nokia.
17  * All Rights Reserved.
18  */

19
20 package org.netbeans.spi.enode;
21
22 import org.netbeans.modules.enode.ScaledIcon;
23 import java.awt.Color JavaDoc;
24 import java.awt.Graphics JavaDoc;
25 import java.awt.Image JavaDoc;
26 import java.awt.image.BufferedImage JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.io.IOException JavaDoc;
29
30 import javax.swing.ImageIcon JavaDoc;
31
32 import org.openide.ErrorManager;
33 import org.openide.util.Utilities;
34
35
36
37 /**
38  * This class allows to load icons without raising any exceptions. When the
39  * requested icon cannot be loaded, a default icon is rendered in memory and
40  * returned instead. The user will detect the (ugly) default icon, so that
41  * no further notifications are needed. Any exceptions that occur when loading
42  * an icon fails are logged.
43  * <p>
44  * In case a loaded icon does not match the requested size, the icon will be
45  * scaled to proper size automatically.
46  *
47  * @author John Stuwe
48  *
49  * @see #ScaledIcon
50  */

51 public class NbIcon extends ImageIcon JavaDoc {
52     //=======================================================================
53
// Public constants
54

55     /**
56      * Constant for the default icons size 16x16 pixels.
57      */

58     public static final int SIZE_16x16 = 16;
59     
60     /**
61      * Constant for the default icons size 20x20 pixels.
62      */

63     public static final int SIZE_20x20 = 20;
64     
65     /**
66      * Constant for the default icons size 32x32 pixels.
67      */

68     public static final int SIZE_32x32 = 32;
69     
70     //=======================================================================
71
// Private data members
72

73     //
74
// Default description for unknown icon
75
//
76
private static final String JavaDoc DESCRIPTION = "Unknown Icon";
77     
78     //
79
// Constants needed for drawing the icon.
80
//
81
private static final int PREFERRED_SIZE = 16;
82     private static final int RECT_X = 4;
83     private static final int RECT_Y = 4;
84     private static final int RECT_WIDTH = 8;
85     private static final int RECT_HEIGHT = 8;
86     private static final int CROSS_X = 6;
87     private static final int CROSS_Y = 6;
88     private static final int CROSS_WIDTH = 4;
89     private static final int CROSS_HEIGHT = 4;
90     
91     //
92
// The defaukt icon sizes get cached to safe memory...
93
//
94
private static ImageIcon JavaDoc theDefault16x16Icon;
95     private static ImageIcon JavaDoc theDefault20x20Icon;
96     private static ImageIcon JavaDoc theDefault32x32Icon;
97     
98     //
99
// Width and height of the icon.
100
//
101
private Image JavaDoc myImage = null;
102     private int mySize = 0;
103     
104     //=======================================================================
105
// Private constructor
106

107     /**
108      * Creates a rectangular icon with the given size.
109      *
110      * @param size The width and height in pixels.
111      * @param enabled Boolean flag that indicates if an enabled or disabled
112      * icon shall be drawn.
113      */

114     private NbIcon( int size ) {
115         super( );
116         setDescription( DESCRIPTION );
117         
118         mySize = size;
119         myImage = new BufferedImage JavaDoc( mySize, mySize, BufferedImage.TYPE_INT_ARGB );
120         
121         Color JavaDoc fill = Color.WHITE;
122         Color JavaDoc border = Color.BLACK;
123         Color JavaDoc cross = Color.RED;
124         
125         Graphics JavaDoc g = myImage.getGraphics( );
126         g.setColor( fill );
127         g.fillRect( 0, 0, size - 1, size - 1 );
128         g.setColor( border );
129         g.drawRect( 0, 0, size - 1, size - 1 );
130         
131         //
132
// Add 'button' with red cross in the upper
133
// left corner.
134
//
135
if( size > PREFERRED_SIZE ) {
136             g.draw3DRect( RECT_X, RECT_Y, RECT_WIDTH, RECT_HEIGHT, true );
137             g.setColor( cross );
138             g.drawLine( CROSS_X, CROSS_Y, CROSS_X + CROSS_WIDTH,
139             CROSS_Y + CROSS_HEIGHT );
140             g.drawLine( CROSS_X, CROSS_Y + CROSS_HEIGHT, CROSS_X + CROSS_WIDTH,
141             CROSS_Y );
142         }
143         else {
144             g.setColor( cross );
145             g.drawLine( RECT_X, RECT_Y, size - RECT_X - 1, size - RECT_Y - 1 );
146             g.drawLine( size - RECT_X - 1, RECT_Y, RECT_X, size - RECT_Y - 1 );
147         }
148         
149         g.dispose( );
150         
151         setImage( myImage );
152     }
153     
154     //=======================================================================
155
// Public methods
156

157     /**
158      * Factory method that loads an icon from the given {@link URL}. If the
159      * icon cannot be loaded, a default icon with the given size is returned.
160      * In case the with or height of the loaded icon does not match the
161      * given size the icon will be scaled to fit the requested size.
162      *
163      * @param url The URL from which the icon shall be loaded.
164      * @param size The required width and height of the icon.
165      * @param description The description of the icon. Can be used to
166      * identify the icon later.
167      */

168     public static ImageIcon JavaDoc loadIcon( URL JavaDoc url, int size, String JavaDoc description ) {
169         ImageIcon JavaDoc icon = null;
170         
171         try {
172             icon = new ImageIcon JavaDoc( url );
173             
174             
175             if( icon.getIconHeight( ) != size ||
176             icon.getIconWidth( ) != size ) {
177                 icon = (ImageIcon JavaDoc)ScaledIcon.create( icon, size );
178             }
179         }
180         catch( Exception JavaDoc e ) {
181             ErrorManager manager = ErrorManager.getDefault( ).getInstance( "org.netbeans.modules.enode" );
182             manager.notify( ErrorManager.INFORMATIONAL, e );
183         }
184         
185         if( icon == null ) {
186             icon = unknownIcon( size );
187         }
188         
189         if( description != null ) {
190             icon.setDescription( description );
191         }
192         
193         return icon;
194     }
195     
196     /**
197      * Factory method that loads an icon from the JAR file path (across
198      * module boundaries). If the icon cannot be loaded, a default icon with
199      * the given size is returned. In case the with or height of the loaded
200      * icon does not match the given size the icon will be scaled to fit the
201      * requested size.
202      *
203      * @param file The JAR path from which the icon shall be loaded.
204      * @param size The required width and height of the icon.
205      * @param description The description of the icon. Can be used to
206      * identify the icon later.
207      */

208     public static ImageIcon JavaDoc loadIcon( String JavaDoc file, int size, String JavaDoc description ) {
209         ImageIcon JavaDoc icon = null;
210         
211         try {
212             if (file == null) {
213                 throw new IllegalStateException JavaDoc("Icon with description " + description + " cannot be loaded.");
214             }
215             Image JavaDoc image = Utilities.loadImage( file );
216             if (image == null) {
217                 throw new IOException JavaDoc("File " + file + " cannot be found.");
218             }
219             icon = new ImageIcon JavaDoc( image );
220             
221             if( icon.getIconHeight( ) != size ||
222             icon.getIconWidth( ) != size ) {
223                 icon = (ImageIcon JavaDoc)ScaledIcon.create( icon, size );
224             }
225         }
226         catch(Exception JavaDoc e) {
227             ErrorManager manager = ErrorManager.getDefault( ).getInstance( "org.netbeans.modules.enode" );
228             manager.notify( ErrorManager.INFORMATIONAL, e );
229         }
230         
231         if( icon == null ) {
232             icon = unknownIcon( size );
233         }
234         
235         if( description != null ) {
236             icon.setDescription( description );
237         }
238         
239         return icon;
240     }
241     
242     /**
243      * Creates a default icon with the given size as width and height.
244      * The icon is rendered in a {@link Graphics} context of a {@link
245      * BufferedImage} so that no resources have to be loaded (i.e. it
246      * should always work).
247      *
248      * @param size The width and height of the icon.
249      */

250     public static ImageIcon JavaDoc unknownIcon( int size ) {
251         ImageIcon JavaDoc icon = null;
252         
253         switch( size ) {
254             case SIZE_16x16:
255                 
256                 if( theDefault16x16Icon == null ) {
257                     theDefault16x16Icon = new NbIcon( SIZE_16x16 );
258                 }
259                 
260                 icon = theDefault16x16Icon;
261                 
262                 break;
263                 
264             case SIZE_20x20:
265                 
266                 if( theDefault20x20Icon == null ) {
267                     theDefault20x20Icon = new NbIcon( SIZE_20x20 );
268                 }
269                 
270                 icon = theDefault20x20Icon;
271                 
272                 break;
273                 
274             case SIZE_32x32:
275                 
276                 if( theDefault32x32Icon == null ) {
277                     theDefault32x32Icon = new NbIcon( SIZE_32x32 );
278                 }
279                 
280                 icon = theDefault32x32Icon;
281                 
282                 break;
283                 
284             default:
285                 icon = new NbIcon( size );
286         }
287         
288         return icon;
289     }
290 }
291
292
Popular Tags