Coverage Report - org.seasar.eclipse.common.util.SWTUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
SWTUtil
95%
60/63
85%
22/26
0
 
 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  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  
      * アンダースコアで区切られた文字列を、単語境界を大文字にした文字列に変換します。<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  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  
      * {@link SWT} クラスの持つ定数を返します。<br>
 92  
      * 
 93  
      * @param name
 94  
      *            定数名
 95  
      * @return 値。存在しない定数名が指定された場合、<code>SWT.NONE</code>を返します。
 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  
      * カンマ区切りの定数から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  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  
      * カンマ区切りの定数から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  884
         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  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  
      * 指定された {@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  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  
 }