1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jelly.tags.swing.converters;
17
18 import javax.swing.DebugGraphics;
19 import java.util.StringTokenizer;
20
21 import org.apache.commons.beanutils.Converter;
22 import org.apache.commons.beanutils.ConvertUtils;
23
24 /***
25 * A Converter that turns Strings in one of the constants of
26 * {@link DebugGraphics} to their appropriate integer constant.
27 *
28 * @author <a href="mailto:paul@activemath.org">Paul Libbrecht</a>
29 * @version $Revision: 155420 $
30 */
31 public class DebugGraphicsConverter implements Converter {
32
33 private static String usageText =
34 "DebugGraphics options are set as a \"|\" separated list of words using one of the constants of DebugGraphics: log, flash, or buffered.";
35
36 public static void register() {
37 ConvertUtils.register(
38 new DebugGraphicsConverter(),
39 java.lang.Integer.class);
40 }
41
42 /*** Part of the Converter interface.
43 * @see org.apache.commons.beanutils.Converter#convert(java.lang.Class, java.lang.Object)
44 */
45 public Object convert(Class type, Object value) {
46 return convert(value);
47 }
48
49 /*** This is not part of the converter interface, it's for use by
50 * classes that don't use DebugGraphicsConverter through BeanUtils.
51 * @param from
52 * @return
53 */
54 public Object convert(Object value) {
55 if (value != null) {
56 int result = 0;
57 StringTokenizer stok =
58 new StringTokenizer(value.toString(), ", \t|", false);
59 while (stok.hasMoreTokens()) {
60 String tok = stok.nextToken();
61 result = result | recognizeOption(tok);
62 }
63 return new Integer(result);
64 }
65 return null;
66 }
67
68 protected int recognizeOption(String value) {
69 value = value.toString().toLowerCase();
70
71 if ("log".equals(value) || "log_option".equals(value)) {
72 return DebugGraphics.LOG_OPTION;
73 }
74 else if ("flash".equals(value) || "flash_option".equals(value)) {
75 return DebugGraphics.FLASH_OPTION;
76 }
77 else if ("buffered".equals(value) || "buffered_option".equals(value)) {
78 return DebugGraphics.BUFFERED_OPTION;
79 }
80 else {
81 throw new IllegalArgumentException(usageText);
82 }
83 }
84 }