1 package org.votech.ds6.imageutils;
2
3 import java.awt.Image;
4 import java.awt.image.BufferedImage;
5 import java.io.File;
6 import java.io.IOException;
7 import java.net.URL;
8 import java.util.Map;
9 import java.util.WeakHashMap;
10
11 import javax.imageio.ImageIO;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15
16
17
18 public class ImageHelper {
19 /**
20 * Logger for this class
21 */
22 private static final Log logger = LogFactory.getLog(ImageHelper.class);
23 private Class base;
24
25
26 /**
27 * Construct an ImageHelper. All images will be located on
28 * the classpath relative to the supplied class.
29 * @param base
30 */
31 public ImageHelper(Class base){
32 this.base = base;
33 }
34
35 private static Map<URL, ResizableImageIcon> imageCache=new WeakHashMap<URL, ResizableImageIcon>();
36
37 /**
38 * Get a resizable Icon from the supplied URL.
39 * @param url
40 * @return
41 */
42 public static ResizableImageIcon getIcon(URL url) {
43 ResizableImageIcon im = imageCache.get(url);
44 if (im==null) {
45 im = new ResizableImageIcon(url);
46 imageCache.put(url, im);
47 }
48 return im;
49 }
50
51 /**
52 * Get an image from the supplied URL
53 * @param url
54 * @return
55 */
56 public static Image getImage(URL url) {
57 return getIcon(url).getImage();
58 }
59
60
61
62 /**
63 * Get a resizable icon with the given name, stored on the classpath
64 * relative to the supplied base class.
65 * @param name
66 * @return
67 */
68 public ResizableImageIcon getIcon(String name) {
69
70 URL url = base.getResource(name);
71 return getIcon(url);
72 }
73
74 /**
75 * Get an image with the given name, stored on the classpath
76 * relative to the supplied base class.
77 */
78 public Image getImage(String name) {
79 return getIcon(name).getImage();
80 }
81
82 /**
83 * Copy an image out to a given file.
84 * @param original original image
85 * @param file location to copy to
86 * @param file format
87 * @throws IOException
88 */
89 public static void copyTo(URL original, File file, String format) throws IOException {
90 BufferedImage image = ImageIO.read(original);
91 ImageIO.write(image, format, file );
92 }
93
94 /**
95 * Copy an image that's on the classpath relative to the base class out to a given file
96 * @param location location of the file relative to the base class
97 * @param file
98 * @param format
99 * @throws IOException
100 */
101 public void copyTo(String location, File file, String format) throws IOException {
102 URL url = base.getResource(location);
103 copyTo(url, file, format);
104 }
105
106 /**
107 * Copy an image from the classpath (relative to the base class) to a temporary file.
108 * @param image image name
109 * @param type jpg or gif etc
110 * @return a URL to the local file
111 */
112 public URL copyImageToTempFile(String image, String type) {
113
114 try {
115 File iconFile = File.createTempFile("tabviewIcon",".jpg");
116 iconFile.deleteOnExit();
117 copyTo(image, iconFile, "JPG");
118 return iconFile.toURL();
119 } catch (IOException e) {
120 logger.warn("Couldn't copy icon logo to temporary file");
121 throw new RuntimeException(e);
122 }
123 }
124
125 public URL getImageUrl(String image) {
126 return base.getResource(image);
127 }
128
129 }