1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.commons.gui.util;
21
22 import java.awt.Component;
23 import java.awt.Graphics;
24 import java.awt.Image;
25 import java.net.URL;
26 import java.util.ArrayList;
27 import java.util.List;
28
29 import javax.swing.Icon;
30 import javax.swing.ImageIcon;
31
32 /***
33 * @author Steffen Pingel
34 */
35 public class IconHelper {
36
37 /***
38 * The default path to search for icons.
39 */
40 public static final String DEFAULT_ICON_PATH = "icons/";
41
42 public static ClassLoader classLoader = IconHelper.class.getClassLoader();
43
44 /***
45 * List bigger sizes first. Scaling down looks nicer than scaling up.
46 */
47 private static final int[] DEFAULT_ICON_SUBDIRECTORIES = {
48 64, 52, 32, 22, 16
49 };
50
51 private static String iconPath = DEFAULT_ICON_PATH;
52 private static int[] subDirectories = DEFAULT_ICON_SUBDIRECTORIES;
53
54 public static final Icon getIcon(String filename, int size, boolean createEmptyIcon)
55 {
56 if (filename == null) {
57 return createEmptyIcon ? new EmptyIcon(size) : null;
58 }
59
60 ImageIcon icon = getImage(size, filename);
61 if (icon != null) {
62 if (icon.getIconWidth() != size || icon.getIconHeight() != size) {
63
64 icon = new ImageIcon(icon.getImage().getScaledInstance
65 (size, size, Image.SCALE_SMOOTH));
66 }
67
68 return icon;
69 }
70 else {
71 return createEmptyIcon ? new EmptyIcon(size) : null;
72 }
73 }
74
75 /***
76 * @return never returns null
77 * @see #getIcon(String, int, boolean)
78 */
79 public static final Icon getIcon(String filename, int size)
80 {
81 return getIcon(filename, size, true);
82 }
83
84 /***
85 * Searches {@link #iconPath} for <code>filename</code>.
86 * @return null, if <code>filename</code> does not exists
87 */
88 public static final ImageIcon getImage(String filename)
89 {
90 URL url = classLoader.getResource(iconPath + filename);
91 return (url != null) ? getImageIcon(url) : null;
92 }
93
94 public static final ImageIcon getImage(int size, String filename)
95 {
96 URL url = getImageURL(size, filename);
97 if (url != null) {
98 return getImageIcon(url);
99 }
100
101
102 for (int subDirectory : subDirectories) {
103 if (subDirectory == size) {
104 continue;
105 }
106 url = getImageURL(subDirectory, filename);
107 if (url != null) {
108 return getImageIcon(url);
109 }
110 }
111
112 return null;
113 }
114
115 public static final URL getImageURL(int size, String filename)
116 {
117 return classLoader.getResource(DEFAULT_ICON_PATH + size + "/" + filename);
118 }
119
120 public static final ImageIcon getImageIcon(URL url)
121 {
122 return new ImageIcon(url);
123 }
124
125 public static final Icon getEmptyIcon(int size)
126 {
127 return new EmptyIcon(size);
128 }
129
130 public static final List<? extends Image> getApplicationIcons(String filename) {
131 List<Image> icons = new ArrayList<Image>();
132 for (int subDirectory : subDirectories) {
133 URL url = getImageURL(subDirectory, filename);
134 if (url != null) {
135 icons.add(getImageIcon(url).getImage());
136 }
137 }
138 return (icons.isEmpty()) ? null : icons;
139 }
140
141 public static final Icon getListIcon(String filename)
142 {
143 return getIcon(filename, 32, false);
144 }
145
146 public static final Icon getButtonIcon(String filename)
147 {
148 return getIcon(filename, 16, false);
149 }
150
151 public static final Icon getMenuIcon(String filename)
152 {
153 return getIcon(filename, 16);
154 }
155
156 public static final Icon getStatusBarIcon(String filename)
157 {
158 return getIcon(filename, 16, false);
159 }
160
161 public static final ImageIcon getSystemTrayIcon(String filename)
162 {
163 Icon icon = getIcon(filename, 16, false);
164 return (icon instanceof ImageIcon) ? (ImageIcon) icon : null;
165 }
166
167 public static final Icon getTableIcon(String filename)
168 {
169 return getIcon(filename, 14, true);
170 }
171
172 public static final Icon getTabTitleIcon(String filename)
173 {
174 return getIcon(filename, 16, false);
175 }
176
177 public static final Icon getTreeIcon(String filename)
178 {
179 return getIcon(filename, 16, false);
180 }
181
182 public static final Icon getToolBarIcon(String filename)
183 {
184 return getIcon(filename, 22);
185 }
186
187 public static final Icon getTitleIcon(String filename)
188 {
189 return getIcon(filename, 32, false);
190 }
191
192 public static void setIconPath(String iconPath)
193 {
194 IconHelper.iconPath = iconPath;
195 }
196
197 public static void setSubDirectories(int[] subDirectories)
198 {
199 IconHelper.subDirectories = subDirectories;
200 }
201
202 /***
203 * Provides an empty, transparent icon.
204 */
205 public static class EmptyIcon implements Icon
206 {
207 private int size;
208
209 /***
210 * Constructs an empty icon with a width and heigt of
211 * <code>size</code>.
212 */
213 public EmptyIcon(int size)
214 {
215 this.size = size;
216 }
217
218 public int getIconHeight()
219 {
220 return size;
221 }
222
223 public int getIconWidth()
224 {
225 return size;
226 }
227
228 public void paintIcon(Component c, Graphics g, int x, int y)
229 {
230 }
231
232 }
233
234 public static Icon getActionIcon(String filename)
235 {
236 return getIcon(filename, 16, false);
237 }
238
239 }