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.lang.reflect.Field;
19 import java.lang.reflect.Modifier;
20 import java.util.HashMap;
21 import java.util.Map;
22 import java.util.StringTokenizer;
23
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.graphics.Color;
26 import org.eclipse.swt.graphics.GC;
27 import org.eclipse.swt.graphics.Image;
28 import org.eclipse.swt.graphics.ImageData;
29 import org.eclipse.swt.graphics.ImageLoader;
30 import org.eclipse.swt.graphics.Rectangle;
31 import org.eclipse.swt.widgets.Display;
32 import org.eclipse.swt.widgets.Shell;
33 import org.seasar.framework.util.FieldUtil;
34 import org.seasar.framework.util.StringUtil;
35
36
37
38
39
40
41 public class SWTUtil {
42 private static final Map<String, Integer> constants = new HashMap<String, Integer>();
43
44 private static final Map<String, Integer> colorConstats = new HashMap<String, Integer>();
45
46 static {
47 initialize();
48 }
49
50 private SWTUtil() {
51 }
52
53 private static synchronized void initialize() {
54 Field[] fields = SWT.class.getFields();
55 for (Field field : fields) {
56 if (Modifier.isStatic(field.getModifiers())
57 && (field.getType() == Integer.TYPE)) {
58 String name = field.getName();
59 int constant = FieldUtil.getInt(field);
60 constants.put(name, new Integer(constant));
61
62 if (name.startsWith("COLOR_")) {
63 String colorName = name.substring(6);
64 colorConstats.put(colorName, new Integer(constant));
65 }
66 }
67 }
68 }
69
70
71
72
73
74
75
76
77
78
79
80
81 public static String convertConstantName(final String name) {
82 StringTokenizer st = new StringTokenizer(name, "_");
83 StringBuilder builder = new StringBuilder("");
84 while (st.hasMoreTokens()) {
85 builder.append(StringUtil.capitalize(st.nextToken().toLowerCase()));
86 }
87 return builder.toString();
88 }
89
90
91
92
93
94
95
96
97 public static int getSWTConstant(final String name) {
98 int constant = SWT.NONE;
99 Integer constantObj = constants.get(name);
100 if (constantObj != null) {
101 constant = constantObj.intValue();
102 }
103 return constant;
104 }
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122 public static int getStyle(final String styles, final int defaultStyle) {
123 int result = 0;
124 if (styles != null) {
125 StringTokenizer st = new StringTokenizer(styles, ",");
126 while (st.hasMoreTokens()) {
127 String style = st.nextToken().trim();
128 int constant = getSWTConstant(style);
129 if (constant != SWT.NULL) {
130 result |= constant;
131 }
132 }
133 } else {
134 result = defaultStyle;
135 }
136 return result;
137 }
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153 public static int getStyle(final String styles) {
154 return getStyle(styles, SWT.NONE);
155 }
156
157
158
159
160
161
162
163
164
165
166
167
168
169 public static Color getColor(final String colorString) {
170 Color color = ColorManager.getColor(colorString);
171 if (color == null) {
172 String symbolicName = colorString.toUpperCase();
173 Integer constant = colorConstats.get(symbolicName);
174 if (constant != null) {
175 Display display = Display.getCurrent();
176 if (display != null) {
177 color = display.getSystemColor(constant.intValue());
178 ColorManager.putColor(symbolicName, color.getRGB());
179 color.dispose();
180 color = ColorManager.getColor(symbolicName);
181 }
182 }
183 }
184 return color;
185 }
186
187
188
189
190
191
192
193
194
195
196
197 public static void saveWindowImage(final Shell shell, final String path,
198 final int format) {
199 if (shell == null) {
200 return;
201 }
202
203 Rectangle bounds = shell.getBounds();
204
205 GC gc = new GC(shell.getDisplay(), SWT.BITMAP);
206 Image image = new Image(shell.getDisplay(), bounds.width, bounds.height);
207 gc.copyArea(image, bounds.x, bounds.y);
208 ImageLoader loader = new ImageLoader();
209 loader.data = new ImageData[] { image.getImageData() };
210 loader.save(path, format);
211 image.dispose();
212 gc.dispose();
213 }
214 }