1 | package de.uka.ipd.sdq.pcm.dialogs; |
2 | |
3 | import java.io.BufferedInputStream; |
4 | import java.io.FileInputStream; |
5 | import java.io.InputStream; |
6 | import java.util.HashMap; |
7 | import java.util.Iterator; |
8 | |
9 | import org.eclipse.swt.SWT; |
10 | import org.eclipse.swt.graphics.Color; |
11 | import org.eclipse.swt.graphics.Cursor; |
12 | import org.eclipse.swt.graphics.Font; |
13 | import org.eclipse.swt.graphics.FontData; |
14 | import org.eclipse.swt.graphics.GC; |
15 | import org.eclipse.swt.graphics.Image; |
16 | import org.eclipse.swt.graphics.ImageData; |
17 | import org.eclipse.swt.graphics.Point; |
18 | import org.eclipse.swt.graphics.RGB; |
19 | import org.eclipse.swt.graphics.Rectangle; |
20 | import org.eclipse.swt.widgets.Canvas; |
21 | import org.eclipse.swt.widgets.Control; |
22 | import org.eclipse.swt.widgets.CoolBar; |
23 | import org.eclipse.swt.widgets.CoolItem; |
24 | import org.eclipse.swt.widgets.Display; |
25 | |
26 | /** |
27 | * Utility class for managing OS resources associated with SWT controls such as |
28 | * colors, fonts, images, etc. |
29 | * |
30 | * !!! IMPORTANT !!! Application code must explicitly invoke the <code>dispose()</code> |
31 | * method to release the operating system resources managed by cached objects |
32 | * when those objects and OS resources are no longer needed (e.g. on |
33 | * application shutdown) |
34 | * |
35 | * This class may be freely distributed as part of any application or plugin. |
36 | * <p> |
37 | * Copyright (c) 2003 - 2005, Instantiations, Inc. <br>All Rights Reserved |
38 | * |
39 | * @author scheglov_ke |
40 | * @author Dan Rubel |
41 | */ |
42 | public class SWTResourceManager { |
43 | |
44 | /** |
45 | * Dispose of cached objects and their underlying OS resources. This should |
46 | * only be called when the cached objects are no longer needed (e.g. on |
47 | * application shutdown) |
48 | */ |
49 | public static void dispose() { |
50 | disposeColors(); |
51 | disposeFonts(); |
52 | disposeImages(); |
53 | disposeCursors(); |
54 | } |
55 | |
56 | ////////////////////////////// |
57 | // Color support |
58 | ////////////////////////////// |
59 | |
60 | /** |
61 | * Maps RGB values to colors |
62 | */ |
63 | private static HashMap<RGB, Color> m_ColorMap = new HashMap<RGB, Color>(); |
64 | |
65 | /** |
66 | * Returns the system color matching the specific ID |
67 | * @param systemColorID int The ID value for the color |
68 | * @return Color The system color matching the specific ID |
69 | */ |
70 | public static Color getColor(int systemColorID) { |
71 | Display display = Display.getCurrent(); |
72 | return display.getSystemColor(systemColorID); |
73 | } |
74 | |
75 | /** |
76 | * Returns a color given its red, green and blue component values |
77 | * @param r int The red component of the color |
78 | * @param g int The green component of the color |
79 | * @param b int The blue component of the color |
80 | * @return Color The color matching the given red, green and blue componet values |
81 | */ |
82 | public static Color getColor(int r, int g, int b) { |
83 | return getColor(new RGB(r, g, b)); |
84 | } |
85 | |
86 | /** |
87 | * Returns a color given its RGB value |
88 | * @param rgb RGB The RGB value of the color |
89 | * @return Color The color matching the RGB value |
90 | */ |
91 | public static Color getColor(RGB rgb) { |
92 | Color color = m_ColorMap.get(rgb); |
93 | if (color == null) { |
94 | Display display = Display.getCurrent(); |
95 | color = new Color(display, rgb); |
96 | m_ColorMap.put(rgb, color); |
97 | } |
98 | return color; |
99 | } |
100 | |
101 | /** |
102 | * Dispose of all the cached colors |
103 | */ |
104 | public static void disposeColors() { |
105 | for (Iterator<Color> iter = m_ColorMap.values().iterator(); iter.hasNext();) |
106 | ((Color) iter.next()).dispose(); |
107 | m_ColorMap.clear(); |
108 | } |
109 | |
110 | ////////////////////////////// |
111 | // Image support |
112 | ////////////////////////////// |
113 | |
114 | /** |
115 | * Maps image names to images |
116 | */ |
117 | private static HashMap<String, Image> m_ClassImageMap = new HashMap<String, Image>(); |
118 | |
119 | /** |
120 | * Maps images to image decorators |
121 | */ |
122 | private static HashMap<Image, HashMap<Image, Image>> m_ImageToDecoratorMap = new HashMap<Image, HashMap<Image, Image>>(); |
123 | |
124 | /** |
125 | * Returns an image encoded by the specified input stream |
126 | * @param is InputStream The input stream encoding the image data |
127 | * @return Image The image encoded by the specified input stream |
128 | */ |
129 | protected static Image getImage(InputStream is) { |
130 | Display display = Display.getCurrent(); |
131 | ImageData data = new ImageData(is); |
132 | if (data.transparentPixel > 0) |
133 | return new Image(display, data, data.getTransparencyMask()); |
134 | return new Image(display, data); |
135 | } |
136 | |
137 | /** |
138 | * Returns an image stored in the file at the specified path |
139 | * @param path String The path to the image file |
140 | * @return Image The image stored in the file at the specified path |
141 | */ |
142 | public static Image getImage(String path) { |
143 | return getImage("default", path); //$NON-NLS-1$ |
144 | } |
145 | |
146 | /** |
147 | * Returns an image stored in the file at the specified path |
148 | * @param section The section to which belongs specified image |
149 | * @param path String The path to the image file |
150 | * @return Image The image stored in the file at the specified path |
151 | */ |
152 | public static Image getImage(String section, String path) { |
153 | String key = section + '|' + SWTResourceManager.class.getName() + '|' + path; |
154 | Image image = m_ClassImageMap.get(key); |
155 | if (image == null) { |
156 | try { |
157 | FileInputStream fis = new FileInputStream(path); |
158 | image = getImage(fis); |
159 | m_ClassImageMap.put(key, image); |
160 | fis.close(); |
161 | } catch (Exception e) { |
162 | image = getMissingImage(); |
163 | m_ClassImageMap.put(key, image); |
164 | } |
165 | } |
166 | return image; |
167 | } |
168 | |
169 | /** |
170 | * Returns an image stored in the file at the specified path relative to the specified class |
171 | * @param clazz Class The class relative to which to find the image |
172 | * @param path String The path to the image file |
173 | * @return Image The image stored in the file at the specified path |
174 | */ |
175 | public static Image getImage(Class<?> clazz, String path) { |
176 | String key = clazz.getName() + '|' + path; |
177 | Image image = m_ClassImageMap.get(key); |
178 | if (image == null) { |
179 | try { |
180 | if (path.length() > 0 && path.charAt(0) == '/') { |
181 | String newPath = path.substring(1, path.length()); |
182 | image = getImage(new BufferedInputStream(clazz.getClassLoader().getResourceAsStream(newPath))); |
183 | } else { |
184 | image = getImage(clazz.getResourceAsStream(path)); |
185 | } |
186 | m_ClassImageMap.put(key, image); |
187 | } catch (Exception e) { |
188 | image = getMissingImage(); |
189 | m_ClassImageMap.put(key, image); |
190 | } |
191 | } |
192 | return image; |
193 | } |
194 | |
195 | private static final int MISSING_IMAGE_SIZE = 10; |
196 | private static Image getMissingImage() { |
197 | Image image = new Image(Display.getCurrent(), MISSING_IMAGE_SIZE, MISSING_IMAGE_SIZE); |
198 | // |
199 | GC gc = new GC(image); |
200 | gc.setBackground(getColor(SWT.COLOR_RED)); |
201 | gc.fillRectangle(0, 0, MISSING_IMAGE_SIZE, MISSING_IMAGE_SIZE); |
202 | gc.dispose(); |
203 | // |
204 | return image; |
205 | } |
206 | |
207 | /** |
208 | * Style constant for placing decorator image in top left corner of base image. |
209 | */ |
210 | public static final int TOP_LEFT = 1; |
211 | /** |
212 | * Style constant for placing decorator image in top right corner of base image. |
213 | */ |
214 | public static final int TOP_RIGHT = 2; |
215 | /** |
216 | * Style constant for placing decorator image in bottom left corner of base image. |
217 | */ |
218 | public static final int BOTTOM_LEFT = 3; |
219 | /** |
220 | * Style constant for placing decorator image in bottom right corner of base image. |
221 | */ |
222 | public static final int BOTTOM_RIGHT = 4; |
223 | |
224 | /** |
225 | * Returns an image composed of a base image decorated by another image |
226 | * @param baseImage Image The base image that should be decorated |
227 | * @param decorator Image The image to decorate the base image |
228 | * @return Image The resulting decorated image |
229 | */ |
230 | public static Image decorateImage(Image baseImage, Image decorator) { |
231 | return decorateImage(baseImage, decorator, BOTTOM_RIGHT); |
232 | } |
233 | |
234 | /** |
235 | * Returns an image composed of a base image decorated by another image |
236 | * @param baseImage Image The base image that should be decorated |
237 | * @param decorator Image The image to decorate the base image |
238 | * @param corner The corner to place decorator image |
239 | * @return Image The resulting decorated image |
240 | */ |
241 | public static Image decorateImage(Image baseImage, Image decorator, int corner) { |
242 | HashMap<Image, Image> decoratedMap = m_ImageToDecoratorMap.get(baseImage); |
243 | if (decoratedMap == null) { |
244 | decoratedMap = new HashMap<Image, Image>(); |
245 | m_ImageToDecoratorMap.put(baseImage, decoratedMap); |
246 | } |
247 | Image result = decoratedMap.get(decorator); |
248 | if (result == null) { |
249 | Rectangle bid = baseImage.getBounds(); |
250 | Rectangle did = decorator.getBounds(); |
251 | result = new Image(Display.getCurrent(), bid.width, bid.height); |
252 | GC gc = new GC(result); |
253 | gc.drawImage(baseImage, 0, 0); |
254 | // |
255 | if (corner == TOP_LEFT) { |
256 | gc.drawImage(decorator, 0, 0); |
257 | } else if (corner == TOP_RIGHT) { |
258 | gc.drawImage(decorator, bid.width - did.width - 1, 0); |
259 | } else if (corner == BOTTOM_LEFT) { |
260 | gc.drawImage(decorator, 0, bid.height - did.height - 1); |
261 | } else if (corner == BOTTOM_RIGHT) { |
262 | gc.drawImage(decorator, bid.width - did.width - 1, bid.height - did.height - 1); |
263 | } |
264 | // |
265 | gc.dispose(); |
266 | decoratedMap.put(decorator, result); |
267 | } |
268 | return result; |
269 | } |
270 | |
271 | /** |
272 | * Dispose all of the cached images |
273 | */ |
274 | public static void disposeImages() { |
275 | for (Iterator<Image> I = m_ClassImageMap.values().iterator(); I.hasNext();) |
276 | ((Image) I.next()).dispose(); |
277 | m_ClassImageMap.clear(); |
278 | // |
279 | for (Iterator<HashMap<Image, Image>> I = m_ImageToDecoratorMap.values().iterator(); I.hasNext();) { |
280 | HashMap<?, ?> decoratedMap = (HashMap<?, ?>) I.next(); |
281 | for (Iterator<?> J = decoratedMap.values().iterator(); J.hasNext();) { |
282 | Image image = (Image) J.next(); |
283 | image.dispose(); |
284 | } |
285 | } |
286 | } |
287 | |
288 | /** |
289 | * Dispose cached images in specified section |
290 | * @param section the section do dispose |
291 | */ |
292 | public static void disposeImages(String section) { |
293 | for (Iterator<String> I = m_ClassImageMap.keySet().iterator(); I.hasNext();) { |
294 | String key = (String) I.next(); |
295 | if (!key.startsWith(section + '|')) |
296 | continue; |
297 | Image image = m_ClassImageMap.get(key); |
298 | image.dispose(); |
299 | I.remove(); |
300 | } |
301 | } |
302 | |
303 | ////////////////////////////// |
304 | // Font support |
305 | ////////////////////////////// |
306 | |
307 | /** |
308 | * Maps font names to fonts |
309 | */ |
310 | private static HashMap<String, Font> m_FontMap = new HashMap<String, Font>(); |
311 | |
312 | /** |
313 | * Maps fonts to their bold versions |
314 | */ |
315 | private static HashMap<Font, Font> m_FontToBoldFontMap = new HashMap<Font, Font>(); |
316 | |
317 | /** |
318 | * Returns a font based on its name, height and style |
319 | * @param name String The name of the font |
320 | * @param height int The height of the font |
321 | * @param style int The style of the font |
322 | * @return Font The font matching the name, height and style |
323 | */ |
324 | public static Font getFont(String name, int height, int style) { |
325 | return getFont(name, height, style, false, false); |
326 | } |
327 | |
328 | |
329 | /** |
330 | * Returns a font based on its name, height and style. |
331 | * Windows-specific strikeout and underline flags are also supported. |
332 | * @param name String The name of the font |
333 | * @param size int The size of the font |
334 | * @param style int The style of the font |
335 | * @param strikeout boolean The strikeout flag (warning: Windows only) |
336 | * @param underline boolean The underline flag (warning: Windows only) |
337 | * @return Font The font matching the name, height, style, strikeout and underline |
338 | */ |
339 | public static Font getFont(String name, int size, int style, boolean strikeout, boolean underline) { |
340 | String fontName = name + '|' + size + '|' + style + '|' + strikeout + '|' + underline; |
341 | Font font = m_FontMap.get(fontName); |
342 | if (font == null) { |
343 | FontData fontData = new FontData(name, size, style); |
344 | if (strikeout || underline) { |
345 | try { |
346 | Class<?> logFontClass = Class.forName("org.eclipse.swt.internal.win32.LOGFONT"); //$NON-NLS-1$ |
347 | Object logFont = FontData.class.getField("data").get(fontData); //$NON-NLS-1$ |
348 | if (logFont != null && logFontClass != null) { |
349 | if (strikeout) { |
350 | logFontClass.getField("lfStrikeOut").set(logFont, new Byte((byte) 1)); //$NON-NLS-1$ |
351 | } |
352 | if (underline) { |
353 | logFontClass.getField("lfUnderline").set(logFont, new Byte((byte) 1)); //$NON-NLS-1$ |
354 | } |
355 | } |
356 | } catch (Throwable e) { |
357 | System.err.println( |
358 | "Unable to set underline or strikeout" + " (probably on a non-Windows platform). " + e); //$NON-NLS-1$ //$NON-NLS-2$ |
359 | } |
360 | } |
361 | font = new Font(Display.getCurrent(), fontData); |
362 | m_FontMap.put(fontName, font); |
363 | } |
364 | return font; |
365 | } |
366 | |
367 | |
368 | /** |
369 | * Return a bold version of the give font |
370 | * @param baseFont Font The font for whoch a bold version is desired |
371 | * @return Font The bold version of the give font |
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 | * Dispose all of the cached fonts |
386 | */ |
387 | public static void disposeFonts() { |
388 | for (Iterator<Font> iter = m_FontMap.values().iterator(); iter.hasNext();) |
389 | ((Font) iter.next()).dispose(); |
390 | m_FontMap.clear(); |
391 | } |
392 | |
393 | ////////////////////////////// |
394 | // CoolBar support |
395 | ////////////////////////////// |
396 | |
397 | /** |
398 | * Fix the layout of the specified CoolBar |
399 | * @param bar CoolBar The CoolBar that shgoud be fixed |
400 | */ |
401 | public static void fixCoolBarSize(CoolBar bar) { |
402 | CoolItem[] items = bar.getItems(); |
403 | // ensure that each item has control (at least empty one) |
404 | for (int i = 0; i < items.length; i++) { |
405 | CoolItem item = items[i]; |
406 | if (item.getControl() == null) |
407 | item.setControl(new Canvas(bar, SWT.NONE) { |
408 | @Override |
409 | public Point computeSize(int wHint, int hHint, boolean changed) { |
410 | return new Point(20, 20); |
411 | } |
412 | }); |
413 | } |
414 | // compute size for each item |
415 | for (int i = 0; i < items.length; i++) { |
416 | CoolItem item = items[i]; |
417 | Control control = item.getControl(); |
418 | control.pack(); |
419 | Point size = control.getSize(); |
420 | item.setSize(item.computeSize(size.x, size.y)); |
421 | } |
422 | } |
423 | |
424 | ////////////////////////////// |
425 | // Cursor support |
426 | ////////////////////////////// |
427 | |
428 | /** |
429 | * Maps IDs to cursors |
430 | */ |
431 | private static HashMap<Integer, Cursor> m_IdToCursorMap = new HashMap<Integer, Cursor>(); |
432 | |
433 | /** |
434 | * Returns the system cursor matching the specific ID |
435 | * @param id int The ID value for the cursor |
436 | * @return Cursor The system cursor matching the specific ID |
437 | */ |
438 | public static Cursor getCursor(int id) { |
439 | Integer key = new Integer(id); |
440 | Cursor cursor = m_IdToCursorMap.get(key); |
441 | if (cursor == null) { |
442 | cursor = new Cursor(Display.getDefault(), id); |
443 | m_IdToCursorMap.put(key, cursor); |
444 | } |
445 | return cursor; |
446 | } |
447 | |
448 | /** |
449 | * Dispose all of the cached cursors |
450 | */ |
451 | public static void disposeCursors() { |
452 | for (Iterator<Cursor> iter = m_IdToCursorMap.values().iterator(); iter.hasNext();) |
453 | ((Cursor) iter.next()).dispose(); |
454 | m_IdToCursorMap.clear(); |
455 | } |
456 | } |