1 19 20 package org.netbeans.spi.enode; 21 22 import org.netbeans.modules.enode.ScaledIcon; 23 import java.awt.Color ; 24 import java.awt.Graphics ; 25 import java.awt.Image ; 26 import java.awt.image.BufferedImage ; 27 import java.net.URL ; 28 import java.io.IOException ; 29 30 import javax.swing.ImageIcon ; 31 32 import org.openide.ErrorManager; 33 import org.openide.util.Utilities; 34 35 36 37 51 public class NbIcon extends ImageIcon { 52 55 58 public static final int SIZE_16x16 = 16; 59 60 63 public static final int SIZE_20x20 = 20; 64 65 68 public static final int SIZE_32x32 = 32; 69 70 73 private static final String DESCRIPTION = "Unknown Icon"; 77 78 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 private static ImageIcon theDefault16x16Icon; 95 private static ImageIcon theDefault20x20Icon; 96 private static ImageIcon theDefault32x32Icon; 97 98 private Image myImage = null; 102 private int mySize = 0; 103 104 107 114 private NbIcon( int size ) { 115 super( ); 116 setDescription( DESCRIPTION ); 117 118 mySize = size; 119 myImage = new BufferedImage ( mySize, mySize, BufferedImage.TYPE_INT_ARGB ); 120 121 Color fill = Color.WHITE; 122 Color border = Color.BLACK; 123 Color cross = Color.RED; 124 125 Graphics 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 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 157 168 public static ImageIcon loadIcon( URL url, int size, String description ) { 169 ImageIcon icon = null; 170 171 try { 172 icon = new ImageIcon ( url ); 173 174 175 if( icon.getIconHeight( ) != size || 176 icon.getIconWidth( ) != size ) { 177 icon = (ImageIcon )ScaledIcon.create( icon, size ); 178 } 179 } 180 catch( Exception 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 208 public static ImageIcon loadIcon( String file, int size, String description ) { 209 ImageIcon icon = null; 210 211 try { 212 if (file == null) { 213 throw new IllegalStateException ("Icon with description " + description + " cannot be loaded."); 214 } 215 Image image = Utilities.loadImage( file ); 216 if (image == null) { 217 throw new IOException ("File " + file + " cannot be found."); 218 } 219 icon = new ImageIcon ( image ); 220 221 if( icon.getIconHeight( ) != size || 222 icon.getIconWidth( ) != size ) { 223 icon = (ImageIcon )ScaledIcon.create( icon, size ); 224 } 225 } 226 catch(Exception 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 250 public static ImageIcon unknownIcon( int size ) { 251 ImageIcon 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 |