org.salespointframework.core
Class ResourceManager

java.lang.Object
  extended by org.salespointframework.core.ResourceManager

public class ResourceManager
extends java.lang.Object

The global manager for all resources. The ResourceManager is responsible for managing all binary resources of the framework. This can be images, videos, text ...

Resources are managed as follows:

The root directory is a directory ending with the name "resource". Here it is just the directory "resource". Starting from this directory all resources are given back relative to this path.

There can be multiple resouces to manage: gif-images, jpeg-images, text and others.
For all of these resources there should exist one resource module.
There is already one module for gif-resources.

Resources are requested as follows:
URL url = ResourceManager.getInstance().getResource (String type, String path);
The "type" is the name of the resource module. The path string is the path relative to the directory of the requested type and is separated with a dot.
Example:

An icon of the size 16x16 should be requested. It is located in the directory "resource/gif/icon".
The icon's name is "document.gif". So the call is:
URL url = ResourceManager.getInstance().getResource (RESOURCE_GIF, "icon.document");

Version:
1.0
Author:
Thomas Medack

Nested Class Summary
static interface ResourceManager.ResourceModule
          Interface for a resource module.
private static class ResourceManager.ResourceModuleGIF
          Predefined module for .gif-resources.
private static class ResourceManager.ResourceModulePNG
          Predefined module for .png-resources.
static class ResourceManager.StandardResource
          Standard resources
 
Field Summary
private  java.util.Map<java.lang.String,java.awt.image.BufferedImage> imageCache
          Image Cache
private static ResourceManager instance
          The singleton instance of the resource manager.
private  java.util.HashMap<java.lang.String,ResourceManager.ResourceModule> moduls
          Map of all resource modules.
static java.lang.String RESOURCE_GIF
          Type name for resource module: GIF
static java.lang.String RESOURCE_PNG
           
protected  java.lang.String resourceRootPath
          The root path of all resources.
protected  java.util.Map<ResourceManager.StandardResource,java.awt.image.BufferedImage> standardResources
          Map of all standard resources
 
Constructor Summary
protected ResourceManager()
          Creates a new instance of ResourceManager.
 
Method Summary
 void addResourceModule(ResourceManager.ResourceModule rm)
          Adds a resource module to the ResourceManager.
static java.awt.image.BufferedImage createImageFromByteArray(byte[] data)
          creates an optimized image from a byte array
 java.awt.image.BufferedImage getCachedImageRessource(java.lang.String type, java.lang.String path)
          Returns a resource as BufferedImage.
 java.awt.image.BufferedImage getCachedImageRessource(java.net.URL url)
          get a cached and optimized image from given url
 java.lang.String getFullPath(java.lang.String type, java.lang.String path)
          Returns the full path of a resource..
static ResourceManager getInstance()
          Returns the singleton instance of the ResourceManager.
 java.net.URL getResource(java.lang.String type, java.lang.String path)
          Returns a resource as URL.
 java.awt.image.BufferedImage getStandardResource(ResourceManager.StandardResource stdres)
          get a specific standard resource
static byte[] loadImageAsByteArray(java.net.URI uri)
          loads the specified image and returns an byte array
static byte[] loadImageAsByteArray(java.net.URI uri, int maxdim)
          loads the specified image. resizes it and returns an byte array
static java.awt.image.BufferedImage optimizeImage(java.awt.Image img, boolean forceOpaque)
          Optimize Image for Screen
 void removeResourceModule(ResourceManager.ResourceModule rm)
          Removes a resource module from the ResourceManager.
 void setStandardResource(ResourceManager.StandardResource stdres, java.awt.image.BufferedImage bi)
          Set a standard resource
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

standardResources

protected java.util.Map<ResourceManager.StandardResource,java.awt.image.BufferedImage> standardResources
Map of all standard resources


moduls

private java.util.HashMap<java.lang.String,ResourceManager.ResourceModule> moduls
Map of all resource modules.


resourceRootPath

protected java.lang.String resourceRootPath
The root path of all resources.


instance

private static ResourceManager instance
The singleton instance of the resource manager.


imageCache

private java.util.Map<java.lang.String,java.awt.image.BufferedImage> imageCache
Image Cache


RESOURCE_GIF

public static final java.lang.String RESOURCE_GIF
Type name for resource module: GIF

See Also:
Constant Field Values

RESOURCE_PNG

public static final java.lang.String RESOURCE_PNG
See Also:
Constant Field Values
Constructor Detail

ResourceManager

protected ResourceManager()
Creates a new instance of ResourceManager.

Method Detail

getInstance

public static ResourceManager getInstance()
Returns the singleton instance of the ResourceManager.

Returns:
the singleton instance.

addResourceModule

public void addResourceModule(ResourceManager.ResourceModule rm)
Adds a resource module to the ResourceManager.

Parameters:
rm - the resource module to be added.

removeResourceModule

public void removeResourceModule(ResourceManager.ResourceModule rm)
Removes a resource module from the ResourceManager.

Parameters:
rm - the resource module to be removed.

getResource

public java.net.URL getResource(java.lang.String type,
                                java.lang.String path)
Returns a resource as URL. Therefore the logical name of the resource module and the relative path starting from it have to be specified.

Example:

logical name of the gif-module: "RESOURCE_GIF". The absolute path of this module is "org/eit/ui/resource/gif". The requested resource is "document.gif".
Therefore the call is: getResource ("RESOURCE_GIF", "icon.16x16.document) (without file extension).

Parameters:
type - the logical type name
path - the relative path + file name without extension
Returns:
the resource's URL.

setStandardResource

public void setStandardResource(ResourceManager.StandardResource stdres,
                                java.awt.image.BufferedImage bi)
Set a standard resource

Parameters:
stdres - Resource to set
bi - image resource

getStandardResource

public java.awt.image.BufferedImage getStandardResource(ResourceManager.StandardResource stdres)
get a specific standard resource

Parameters:
stdres - resource to get
Returns:

createImageFromByteArray

public static java.awt.image.BufferedImage createImageFromByteArray(byte[] data)
creates an optimized image from a byte array

Parameters:
data -
Returns:

loadImageAsByteArray

public static byte[] loadImageAsByteArray(java.net.URI uri)
loads the specified image and returns an byte array

Parameters:
uri - loaction of image

loadImageAsByteArray

public static byte[] loadImageAsByteArray(java.net.URI uri,
                                          int maxdim)
loads the specified image. resizes it and returns an byte array

Parameters:
uri - location of image
if - width or height exceeds this value, the image will be scaled down by aspect ratio

optimizeImage

public static java.awt.image.BufferedImage optimizeImage(java.awt.Image img,
                                                         boolean forceOpaque)
Optimize Image for Screen

Parameters:
img -
Returns:

getCachedImageRessource

public java.awt.image.BufferedImage getCachedImageRessource(java.lang.String type,
                                                            java.lang.String path)
Returns a resource as BufferedImage. Resource is cached internally. resource translation is based on getResource(String, String)

Parameters:
type -
path -
Returns:
loaded image

getCachedImageRessource

public java.awt.image.BufferedImage getCachedImageRessource(java.net.URL url)
get a cached and optimized image from given url

Parameters:
url -
Returns:

getFullPath

public java.lang.String getFullPath(java.lang.String type,
                                    java.lang.String path)
Returns the full path of a resource..

Parameters:
type - the logical type name
path - der relative path + file name without extension
Returns:
the resource's full path as String.