1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.eclipse.common.util;
17
18 import java.io.InputStream;
19 import java.lang.reflect.Field;
20 import java.lang.reflect.Modifier;
21 import java.net.URL;
22 import java.util.Enumeration;
23 import java.util.Locale;
24 import java.util.ResourceBundle;
25
26 import org.eclipse.jface.resource.ImageDescriptor;
27 import org.eclipse.jface.resource.ImageRegistry;
28 import org.eclipse.swt.graphics.Image;
29 import org.eclipse.swt.graphics.ImageData;
30 import org.eclipse.swt.widgets.Display;
31 import org.seasar.framework.beans.BeanDesc;
32 import org.seasar.framework.beans.factory.BeanDescFactory;
33 import org.seasar.framework.exception.ResourceNotFoundRuntimeException;
34 import org.seasar.framework.log.Logger;
35 import org.seasar.framework.util.FieldUtil;
36 import org.seasar.framework.util.ResourceUtil;
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class ImageManager {
51 private static ImageRegistry imageRegistry = new ImageRegistry();
52
53 protected static final Logger logger = Logger.getLogger(ImageManager.class);
54
55 private ImageManager() {
56 }
57
58
59
60
61
62
63
64 public static void init(final Display display) {
65 imageRegistry = new ImageRegistry(display);
66 }
67
68
69
70
71
72
73
74
75 public static Image getImage(final String key) {
76 return imageRegistry.get(key);
77 }
78
79
80
81
82
83
84
85
86 public static ImageDescriptor getImageDescriptor(final String key) {
87 return imageRegistry.getDescriptor(key);
88 }
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116 public static Image loadImage(final String path) {
117 Image image = getImage(path);
118 if (image == null) {
119 image = putImage(path, path);
120 }
121 return image;
122 }
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139 public static ImageDescriptor loadImageDescriptor(final String path) {
140 ImageDescriptor descriptor = getImageDescriptor(path);
141 if (descriptor == null) {
142 descriptor = putImageDescriptor(path, path);
143 }
144 return descriptor;
145 }
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163 public static Image putImage(final String key, final String path) {
164 checkKey(key);
165
166 InputStream is = ResourceUtil.getResourceAsStream(normalizePath(path));
167 Image image = new Image(Display.getCurrent(), is);
168 imageRegistry.put(key, image);
169 return image;
170 }
171
172
173
174
175
176
177
178
179
180
181
182
183
184 public static Image putImage(final String key, final ImageData imageData) {
185 checkKey(key);
186 Image image = new Image(Display.getCurrent(), imageData);
187 imageRegistry.put(key, image);
188 return image;
189 }
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207 public static ImageDescriptor putImageDescriptor(final String key,
208 final String path) {
209 checkKey(key);
210
211 URL url = ResourceUtil.getResource(normalizePath(path));
212 ImageDescriptor descriptor = ImageDescriptor.createFromURL(url);
213 imageRegistry.put(key, descriptor);
214 return descriptor;
215 }
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239 public static void loadImages(final ResourceBundle bundle) {
240 Enumeration keys = bundle.getKeys();
241 while (keys.hasMoreElements()) {
242 String key = (String) keys.nextElement();
243 String path = bundle.getString(key);
244 putImageDescriptor(key, path);
245 }
246 }
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263 public static void loadImages(final String baseName,
264 final ClassLoader loader) {
265 ResourceBundle imageResources = ResourceBundle.getBundle(baseName,
266 Locale.getDefault(), loader);
267 loadImages(imageResources);
268 }
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283 public static void loadImages(final String baseName) {
284 ResourceBundle imageResources = ResourceBundle.getBundle(baseName);
285 loadImages(imageResources);
286 }
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321 public static void injectImages(final Class clazz) {
322 BeanDesc beanDesc = BeanDescFactory.getBeanDesc(clazz);
323 for (int i = 0; i < beanDesc.getFieldSize(); i++) {
324 Field field = beanDesc.getField(i);
325 String key = field.getName();
326 if (!validateMask(field)) {
327 continue;
328 }
329
330 if (isAssignableFrom(Image.class, field)) {
331 injectField(clazz, field, imageRegistry.get(key));
332 } else if (isAssignableFrom(ImageDescriptor.class, field)) {
333 injectField(clazz, field, imageRegistry.getDescriptor(key));
334 }
335 }
336 }
337
338
339
340
341
342
343
344 public static void dispose() {
345 if (imageRegistry != null) {
346 imageRegistry.dispose();
347 }
348 imageRegistry = null;
349 }
350
351 protected static void injectField(final Class clazz, final Field field,
352 final Object o) {
353 if (o != null) {
354 FieldUtil.set(field, null, o);
355 }
356 }
357
358 protected static boolean validateMask(final Field field) {
359 final int MOD_EXPECTED = Modifier.PUBLIC | Modifier.STATIC;
360 final int MOD_MASK = MOD_EXPECTED | Modifier.FINAL;
361 return (field.getModifiers() & MOD_MASK) == MOD_EXPECTED;
362 }
363
364 protected static boolean isAssignableFrom(final Class<?> clazz,
365 final Field target) {
366 return clazz.isAssignableFrom(target.getType());
367 }
368
369 protected static void checkKey(final String key) {
370 if (imageRegistry.get(key) != null) {
371 imageRegistry.remove(key);
372 }
373 }
374
375 protected static String normalizePath(final String path) {
376 if ((path != null) && (path.startsWith("/"))) {
377 if (path.length() > 1) {
378 return path.substring(1);
379 }
380 return "";
381 }
382 return path;
383 }
384 }