1
2
3
4
5
6
7
8
9
10
11 package com.cloudgarden.resource;
12
13 import java.util.HashMap;
14 import java.util.Iterator;
15 import java.util.Vector;
16
17 import org.eclipse.swt.events.DisposeEvent;
18 import org.eclipse.swt.events.DisposeListener;
19 import org.eclipse.swt.graphics.Color;
20 import org.eclipse.swt.graphics.Cursor;
21 import org.eclipse.swt.graphics.Font;
22 import org.eclipse.swt.graphics.FontData;
23 import org.eclipse.swt.graphics.Image;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Display;
26 import org.eclipse.swt.widgets.Widget;
27
28
29
30
31
32
33
34 @SuppressWarnings("all")
35 public class SWTResourceManager {
36
37 private static HashMap resources = new HashMap();
38
39 private static Vector users = new Vector();
40
41 private static SWTResourceManager instance = new SWTResourceManager();
42
43 private static DisposeListener disposeListener = new DisposeListener() {
44 public void widgetDisposed(DisposeEvent e) {
45 users.remove(e.getSource());
46 if (users.size() == 0) {
47 dispose();
48 }
49 }
50 };
51
52
53
54
55
56
57
58
59
60 public static void registerResourceUser(Widget widget) {
61 if (users.contains(widget)) {
62 return;
63 }
64 users.add(widget);
65 widget.addDisposeListener(disposeListener);
66 }
67
68 public static void dispose() {
69 Iterator it = resources.keySet().iterator();
70 while (it.hasNext()) {
71 Object resource = resources.get(it.next());
72 if (resource instanceof Font) {
73 ((Font)resource).dispose();
74 } else if (resource instanceof Color) {
75 ((Color)resource).dispose();
76 } else if (resource instanceof Image) {
77 ((Image)resource).dispose();
78 } else if (resource instanceof Cursor) {
79 ((Cursor)resource).dispose();
80 }
81 }
82 resources.clear();
83 }
84
85 public static Font getFont(String name, int size, int style) {
86 return getFont(name, size, style, false, false);
87 }
88
89 public static Font getFont(String name, int size, int style, boolean strikeout, boolean underline) {
90 String fontName = name + "|" + size + "|" + style + "|" + strikeout + "|" + underline;
91 if (resources.containsKey(fontName)) {
92 return (Font)resources.get(fontName);
93 }
94 FontData fd = new FontData(name, size, style);
95 if (strikeout || underline) {
96 try {
97 Class lfCls = Class.forName("org.eclipse.swt.internal.win32.LOGFONT");
98 Object lf = FontData.class.getField("data").get(fd);
99 if (lf != null && lfCls != null) {
100 if (strikeout) {
101 lfCls.getField("lfStrikeOut").set(lf, new Byte((byte)1));
102 }
103 if (underline) {
104 lfCls.getField("lfUnderline").set(lf, new Byte((byte)1));
105 }
106 }
107 } catch (Throwable e) {
108 System.err.println("Unable to set underline or strikeout" + " (probably on a non-Windows platform). "
109 + e);
110 }
111 }
112 Font font = new Font(Display.getDefault(), fd);
113 resources.put(fontName, font);
114 return font;
115 }
116
117 public static Image getImage(String url, Control widget) {
118 Image img = getImage(url);
119 if (img != null && widget != null) {
120 img.setBackground(widget.getBackground());
121 }
122 return img;
123 }
124
125 public static Image getImage(String url) {
126 try {
127 url = url.replace('\\', '/');
128 if (url.startsWith("/")) {
129 url = url.substring(1);
130 }
131 if (resources.containsKey(url)) {
132 return (Image)resources.get(url);
133 }
134 Image img = new Image(Display.getDefault(), instance.getClass().getClassLoader().getResourceAsStream(url));
135 if (img != null) {
136 resources.put(url, img);
137 }
138 return img;
139 } catch (Exception e) {
140 System.err.println("SWTResourceManager.getImage: Error getting image " + url + ", " + e);
141 return null;
142 }
143 }
144
145 public static Color getColor(int red, int green, int blue) {
146 String name = "COLOR:" + red + "," + green + "," + blue;
147 if (resources.containsKey(name)) {
148 return (Color)resources.get(name);
149 }
150 Color color = new Color(Display.getDefault(), red, green, blue);
151 resources.put(name, color);
152 return color;
153 }
154
155 public static Cursor getCursor(int type) {
156 String name = "CURSOR:" + type;
157 if (resources.containsKey(name)) {
158 return (Cursor)resources.get(name);
159 }
160 Cursor cursor = new Cursor(Display.getDefault(), type);
161 resources.put(name, cursor);
162 return cursor;
163 }
164
165 }