1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.wb.swt;
12
13 import java.io.FileInputStream;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.util.HashMap;
17 import java.util.Map;
18
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.graphics.Color;
21 import org.eclipse.swt.graphics.Cursor;
22 import org.eclipse.swt.graphics.Font;
23 import org.eclipse.swt.graphics.FontData;
24 import org.eclipse.swt.graphics.GC;
25 import org.eclipse.swt.graphics.Image;
26 import org.eclipse.swt.graphics.ImageData;
27 import org.eclipse.swt.graphics.RGB;
28 import org.eclipse.swt.graphics.Rectangle;
29 import org.eclipse.swt.widgets.Display;
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public class SWTResourceManager {
44
45
46
47
48
49 private static Map<RGB, Color> m_colorMap = new HashMap<RGB, Color>();
50
51
52
53
54
55
56
57 public static Color getColor(int systemColorID) {
58 Display display = Display.getCurrent();
59 return display.getSystemColor(systemColorID);
60 }
61
62
63
64
65
66
67
68
69
70
71
72 public static Color getColor(int r, int g, int b) {
73 return getColor(new RGB(r, g, b));
74 }
75
76
77
78
79
80
81
82 public static Color getColor(RGB rgb) {
83 Color color = m_colorMap.get(rgb);
84 if (color == null) {
85 Display display = Display.getCurrent();
86 color = new Color(display, rgb);
87 m_colorMap.put(rgb, color);
88 }
89 return color;
90 }
91
92
93
94 public static void disposeColors() {
95 for (Color color : m_colorMap.values()) {
96 color.dispose();
97 }
98 m_colorMap.clear();
99 }
100
101
102
103
104
105
106
107
108 private static Map<String, Image> m_imageMap = new HashMap<String, Image>();
109
110
111
112
113
114
115
116 protected static Image getImage(InputStream stream) throws IOException {
117 try {
118 Display display = Display.getCurrent();
119 ImageData data = new ImageData(stream);
120 if (data.transparentPixel > 0) {
121 return new Image(display, data, data.getTransparencyMask());
122 }
123 return new Image(display, data);
124 } finally {
125 stream.close();
126 }
127 }
128
129
130
131
132
133
134
135 public static Image getImage(String path) {
136 Image image = m_imageMap.get(path);
137 if (image == null) {
138 try {
139 image = getImage(new FileInputStream(path));
140 m_imageMap.put(path, image);
141 } catch (Exception e) {
142 image = getMissingImage();
143 m_imageMap.put(path, image);
144 }
145 }
146 return image;
147 }
148
149
150
151
152
153
154
155
156
157 public static Image getImage(Class<?> clazz, String path) {
158 String key = clazz.getName() + '|' + path;
159 Image image = m_imageMap.get(key);
160 if (image == null) {
161 try {
162 image = getImage(clazz.getResourceAsStream(path));
163 m_imageMap.put(key, image);
164 } catch (Exception e) {
165 image = getMissingImage();
166 m_imageMap.put(key, image);
167 }
168 }
169 return image;
170 }
171 private static final int MISSING_IMAGE_SIZE = 10;
172
173
174
175 private static Image getMissingImage() {
176 Image image = new Image(Display.getCurrent(), MISSING_IMAGE_SIZE, MISSING_IMAGE_SIZE);
177
178 GC gc = new GC(image);
179 gc.setBackground(getColor(SWT.COLOR_RED));
180 gc.fillRectangle(0, 0, MISSING_IMAGE_SIZE, MISSING_IMAGE_SIZE);
181 gc.dispose();
182
183 return image;
184 }
185
186
187
188 public static final int TOP_LEFT = 1;
189
190
191
192 public static final int TOP_RIGHT = 2;
193
194
195
196 public static final int BOTTOM_LEFT = 3;
197
198
199
200 public static final int BOTTOM_RIGHT = 4;
201
202
203
204 protected static final int LAST_CORNER_KEY = 5;
205
206
207
208 @SuppressWarnings("unchecked")
209 private static Map<Image, Map<Image, Image>>[] m_decoratedImageMap = new Map[LAST_CORNER_KEY];
210
211
212
213
214
215
216
217
218
219 public static Image decorateImage(Image baseImage, Image decorator) {
220 return decorateImage(baseImage, decorator, BOTTOM_RIGHT);
221 }
222
223
224
225
226
227
228
229
230
231
232
233 public static Image decorateImage(final Image baseImage, final Image decorator, final int corner) {
234 if (corner <= 0 || corner >= LAST_CORNER_KEY) {
235 throw new IllegalArgumentException("Wrong decorate corner");
236 }
237 Map<Image, Map<Image, Image>> cornerDecoratedImageMap = m_decoratedImageMap[corner];
238 if (cornerDecoratedImageMap == null) {
239 cornerDecoratedImageMap = new HashMap<Image, Map<Image, Image>>();
240 m_decoratedImageMap[corner] = cornerDecoratedImageMap;
241 }
242 Map<Image, Image> decoratedMap = cornerDecoratedImageMap.get(baseImage);
243 if (decoratedMap == null) {
244 decoratedMap = new HashMap<Image, Image>();
245 cornerDecoratedImageMap.put(baseImage, decoratedMap);
246 }
247
248 Image result = decoratedMap.get(decorator);
249 if (result == null) {
250 Rectangle bib = baseImage.getBounds();
251 Rectangle dib = decorator.getBounds();
252
253 result = new Image(Display.getCurrent(), bib.width, bib.height);
254
255 GC gc = new GC(result);
256 gc.drawImage(baseImage, 0, 0);
257 if (corner == TOP_LEFT) {
258 gc.drawImage(decorator, 0, 0);
259 } else if (corner == TOP_RIGHT) {
260 gc.drawImage(decorator, bib.width - dib.width, 0);
261 } else if (corner == BOTTOM_LEFT) {
262 gc.drawImage(decorator, 0, bib.height - dib.height);
263 } else if (corner == BOTTOM_RIGHT) {
264 gc.drawImage(decorator, bib.width - dib.width, bib.height - dib.height);
265 }
266 gc.dispose();
267
268 decoratedMap.put(decorator, result);
269 }
270 return result;
271 }
272
273
274
275 public static void disposeImages() {
276
277 {
278 for (Image image : m_imageMap.values()) {
279 image.dispose();
280 }
281 m_imageMap.clear();
282 }
283
284 for (int i = 0; i < m_decoratedImageMap.length; i++) {
285 Map<Image, Map<Image, Image>> cornerDecoratedImageMap = m_decoratedImageMap[i];
286 if (cornerDecoratedImageMap != null) {
287 for (Map<Image, Image> decoratedMap : cornerDecoratedImageMap.values()) {
288 for (Image image : decoratedMap.values()) {
289 image.dispose();
290 }
291 decoratedMap.clear();
292 }
293 cornerDecoratedImageMap.clear();
294 }
295 }
296 }
297
298
299
300
301
302
303
304
305 private static Map<String, Font> m_fontMap = new HashMap<String, Font>();
306
307
308
309 private static Map<Font, Font> m_fontToBoldFontMap = new HashMap<Font, Font>();
310
311
312
313
314
315
316
317
318
319
320
321 public static Font getFont(String name, int height, int style) {
322 return getFont(name, height, style, false, false);
323 }
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340 public static Font getFont(String name, int size, int style, boolean strikeout, boolean underline) {
341 String fontName = name + '|' + size + '|' + style + '|' + strikeout + '|' + underline;
342 Font font = m_fontMap.get(fontName);
343 if (font == null) {
344 FontData fontData = new FontData(name, size, style);
345 if (strikeout || underline) {
346 try {
347 Class<?> logFontClass = Class.forName("org.eclipse.swt.internal.win32.LOGFONT");
348 Object logFont = FontData.class.getField("data").get(fontData);
349 if (logFont != null && logFontClass != null) {
350 if (strikeout) {
351 logFontClass.getField("lfStrikeOut").set(logFont, Byte.valueOf((byte) 1));
352 }
353 if (underline) {
354 logFontClass.getField("lfUnderline").set(logFont, Byte.valueOf((byte) 1));
355 }
356 }
357 } catch (Throwable e) {
358 System.err.println("Unable to set underline or strikeout" + " (probably on a non-Windows platform). " + e);
359 }
360 }
361 font = new Font(Display.getCurrent(), fontData);
362 m_fontMap.put(fontName, font);
363 }
364 return font;
365 }
366
367
368
369
370
371
372
373 public static Font getBoldFont(Font baseFont) {
374 Font font = m_fontToBoldFontMap.get(baseFont);
375 if (font == null) {
376 FontData fontDatas[] = baseFont.getFontData();
377 FontData data = fontDatas[0];
378 font = new Font(Display.getCurrent(), data.getName(), data.getHeight(), SWT.BOLD);
379 m_fontToBoldFontMap.put(baseFont, font);
380 }
381 return font;
382 }
383
384
385
386 public static void disposeFonts() {
387
388 for (Font font : m_fontMap.values()) {
389 font.dispose();
390 }
391 m_fontMap.clear();
392
393 for (Font font : m_fontToBoldFontMap.values()) {
394 font.dispose();
395 }
396 m_fontToBoldFontMap.clear();
397 }
398
399
400
401
402
403
404
405
406 private static Map<Integer, Cursor> m_idToCursorMap = new HashMap<Integer, Cursor>();
407
408
409
410
411
412
413
414 public static Cursor getCursor(int id) {
415 Integer key = Integer.valueOf(id);
416 Cursor cursor = m_idToCursorMap.get(key);
417 if (cursor == null) {
418 cursor = new Cursor(Display.getDefault(), id);
419 m_idToCursorMap.put(key, cursor);
420 }
421 return cursor;
422 }
423
424
425
426 public static void disposeCursors() {
427 for (Cursor cursor : m_idToCursorMap.values()) {
428 cursor.dispose();
429 }
430 m_idToCursorMap.clear();
431 }
432
433
434
435
436
437
438
439
440
441 public static void dispose() {
442 disposeColors();
443 disposeImages();
444 disposeFonts();
445 disposeCursors();
446 }
447 }