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 | 4 | private static final Map<String, Integer> constants = new HashMap<String, Integer>(); |
43 | |
|
44 | 4 | private static final Map<String, Integer> colorConstats = new HashMap<String, Integer>(); |
45 | |
|
46 | |
static { |
47 | 4 | initialize(); |
48 | 4 | } |
49 | |
|
50 | 0 | private SWTUtil() { |
51 | 0 | } |
52 | |
|
53 | |
private static synchronized void initialize() { |
54 | 4 | Field[] fields = SWT.class.getFields(); |
55 | 1568 | for (Field field : fields) { |
56 | 1564 | if (Modifier.isStatic(field.getModifiers()) |
57 | |
&& (field.getType() == Integer.TYPE)) { |
58 | 1540 | String name = field.getName(); |
59 | 1540 | int constant = FieldUtil.getInt(field); |
60 | 1540 | constants.put(name, new Integer(constant)); |
61 | |
|
62 | 1540 | if (name.startsWith("COLOR_")) { |
63 | 140 | String colorName = name.substring(6); |
64 | 140 | colorConstats.put(colorName, new Integer(constant)); |
65 | |
} |
66 | |
} |
67 | |
} |
68 | 4 | } |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
public static String convertConstantName(final String name) { |
82 | 312 | StringTokenizer st = new StringTokenizer(name, "_"); |
83 | 312 | StringBuilder builder = new StringBuilder(""); |
84 | 808 | while (st.hasMoreTokens()) { |
85 | 496 | builder.append(StringUtil.capitalize(st.nextToken().toLowerCase())); |
86 | |
} |
87 | 312 | return builder.toString(); |
88 | |
} |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
public static int getSWTConstant(final String name) { |
98 | 1968 | int constant = SWT.NONE; |
99 | 1968 | Integer constantObj = constants.get(name); |
100 | 1968 | if (constantObj != null) { |
101 | 1944 | constant = constantObj.intValue(); |
102 | |
} |
103 | 1968 | 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 | 2620 | int result = 0; |
124 | 2620 | if (styles != null) { |
125 | 1388 | StringTokenizer st = new StringTokenizer(styles, ","); |
126 | 3184 | while (st.hasMoreTokens()) { |
127 | 1796 | String style = st.nextToken().trim(); |
128 | 1796 | int constant = getSWTConstant(style); |
129 | 1796 | if (constant != SWT.NULL) { |
130 | 1776 | result |= constant; |
131 | |
} |
132 | 1796 | } |
133 | 1388 | } else { |
134 | 1232 | result = defaultStyle; |
135 | |
} |
136 | 2620 | 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 | 884 | 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 | 104 | Color color = ColorManager.getColor(colorString); |
171 | 104 | if (color == null) { |
172 | 56 | String symbolicName = colorString.toUpperCase(); |
173 | 56 | Integer constant = colorConstats.get(symbolicName); |
174 | 56 | if (constant != null) { |
175 | 56 | Display display = Display.getCurrent(); |
176 | 56 | if (display != null) { |
177 | 56 | color = display.getSystemColor(constant.intValue()); |
178 | 56 | ColorManager.putColor(symbolicName, color.getRGB()); |
179 | 56 | color.dispose(); |
180 | 56 | color = ColorManager.getColor(symbolicName); |
181 | |
} |
182 | |
} |
183 | |
} |
184 | 104 | 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 | 112 | if (shell == null) { |
200 | 0 | return; |
201 | |
} |
202 | |
|
203 | 112 | Rectangle bounds = shell.getBounds(); |
204 | |
|
205 | 112 | GC gc = new GC(shell.getDisplay(), SWT.BITMAP); |
206 | 112 | Image image = new Image(shell.getDisplay(), bounds.width, bounds.height); |
207 | 112 | gc.copyArea(image, bounds.x, bounds.y); |
208 | 112 | ImageLoader loader = new ImageLoader(); |
209 | 112 | loader.data = new ImageData[] { image.getImageData() }; |
210 | 112 | loader.save(path, format); |
211 | 112 | image.dispose(); |
212 | 112 | gc.dispose(); |
213 | 112 | } |
214 | |
} |