1 /*
2 * Copyright (c) 2003-2008 by Cosylab d. d.
3 *
4 * This file is part of CosyBeans-Common.
5 *
6 * CosyBeans-Common is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CosyBeans-Common is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CosyBeans-Common. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 package com.cosylab.gui.components.util;
21
22 import java.awt.Image;
23 import java.awt.Toolkit;
24 import java.awt.image.ImageProducer;
25
26 import java.io.IOException;
27
28 import java.net.URL;
29
30 import java.security.AccessController;
31 import java.security.PrivilegedAction;
32
33 import java.util.HashMap;
34
35
36 /**
37 * ImageHelper acts as an interface to the all images currently in classpath.
38 * ImageHelper is a singleton class that maintains internal cache to all
39 * accessed images. This class may be used to minimize the memory consumption
40 * where application uses static images. If images are changed independently,
41 * this class should not be used.
42 *
43 * @author <a href="mailto:jernej.kamenik@cosylab.com">Jernej Kamenik</a>
44 * @version $id$
45 *
46 */
47 public class ImageHelper {
48 /** cache for images. */
49 private static final HashMap<String, Image> cache = new HashMap<String, Image>();
50
51 /** singleton instance of this class. */
52 private static ImageHelper instance = null;
53
54 /**
55 * Protected constructor for creation of singleton class.
56 */
57 private ImageHelper() {
58 super();
59 }
60
61 /**
62 * Returns instance of a singleton ImageHelper.
63 *
64 * @return ImageHelper
65 */
66 private static synchronized ImageHelper getInstance() {
67 if (instance == null) {
68 instance = new ImageHelper();
69 }
70
71 return instance;
72 }
73
74 /**
75 * Returns image from the cache. If image cannot be found in cache, return
76 * value will be <code>null</code>.
77 *
78 * @param name String
79 * @return Image instance of an image or null if not found
80 */
81 private static Image getCachedImage(String name) {
82 if (cache.containsKey(name)) {
83 return (Image) cache.get(name);
84 }
85
86 return null;
87 }
88
89 /**
90 * Puts image in the cache. If an image with the specified name has already been stored,
91 * current image will be overwritten.
92 *
93 * @param name
94 * @param image
95 */
96 private static synchronized void setCachedImage(String name, Image image) {
97 cache.put(name, image);
98 }
99
100 /**
101 * Returns instance of the image by loading it using the <code>
102 * ClassLoader</code> instance provided. images are cached. If an image with
103 * the same name is requested several times, <code>createImage</code> will
104 * return the same instance each time. If images will be manipulated
105 * individually, do not use this method to load images. If image is not found
106 * in the classpath, return value will be null.
107 *
108 * @param name String
109 * @param cl ClassLoader Instance of class loader to use
110 * @return image Instance of image or null if image could not be found.
111 */
112 public static Image createimage(String name, ClassLoader cl) {
113 Image image = getCachedImage(name);
114
115 if (image == null) {
116 image = loadResource(name, cl);
117
118 if (image != null) {
119 setCachedImage(name, image);
120 }
121 }
122
123 return image;
124 }
125
126 /**
127 * Returns instance of the image. images are cached. If an image with the same
128 * name is requested several times, <code>createImage</code> will return the
129 * same instance each time. If images will be manipulated individually, do
130 * not use this method to load images. If image is not found in the classpath,
131 * return value will be null.
132 *
133 * @param name String
134 * @return image Instance of image or null if image could not be found.
135 */
136 public static Image createImage(String name) {
137 return createimage(name, ImageHelper.class.getClassLoader());
138 }
139
140 /**
141 * This is a utility method to help in loading image images.
142 * It takes the name of a resource file and loads an image object
143 * from that file. Typically images will be GIFs.
144 * <p>
145 * @param resourceName A pathname relative to the directory
146 * holding the class loader specified. For example,
147 * "wombat.gif".
148 * @param c ClassLoader.
149 * @return an image object. May be null if the load failed.
150 */
151 public static Image loadResource(final String resourceName,
152 final ClassLoader c) {
153 try {
154 ImageProducer ip = (ImageProducer) AccessController.doPrivileged(new PrivilegedAction<Object>() {
155 public Object run() {
156 URL url;
157
158 if ((url = c.getResource(resourceName)) == null) {
159 return null;
160 } else {
161 try {
162 return url.getContent();
163 } catch (IOException ioe) {
164 return null;
165 }
166 }
167 }
168 });
169
170 if (ip == null) {
171 return null;
172 }
173
174 Toolkit tk = Toolkit.getDefaultToolkit();
175
176 return tk.createImage(ip);
177 } catch (Exception ex) {
178 return null;
179 }
180 }
181 }