1 /*
2 * Copyright 2004-2008 the Seasar Foundation and the Others.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13 * either express or implied. See the License for the specific language
14 * governing permissions and limitations under the License.
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 * {@link SWT} クラスの定数を扱うためのユーティリティクラスです。<br />
38 *
39 * @author y-komori
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 * アンダースコアで区切られた文字列を、単語境界を大文字にした文字列に変換します。<br />
72 * <p>
73 * 【例】<br />
74 * MOUSE_DOUBLE_CLICK -> mouseDoubleClick
75 * </p>
76 *
77 * @param name
78 * 変換対象
79 * @return 変換結果
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 * {@link SWT} クラスの持つ定数を返します。<br>
92 *
93 * @param name
94 * 定数名
95 * @return 値。存在しない定数名が指定された場合、<code>SWT.NONE</code>を返します。
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 * カンマ区切りの定数からSWTのスタイルを計算します。<br>
108 * 例えば以下のような入力に対して、本メソッドは
109 * <code>SWT.HORIZONTAL | SWT.SHADOW_IN | SWT.CENTER</code>の計算結果を
110 * 戻り値として返します。<br>
111 * {@link SWT} クラスに定義されていない定数が指定された場合、無視されます。
112 *
113 * 入力例:<code>"HORIZONTAL, SHADOW_IN, CENTER"</code><br>
114 *
115 * @param styles
116 * カンマ区切りの定数。
117 * @param defaultStyle
118 * <code>styles</code> が <code>null</code> だった場合に返すデフォルト値。
119 * @return スタイル値。 引数が <code>null</code> の場合は <code>defalutStyle</code>
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 * カンマ区切りの定数からSWTのスタイルを計算します。<br>
141 * 例えば以下のような入力に対して、本メソッドは
142 * <code>SWT.HORIZONTAL | SWT.SHADOW_IN | SWT.CENTER</code>の計算結果を
143 * 戻り値として返します。<br>
144 * {@link SWT} クラスに定義されていない定数が指定された場合、無視されます。
145 *
146 * 入力例:<code>"HORIZONTAL, SHADOW_IN, CENTER"</code><br>
147 *
148 * @param styles
149 * カンマ区切りの定数。
150 * @return スタイル値。 引数が <code>null</code> の場合は <code>SWT.NONE</code>
151 * を返します。
152 */
153 public static int getStyle(final String styles) {
154 return getStyle(styles, SWT.NONE);
155 }
156
157 /**
158 * {@link Color} オブジェクトを生成します。<br>
159 * <code>colorString</code> で指定された文字列から {@link Color} オブジェクトを生成します。<br>
160 * <code>colorString</code> は #RGB 形式または <code>red</code>、<code>blue</code>
161 * 等 {@link SWT} クラスの <code>COLOR_*</code>
162 * 定数で用意された文字列が指定できます(いずれも、大文字・小文字どちらも使用可能)。<br>
163 * 例: <code>#FF0000</code> を指定した場合、赤を表します。
164 *
165 * @param colorString
166 * 色を表す文字列。
167 * @return <code>Color</code> オブジェクト
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 * 指定された {@link Shell} のイメージをキャプチャしてファイルへ保存します。<br />
189 *
190 * @param shell
191 * キャプチャ対象の {@link Shell} オブジェクト
192 * @param path
193 * 保存先パス
194 * @param format
195 * フォーマット({@link ImageLoader#save(String, int)} メソッドのドキュメントを参照)
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 }