1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jelly.tags.swt;
17
18 import java.lang.reflect.Field;
19 import java.util.StringTokenizer;
20
21 import org.apache.commons.jelly.JellyTagException;
22 import org.apache.commons.jelly.tags.core.UseBeanTag;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 /***
27 * A helper class for working with SWT.
28 * </p>
29 *
30 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
31 * @version 1.1
32 */
33 public class SwtHelper extends UseBeanTag {
34
35 /*** The Log to which logging calls will be made. */
36 private static final Log log = LogFactory.getLog(SwtHelper.class);
37
38
39 /***
40 * Parses the comma delimited String of style codes which are or'd
41 * together. The given class describes the integer static constants
42 *
43 * @param constantClass is the type to look for static fields
44 * @param text is a comma delimited text value such as "border, resize"
45 * @return the int code
46 */
47 public static int parseStyle(Class constantClass, String text) throws JellyTagException {
48 return parseStyle(constantClass, text, true);
49 }
50
51 /***
52 * Parses the comma delimited String of style codes which are or'd
53 * together. The given class describes the integer static constants
54 *
55 * @param constantClass is the type to look for static fields
56 * @param text is a comma delimited text value such as "border, resize"
57 * @param toUpperCase is whether the text should be converted to upper case
58 * before its compared against the reflection fields
59 *
60 * @return the int code
61 */
62 public static int parseStyle(Class constantClass, String text, boolean toUpperCase) throws JellyTagException{
63 int answer = 0;
64 if (text != null) {
65 if (toUpperCase) {
66 text = text.toUpperCase();
67 }
68 StringTokenizer items = new StringTokenizer(text, ",");
69 while (items.hasMoreTokens()) {
70 String token = items.nextToken().trim();
71 answer |= getStyleCode(constantClass, token);
72 }
73 }
74 return answer;
75 }
76
77 /***
78 * @return the code for the given word or zero if the word doesn't match a
79 * valid style
80 */
81 public static int getStyleCode(Class constantClass,String text) throws JellyTagException {
82 try {
83 Field field = constantClass.getField(text);
84 if (field == null) {
85 log.warn( "Unknown style code: " + text +" will be ignored");
86 return 0;
87 }
88 return field.getInt(null);
89 } catch (NoSuchFieldException e) {
90 throw new JellyTagException("The value: " + text + " is not understood", e);
91 } catch (IllegalAccessException e) {
92 throw new JellyTagException("The value: " + text + " is not understood", e);
93 }
94 }
95 }