001: /*
002: * Copyright (c) 2004 JETA Software, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without modification,
005: * are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JETA Software nor the names of its contributors may
015: * be used to endorse or promote products derived from this software without
016: * specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
021: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
022: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
023: * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
024: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
025: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
026: * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: */
029:
030: package com.jeta.open.resources;
031:
032: import java.awt.image.BufferedImage;
033: import java.io.IOException;
034: import java.io.InputStream;
035: import java.util.Hashtable;
036:
037: import javax.swing.ImageIcon;
038:
039: import com.jeta.open.registry.JETARegistry;
040:
041: /**
042: * This class is an implementation of a ResourceLoader. It insulates the
043: * application code from having any need to know about the local file system
044: * directory structure. It is also useful for debugging and development so we
045: * can redirect resource request to debug files if needed.
046: *
047: * @author Jeff Tassin
048: */
049: public class AppResourceLoader implements ResourceLoader {
050: private ClassLoader m_classloader;
051:
052: /** an empty icon if a resource cannot be loaded */
053: private static ImageIcon m_empty_icon;
054:
055: /** cache of images */
056: private static Hashtable m_imagecache = new Hashtable();
057:
058: public AppResourceLoader() {
059:
060: }
061:
062: /**
063: * @return a custom class loader for the application
064: */
065: public ClassLoader getClassLoader() {
066: if (m_classloader == null)
067: return AppResourceLoader.class.getClassLoader();
068: else
069: return m_classloader;
070: }
071:
072: /**
073: * Returns an icon with a red X to show an icon that could not be loaded.
074: */
075: public static ImageIcon getEmptyIcon() {
076: synchronized (AppResourceLoader.class) {
077: if (m_empty_icon == null) {
078: int width = 16;
079: int height = 16;
080: BufferedImage img = new BufferedImage(width, height,
081: BufferedImage.TYPE_INT_RGB);
082: java.awt.Graphics2D bg = img.createGraphics();
083: bg.setColor(javax.swing.UIManager.getColor("control"));
084: bg.fillRect(0, 0, width, height);
085: bg.setColor(java.awt.Color.red);
086: bg.drawRect(0, 0, width - 1, height - 1);
087: bg.drawLine(0, 0, width - 1, height - 1);
088: bg.drawLine(0, height - 1, width - 1, 0);
089: bg.dispose();
090: m_empty_icon = new ImageIcon(img);
091: }
092: }
093: return m_empty_icon;
094: }
095:
096: /**
097: * Opens and returns an input stream for the given resourceName. The
098: * resourceName is relative to the application CLASSPATH (i.e. JAR file).
099: *
100: * @param resourceName
101: * the relative name of the resource to open
102: * @return an input stream for the given resourceName.
103: */
104: public InputStream getResourceAsStream(String resourceName)
105: throws IOException {
106: ClassLoader classloader = getClassLoader();
107: return classloader.getResourceAsStream(resourceName);
108: }
109:
110: /**
111: * Loads an image from disk. The image is loaded relative to the application
112: * directory.
113: *
114: * @todo we need to cache these images
115: */
116: public static ImageIcon getImage(String imageName) {
117: if (imageName != null) {
118: ImageIcon icon = (ImageIcon) m_imagecache.get(imageName);
119: if (icon == null) {
120: try {
121: ResourceLoader loader = (ResourceLoader) JETARegistry
122: .lookup(ResourceLoader.COMPONENT_ID);
123: assert (loader != null);
124: icon = loader.loadImage(imageName);
125: } catch (Exception e) {
126: e.printStackTrace();
127: }
128:
129: if (icon == null) {
130: icon = new ImageIcon();
131: }
132: m_imagecache.put(imageName, icon);
133: }
134: return icon;
135: } else {
136: assert (false);
137: return m_empty_icon;
138: }
139: }
140:
141: /**
142: * Helper utility to load an image file from the application images
143: * directory
144: *
145: * @param imageName
146: * the name (and optional sub directory ) of the file to load
147: */
148: public ImageIcon loadImage(String imageName) {
149: try {
150: ClassLoader classloader = getClassLoader();
151: java.net.URL url = classloader.getResource(imageName);
152: if (url != null) {
153: ImageIcon icon = new ImageIcon(url);
154: return icon;
155: } else {
156: System.err
157: .println("AppResourceLoader.loadImage failed: "
158: + imageName);
159: }
160: } catch (Exception e) {
161: System.err.println("AppResourceLoader.loadImage failed: "
162: + imageName);
163: e.printStackTrace();
164: }
165: return getEmptyIcon();
166: }
167:
168: public void setClassLoader(ClassLoader loader) {
169: m_classloader = loader;
170: }
171:
172: }
|