com.metamatrix.toolbox.ui.widget.util
Class IconFactory

java.lang.Object
  extended by com.metamatrix.toolbox.ui.widget.util.IconFactory

public class IconFactory
extends java.lang.Object

IconFactory is a static class for generating ImageIcon objects from graphics files located either inside a jar file or on the file system. IconFactory first tries to locate the file in the JVM's classpath by searching the fullLocationPath. If it is not found, IconFactory then searches the file system via the relativeLocationPath from the application's current default directory. This design allows for software developers to run the application outside a jar file pulling image resources from their file system with the same code as would be obtaining resources from a jar archive in production.

IconFactory is designed to be set upon application startup and used throughout the application. The following is an example of it's use:

      // initialize the factory paths
      IconFactory.setFullLocationPath("/com/metamatrix/modeler/images/");
      IconFactory.setRelativeLocationPath("../images/");
      ...
      // use the factory
      ImageIcon imageIcon = IconFactory.getIconForImageFile("splash.jpg");
      JLabel imageLabel = new JLabel(imageIcon);
 

IconFactory was designed assuming all images for an application would be located in a single directory. If images are in multiple locations, you could set one location as the default, then for other locations use the getIconForImageFile( ) method where the full and relative paths are specified explicitly.

Usage Note: Graphics file names are case sensitive in jar files, but may not be case sensitive on the file system. Always use case sensitive file names.


Constructor Summary
IconFactory()
           
 
Method Summary
static javax.swing.ImageIcon getIconForImageFile(java.lang.String fileName)
          Generate an ImageIcon from the specified graphics file in the default locations.
static javax.swing.ImageIcon getIconForImageFile(java.lang.String fileName, java.lang.String jarPath, java.lang.String relativePath)
          Generate an ImageIcon from the specified graphics file.
static void setDefaultJarPath(java.lang.String path)
          Set the default path for the images resources as located within a jar.
static void setDefaultRelativePath(java.lang.String path)
          Set the default path for the images resources as located on the file system.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IconFactory

public IconFactory()
Method Detail

setDefaultJarPath

public static void setDefaultJarPath(java.lang.String path)

Set the default path for the images resources as located within a jar. Example:

 IconFactory.setDefaultJarPath("/com/metamatrix/modeler/images/"); 


setDefaultRelativePath

public static void setDefaultRelativePath(java.lang.String path)

Set the default path for the images resources as located on the file system. Example:

 IconFactory.setDefaultRelativePath("../images/"); 

Parameters:
path - relative path string. Default value is "."

getIconForImageFile

public static javax.swing.ImageIcon getIconForImageFile(java.lang.String fileName)
Generate an ImageIcon from the specified graphics file in the default locations. The setDefaultJarPath() method must have been called previously within this JVM or this method will throw a RuntimeException.

Parameters:
fileName - the name of the graphics file to be found either in the specified resource location or relative path on the file system.
Returns:
an ImageIcon object created from the specified file, null if the file cannot be located.
Throws:
java.lang.RuntimeException - if both defaultJarPath and defaultRelativePath properties are null for this static class.

getIconForImageFile

public static javax.swing.ImageIcon getIconForImageFile(java.lang.String fileName,
                                                        java.lang.String jarPath,
                                                        java.lang.String relativePath)
Generate an ImageIcon from the specified graphics file.

Parameters:
fileName - the name of the graphics file - case sensitive.
jarPath - the full path of the image resources. Example:
 /com/metamatrix/modeler/images/ 
relativePath - the path on the file system relative to the current directory. Example:
 ../images/ 
Returns:
an ImageIcon object created from the specified file, null if the file cannot be located.


Copyright © 2009. All Rights Reserved.