1 /***
2 * IconGrabber.java
3 *
4 * Project: Dependency Tool
5 *
6 * WHEN WHO WHAT
7 * 06.06.2003 pko initial public release
8 * 20.01.2003 pko modification
9 * 10.12.2002 pko creation
10 *
11 * Copyright 2003 ELCA Informatique SA
12 * Av. de la Harpe 22-24, 1000 Lausanne 13, Switzerland
13 * www.elca.ch
14 *
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public License
17 * as published by the Free Software Foundation; either version 2.1 of
18 * the License, or (at your option) any later version.
19 *
20 * This library is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
24 *
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 * USA
29 */
30
31 package ch.elca.dependency.util;
32
33 import java.awt.Image;
34 import java.awt.Toolkit;
35 import java.net.URL;
36 import javax.swing.ImageIcon;
37
38 import org.apache.log4j.Logger;
39
40 /***
41 * An utility class used to get Images and Icons easily from the FileSystem.
42 *
43 * @author Pawel Kowalski
44 * @version 1.0-beta
45 */
46 public class IconGrabber {
47
48 /***
49 * Log4J logger.
50 */
51 private final static Logger LOG = Logger.getLogger(IconGrabber.class);
52
53 /***
54 * A ClassLoader to load resources from the FileSystem.
55 */
56 private static final ClassLoader classLoader = ClassLoader.getSystemClassLoader();
57
58 /***
59 * A Toolkit to create images.
60 */
61 private static final Toolkit toolkit = Toolkit.getDefaultToolkit();
62
63 /***
64 * FileSeparator used.
65 */
66 private static final String fileSeparator = "/";
67
68 /***
69 * The path to a directory with images.
70 */
71 private static final String path = "images";
72
73 /***
74 * Get an Icon for its name.
75 *
76 * @param name a <code>String</code> value
77 * @return an <code>ImageIcon</code> value
78 */
79 public static ImageIcon getIcon(String name) {
80 return getIcon(name, name);
81 }
82
83 /***
84 * Get an Icon for its name and associate a description with it.
85 *
86 * @param name a <code>String</code> value
87 * @param description a <code>String</code> value
88 * @return an <code>ImageIcon</code> value
89 */
90 public static ImageIcon getIcon(String name, String description) {
91 URL url = getURL(name);
92 if(url == null) {
93 return null;
94 } else {
95 return new ImageIcon(url, description);
96 }
97 }
98
99 /***
100 * Get an Image for its name.
101 *
102 * @param name a <code>String</code> value
103 * @return an <code>Image</code> value
104 */
105 public static Image getImage(String name) {
106 URL url = getURL(name);
107 if(url == null) {
108 return null;
109 } else {
110 return toolkit.createImage(url);
111 }
112 }
113
114 /***
115 * Get a FileSystem-URL for a name.
116 *
117 * @param name a <code>String</code> value
118 * @return an <code>URL</code> value
119 */
120 private static URL getURL(String name) {
121 URL url = classLoader.getResource(path + fileSeparator + name);
122 if(url == null) {
123 LOG.error("Cannot find icon " + name + " in directory " + path);
124 return null;
125 } else {
126 return url;
127 }
128 }
129 }
This page was automatically generated by Maven