QJCC homepage

biz.chitec.quarterback.swing
Class ResourceLoader

java.lang.Object
  extended bybiz.chitec.quarterback.swing.ResourceLoader

public final class ResourceLoader
extends java.lang.Object

Helper class to load more complex resources more conveniently. This class helps loading resources relative to classpath and independent from the distribution. It works in files-in-directories environment as good as in JAR file environment (which was quit a hard task to accomplish with).

It is possible to set prefixes on different resource types which are used automatically when loading.

Version:
$Id: 11c8768a868103f1cb82e4adf554c62bd0f807e0 $
Author:
Dirk Hillbrecht 1997-2000, chitec/Dirk Hillbrecht 2000,2001,2002. Distributed under the terms of the GNU LGPL.

Field Summary
private static java.lang.ClassLoader cl
           
private static java.util.List iconprefixes
           
private static java.util.List resourceprefixes
           
 
Constructor Summary
private ResourceLoader()
          Uninstanciable as all methods are static.
 
Method Summary
static void addIconPath(java.lang.String px)
          Set general prefix for icons.
static void addResourcePath(java.lang.String px)
          Set general prefix for resource files.
static java.awt.image.BufferedImage getBufferedImage(java.lang.String resourcefile)
          Return a BufferedImage from image data stored in a file (e.g. contained in the JAR).
static javax.swing.Icon getIcon(java.lang.String resourcefile)
          Compatibility method which returns an Icon object without description.
static javax.swing.Icon getIcon(java.lang.String resourcefile, java.lang.String icondescription)
          Compatibility method which returns an Icon object.
static javax.swing.ImageIcon getImageIcon(java.lang.String resourcefile)
          Convenience method without icon description.
static javax.swing.ImageIcon getImageIcon(java.lang.String resourcefile, java.lang.String icondescription)
          Return an ImageIcon stored in a file (e.g. contained in the JAR).
static javax.swing.ImageIcon getImageIconOrNull(java.lang.String resourcefile)
          Convenience method without icon description.
static javax.swing.ImageIcon getImageIconOrNull(java.lang.String resourcefile, java.lang.String icondescription)
          Return an ImageIcon stored in a file (e.g. contained in the JAR).
private static java.net.URL getImageURL(java.lang.String resourcefile)
          Locates an image by the given URL along the iconprefixes.
static java.net.URL getResourceURL(java.lang.String resourcefile)
          Locates an image by the given URL along the iconprefixes.
static java.net.URL getURL(java.lang.String resourcefile)
          Return an URL to any kind of resource.
static void setClassLoader(java.lang.ClassLoader clx)
          Sets the class loader.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

iconprefixes

private static java.util.List iconprefixes

resourceprefixes

private static java.util.List resourceprefixes

cl

private static java.lang.ClassLoader cl
Constructor Detail

ResourceLoader

private ResourceLoader()
Uninstanciable as all methods are static.

Method Detail

getURL

public static final java.net.URL getURL(java.lang.String resourcefile)
Return an URL to any kind of resource. Uses the given class loader to find the requested resource. If no class loader has been given explicitly, the system class loader is taken.

Parameters:
resourcefile - The name of the resource to be referenced
Returns:
The URL, null if resource is not found

getImageURL

private static final java.net.URL getImageURL(java.lang.String resourcefile)
Locates an image by the given URL along the iconprefixes.


getResourceURL

public static final java.net.URL getResourceURL(java.lang.String resourcefile)
Locates an image by the given URL along the iconprefixes.


getBufferedImage

public static final java.awt.image.BufferedImage getBufferedImage(java.lang.String resourcefile)
Return a BufferedImage from image data stored in a file (e.g. contained in the JAR). In case of any problem, null is returned.

Parameters:
resourcefile - File containing the image data. Relative to paths in iconprefixes.
Returns:
buffered image or null if something goes wrong

getImageIconOrNull

public static final javax.swing.ImageIcon getImageIconOrNull(java.lang.String resourcefile,
                                                             java.lang.String icondescription)
Return an ImageIcon stored in a file (e.g. contained in the JAR). Note that this method does not work through getBufferedImage(), but loads the image directly for the sake of efficiency.

Parameters:
resourcefile - The file with the resource in question. Relative to paths in iconprefix
icondescription - Textual description of the icon. May be null.
Returns:
ImageIcon instance, null if image is not found.

getImageIcon

public static final javax.swing.ImageIcon getImageIcon(java.lang.String resourcefile,
                                                       java.lang.String icondescription)
Return an ImageIcon stored in a file (e.g. contained in the JAR). In case of any problem, an empty ImageIcon is returned. Note that this method does not work through getBufferedImage(), but loads the image directly for the sake of efficiency.

Parameters:
resourcefile - The file with the resource in question. Relative to paths in iconprefix
icondescription - Textual description of the icon. May be null.
Returns:
ImageIcon instance, if image is not found, a 1*1 pixel transparent image is returned

getImageIconOrNull

public static javax.swing.ImageIcon getImageIconOrNull(java.lang.String resourcefile)
Convenience method without icon description.

Parameters:
resourcefile - Name of the image resource
Returns:
ImageIcon instance, null if image is not found.

getImageIcon

public static javax.swing.ImageIcon getImageIcon(java.lang.String resourcefile)
Convenience method without icon description.

Parameters:
resourcefile - Name of the image resource
Returns:
ImageIcon instance, if image is not found, a 1*1 pixel transparent image is returned

getIcon

public static javax.swing.Icon getIcon(java.lang.String resourcefile,
                                       java.lang.String icondescription)
Compatibility method which returns an Icon object.

Parameters:
resourcefile - The file with the resource in question. Relative to paths in iconprefix
icondescription - Textual description of the icon. May be null.
Returns:
Icon instance (which actually is an ImageIcon), if image is not found, a 1*1 pixel transparent image is returned

getIcon

public static javax.swing.Icon getIcon(java.lang.String resourcefile)
Compatibility method which returns an Icon object without description.

Parameters:
resourcefile - The file with the resource in question. Relative to paths in iconprefix
Returns:
Icon instance (which actually is an ImageIcon), if image is not found, a 1*1 pixel transparent image is returned

addIconPath

public static void addIconPath(java.lang.String px)
Set general prefix for icons. This prefix is prepended to all requests for loading icons. If given, this makes it a lot easier to put all icons into some directories apart from the sources.


addResourcePath

public static void addResourcePath(java.lang.String px)
Set general prefix for resource files. This prefix is prepended to all requests for loading resources. If given, this makes it a lot easier to put all resources into some directories apart from the sources.


setClassLoader

public static void setClassLoader(java.lang.ClassLoader clx)
Sets the class loader. If set, this classloader will be used to load the icons and whatsoever. If not set, the system class loader will be used.


QJCC homepage