1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.gui.util;
21
22 import java.awt.Component;
23 import java.awt.Graphics;
24 import java.awt.Image;
25 import java.net.URL;
26
27 import javax.swing.Icon;
28 import javax.swing.ImageIcon;
29
30 import org.xnap.action.AbstractXNapAction;
31 import org.xnap.util.FileHelper;
32 import org.xnap.util.Preferences;
33
34 /***
35 * This class provides methods to get icons in certain sizes. All icon
36 * directories are searched.
37 */
38 public class IconHelper
39 {
40
41
42
43 public static final String XNAP_ICON = AbstractXNapAction.XNAP_ICON;
44
45 /***
46 * List bigger sizes first. Scaling down looks nicer than scaling up.
47 */
48 private static final int[] ICON_SUBDIRS = {
49 32, 22, 16, 64, 48,
50 };
51
52 /***
53 * The default path to search for icons.
54 */
55 public static final String DEFAULT_ICON_PATH = "icons/";
56
57
58
59 private static Preferences prefs = Preferences.getInstance();
60
61 /***
62 * The path to search for icons.
63 */
64 private static String iconPath = DEFAULT_ICON_PATH;
65
66
67
68
69
70 /***
71 * Returns an icon with <code>size</code> height and width. If no icon
72 * named <code>filename</code> can be found and
73 * <code>createEmptyIcon</code> is true, an empty icon is returned.
74 *
75 * <p>If the icon does not fit <code>size</code> it is scaled.
76 */
77 public static final Icon getIcon(String filename, int size,
78 boolean createEmptyIcon)
79 {
80 if (filename == null) {
81 return createEmptyIcon ? new EmptyIcon(size) : null;
82 }
83
84 ImageIcon icon = getImage(iconPath, size, filename);
85 if (icon == null && !DEFAULT_ICON_PATH.equals(iconPath)) {
86
87 icon = getImage(DEFAULT_ICON_PATH, size, filename);
88 }
89
90 if (icon != null) {
91 if (icon.getIconWidth() != size || icon.getIconHeight() != size) {
92
93 icon = new ImageIcon(icon.getImage().getScaledInstance
94 (size, size, Image.SCALE_SMOOTH));
95 }
96
97 return icon;
98 }
99 else {
100 return createEmptyIcon ? new EmptyIcon(size) : null;
101 }
102 }
103
104 /***
105 * @return never returns null
106 * @see #getIcon(String, int, boolean)
107 */
108 public static final Icon getIcon(String filename, int size)
109 {
110 return getIcon(filename, size, true);
111 }
112
113 /***
114 * Searches {@link #iconPath} + {@link #ICON_PATH} for
115 * <code>filename</code>. If filename is not found {@link
116 * #ICON_PATH} is searched as a fallback.
117 *
118 * @return null, if <code>filename</code> does not exists.
119 */
120 public static final ImageIcon getImage(String filename)
121 {
122 if (!prefs.getShowIcons()) {
123 return null;
124 }
125
126 URL url = FileHelper.getResource(iconPath + filename);
127 if (url == null && !DEFAULT_ICON_PATH.equals(iconPath)) {
128
129 url = FileHelper.getResource(DEFAULT_ICON_PATH + filename);
130 }
131
132 return (url != null) ? new ImageIcon(url) : null;
133 }
134
135 public static final ImageIcon getImage
136 (String path, int size, String filename)
137 {
138 if (!prefs.getShowIcons()) {
139 return null;
140 }
141
142 URL url = FileHelper.getResource(path + size + "/" + filename);
143 if (url != null) {
144 return new ImageIcon(url);
145 }
146
147
148 for (int i = 0; i < ICON_SUBDIRS.length; i++) {
149 url = FileHelper.getResource
150 (path + ICON_SUBDIRS[i] + "/" + filename);
151 if (url != null) {
152 return new ImageIcon(url);
153 }
154 }
155
156 return null;
157 }
158
159 public static final Icon getEmptyIcon(int size)
160 {
161 return new EmptyIcon(size);
162 }
163
164 public static final Icon getListIcon(String filename)
165 {
166 return getIcon(filename, 32, false);
167 }
168
169 public static final Icon getButtonIcon(String filename)
170 {
171 return getIcon(filename, 16, false);
172 }
173
174 public static final Icon getMenuIcon(String filename)
175 {
176 return getIcon(filename, 16);
177 }
178
179 public static String getIconPath()
180 {
181 return iconPath;
182 }
183
184 public static Icon getScaledLogo(int height)
185 {
186 ImageIcon icon = IconHelper.getImage("xnap_logo.png");
187 return (icon != null)
188 ? new ImageIcon(icon.getImage().getScaledInstance
189 (-1, height, Image.SCALE_SMOOTH))
190 : null;
191 }
192
193 public static final Icon getStatusBarIcon(String filename)
194 {
195 return getIcon(filename, 16, false);
196 }
197
198 public static final Icon getTabTitleIcon(String filename)
199 {
200 return getIcon(filename, 16, false);
201 }
202
203 public static final Icon getTreeIcon(String filename)
204 {
205 return getIcon(filename, 16, false);
206 }
207
208 public static final Icon getToolBarIcon(String filename)
209 {
210 return getIcon(filename, 22);
211 }
212
213 public static final Icon getWizardIcon(String filename)
214 {
215 return getIcon(filename, 48, false);
216 }
217
218 /***
219 * Sets the search path iconPath. Used for icon theming.
220 */
221 public static void setIconPath(String iconPath)
222 {
223 if (iconPath == null) {
224 throw new NullPointerException();
225 }
226
227 IconHelper.iconPath = iconPath;
228 }
229
230 /***
231 * Provides an empty, transparent icon.
232 */
233 public static class EmptyIcon implements Icon
234 {
235 private int size;
236
237 /***
238 * Constructs an empty icon with a width and heigt of
239 * <code>size</code>.
240 */
241 public EmptyIcon(int size)
242 {
243 this.size = size;
244 }
245
246 public int getIconHeight()
247 {
248 return size;
249 }
250
251 public int getIconWidth()
252 {
253 return size;
254 }
255
256 public void paintIcon(Component c, Graphics g, int x, int y)
257 {
258 }
259
260 }
261
262 }