1 19 20 package org.netbeans.spi.enode; 21 22 import java.util.Enumeration ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 import java.util.MissingResourceException ; 27 import java.util.ArrayList ; 28 import java.util.logging.Logger ; 29 30 import javax.swing.ImageIcon ; 31 32 import org.openide.ErrorManager; 33 34 import org.openide.filesystems.FileObject; 35 36 import org.openide.util.NbBundle; 37 38 import org.netbeans.modules.enode.TimedSoftReference; 39 40 41 120 public class IconSet { 121 124 private static final char DEFAULT_MARK = '*'; 129 private static final char SIZE_MARK = '#'; 130 131 private static final String DESCRIPTION_ATTRIBUTE = "description"; 135 private static final String BASE_DIR_ATTRIBUTE = "basedir"; 136 private static final String DEFAULT_SIZE_ATTRIBUTE = "defaultSize"; 137 private static final String BUNDLE_ATTRIBUTE = "bundle"; 138 private static final String INSTANCE_CLASS = "instanceClass"; 139 private static final String INSTANCE_CREATE = "instanceCreate"; 140 141 private static final String PROPERTIES = ".properties"; 145 146 private static Map theIconSetCache; 150 151 private Map myIcons; 159 private IconSet myDelegate; 160 private int myDefaultSize; 161 private String myBundle; 162 private String myDescription; 163 164 167 170 public IconSet( ) { 171 myIcons = new HashMap ( ); 172 myDefaultSize = NbIcon.SIZE_16x16; 173 } 174 175 176 182 IconSet( FileObject file ) { 183 this( ); 184 185 try { 186 if( file == null ) { 187 throw new IllegalArgumentException ( ); 188 } 189 190 String baseDir = (String )file.getAttribute( BASE_DIR_ATTRIBUTE ); 191 if( baseDir != null ) { 192 baseDir = parseBaseDirAttribute( baseDir ); 193 } 194 195 Enumeration attributes = file.getAttributes( ); 196 197 while( attributes.hasMoreElements( ) ) { 198 String attr = (String )attributes.nextElement( ); 199 200 if( !attr.equals( INSTANCE_CLASS ) && 201 !attr.equals( INSTANCE_CREATE ) && 202 !attr.equals( BASE_DIR_ATTRIBUTE ) ) { 203 String value = (String )file.getAttribute( attr ); 204 parseAttribute( attr, value, baseDir ); 205 } 206 } 207 } 208 catch( Exception e ) { 209 ErrorManager manager = 210 ErrorManager.getDefault( ).getInstance( "org.netbeans.spi.enode" ); 211 manager.notify( ErrorManager.INFORMATIONAL, e ); 212 } 213 } 214 215 218 224 public static IconSet getInstance( FileObject file ) { 225 if( theIconSetCache == null ) { 226 theIconSetCache = new HashMap ( ); 227 } 228 229 Object key = file.getPath( ); 230 TimedSoftReference ref = null; 231 synchronized (theIconSetCache) { 232 ref = (TimedSoftReference)theIconSetCache.get(key); 233 } 234 IconSet instance = null; 235 if (ref != null) { 236 instance = (IconSet)ref.get(); 237 } 238 if (instance == null) { 239 instance = new IconSet( file ); 240 synchronized (theIconSetCache) { 241 theIconSetCache.put(key, new TimedSoftReference(instance, theIconSetCache, key)); 242 } 243 } 244 245 return instance; 246 } 247 248 249 258 public void setDelegate( IconSet set ) { 259 if( myDelegate != null ) { 260 throw new IllegalStateException (); 261 } 262 263 myDelegate = set; 264 } 265 266 274 public IconSet getDelegate() { 275 return myDelegate; 276 } 277 278 279 284 public int getDefaultSize( ) { 285 return myDefaultSize; 286 } 287 288 289 298 public ImageIcon getIcon(String name, int size) { 299 ImageIcon icon = null; 300 String file = null; 301 302 if (name == null) { 306 String key = "" + DEFAULT_MARK + size; 307 name = (String ) myIcons.get(key); 308 309 if (name == null) { 310 if (myDelegate != null) { 311 return myDelegate.getIcon(name,size) ; 312 } else { 313 ErrorManager.getDefault().getInstance("org.netbeans.spi.enode").notify( 314 ErrorManager.INFORMATIONAL, 315 new IllegalStateException ( 316 "Icon with the size " + size + " does not exist") 317 ); 318 } 319 } 320 321 file = (String )myIcons.get(makeKey(name, size)); 322 323 } else { 324 file = (String )myIcons.get(makeKey(name, size)); 325 326 if( file == null ) { 330 return getIcon(null, size); 331 } 332 } 333 334 if (file == null && myDelegate != null ) { 335 icon = myDelegate.getIcon(name, size); 336 } else { 337 Logger logger = Logger.getLogger("org.netbeans.spi.enode"); 338 logger.finest( "Loading MO symbol from file " + file ); 339 340 if (file == null) { 341 ErrorManager.getDefault().getInstance("org.netbeans.spi.enode").notify( 342 ErrorManager.INFORMATIONAL, 343 new IllegalStateException ("File cannot be computed for name " + name)); 344 } 345 346 icon = NbIcon.loadIcon( file, size, name ); 347 } 348 349 return icon; 350 } 351 352 353 359 public ImageIcon getDefaultIcon( int size ) { 360 return getIcon( null, size ); 361 } 362 363 364 372 public ImageIcon getDefaultIcon( ) { 373 return getIcon( null, getDefaultSize( ) ); 374 } 375 376 381 public String getDescription( ) { 382 return myDescription; 383 } 384 385 395 public String getIconDisplayName( String name ) { 396 String display = null; 397 398 try { 399 display = NbBundle.getBundle( myBundle ).getString( name ); 400 } 401 catch( MissingResourceException e ) { 402 ErrorManager manager = 403 ErrorManager.getDefault( ).getInstance( "org.netbeans.spi.enode" ); 404 manager.notify( ErrorManager.INFORMATIONAL, e ); 405 406 display = name; 407 } 408 409 return display; 410 } 411 412 413 421 public String [] getAllIconNames( int size ) { 422 ArrayList list = new ArrayList ( ); 423 Iterator iterator = myIcons.keySet( ).iterator( ); 424 425 while( iterator.hasNext( ) ) { 426 String key = (String )iterator.next( ); 427 428 if( !isDefaultKey( key ) ) { 429 int iconSize = parseSize( key, getDefaultSize( ) ); 430 431 if( iconSize == size ) { 432 list.add( parseName( key ) ); 433 } 434 } 435 } 436 437 String [] icons = new String [list.size( )]; 438 439 return (String [])list.toArray( icons ); 440 } 441 442 443 446 454 private void parseBundleAttribute( String baseDir, String bundle ) { 455 myBundle = bundle; 456 457 if( myBundle != null && 458 myBundle.toLowerCase( ).endsWith( PROPERTIES ) ) { 459 int index = myBundle.length( ) - PROPERTIES.length( ); 460 myBundle = myBundle.substring( 0, index ); 461 } 462 463 if( baseDir != null ) { 464 myBundle = baseDir + myBundle; 465 } 466 467 } 468 469 470 479 private void parseDefaultSizeAttribute( String value ) { 480 myDefaultSize = NbIcon.SIZE_16x16; 481 482 try { 483 myDefaultSize = Integer.parseInt( value ); 484 } 485 catch( NumberFormatException nfe ) { 486 ErrorManager manager = 487 ErrorManager.getDefault( ).getInstance( "org.netbeans.spi.enode" ); 488 manager.notify( ErrorManager.INFORMATIONAL, nfe ); 489 } 490 } 491 492 493 501 private static String parseBaseDirAttribute( String value ) { 502 String baseDir = value; 503 504 if( baseDir.charAt( baseDir.length( ) - 1 ) != '/' ) { 505 baseDir = value + '/'; 506 } 507 508 if( value.charAt( 0 ) == '/' ) { 509 baseDir = baseDir.substring( 1 ); 510 } 511 512 return baseDir; 513 } 514 515 516 524 private void parseAttribute( String attribute, String value, String baseDir ) { 525 value = value.trim( ); 529 attribute = attribute.trim( ); 530 531 if( attribute.equals( DESCRIPTION_ATTRIBUTE ) ) { 532 myDescription = value; 533 } 534 else if( attribute.equals( BUNDLE_ATTRIBUTE ) ) { 535 parseBundleAttribute( baseDir, value ); 536 } 537 else if( attribute.equals( DEFAULT_SIZE_ATTRIBUTE ) ) { 538 parseDefaultSizeAttribute( value ); 539 } 540 else { 541 parseIconAttribute( attribute, baseDir, value ); 542 } 543 } 544 545 553 private void parseIconAttribute( String attribute, String baseDir, String bitmap ) { 554 boolean defaultIcon = false; 555 556 if( attribute.charAt( 0 ) == DEFAULT_MARK ) { 561 defaultIcon = true; 562 attribute = attribute.substring( 1 ); 563 } 564 565 if( baseDir != null ) { 566 bitmap = baseDir + bitmap; 567 } 568 569 myIcons.put( attribute, bitmap ); 570 571 if( defaultIcon ) { 575 int size = parseSize( attribute, getDefaultSize( ) ); 576 String key = "" + DEFAULT_MARK + size; 577 attribute = parseName( attribute ); 578 myIcons.put( key, attribute ); 579 } 580 } 581 582 583 593 private static boolean isDefaultKey( String key ) { 594 return key.charAt( 0 ) == DEFAULT_MARK; 595 } 596 597 598 610 private static int parseSize( String attribute, int defaultSize ) { 611 int size = defaultSize; 612 613 try { 614 int start = attribute.lastIndexOf( SIZE_MARK ); 615 String suffix = attribute.substring( start + 1 ); 616 size = Integer.parseInt( suffix ); 617 } 618 catch( Exception e ) { 619 ErrorManager manager = 620 ErrorManager.getDefault( ).getInstance( "org.netbeans.spi.enode" ); 621 manager.notify( ErrorManager.INFORMATIONAL, e ); 622 } 623 624 return size; 625 } 626 627 637 private static String parseName( String attribute ) { 638 String name = attribute; 639 640 int index = attribute.lastIndexOf( SIZE_MARK ); 641 642 if( index > 0 ) { 643 name = attribute.substring( 0, index ); 644 } 645 646 return name; 647 } 648 649 658 private static String makeKey( String name, int size ) { 659 return name + SIZE_MARK + size; 660 } 661 } 662 663 | Popular Tags |