1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jelly.tags.swt.converters;
17
18 import java.util.StringTokenizer;
19
20 import org.apache.commons.beanutils.Converter;
21
22 import org.eclipse.swt.graphics.Point;
23
24 /***
25 * A Converter that turns Strings in the form "x, y" into Point objects
26 *
27 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
28 * @version $Revision: 155420 $
29 */
30 public class PointConverter implements Converter {
31
32 private static final PointConverter instance = new PointConverter();
33
34 public static PointConverter getInstance() {
35 return instance;
36 }
37
38 /***
39 * Parsers a String in the form "x, y" into an SWT Point class
40 * @param text
41 * @return Point
42 */
43 public Point parse(String text) {
44 StringTokenizer items = new StringTokenizer( text, "," );
45 int x = 0;
46 int y = 0;
47 if ( items.hasMoreTokens() ) {
48 x = parseNumber( items.nextToken() );
49 }
50 if ( items.hasMoreTokens() ) {
51 y = parseNumber( items.nextToken() );
52 }
53 return new Point( x, y );
54 }
55
56
57
58 public Object convert(Class type, Object value) {
59 Object answer = null;
60 if ( value != null ) {
61 String text = value.toString();
62 answer = parse(text);
63 }
64
65 System.out.println("Converting value: " + value + " into: " + answer);
66
67 return answer;
68 }
69
70 protected int parseNumber(String text) {
71 text = text.trim();
72 return Integer.parseInt(text.trim());
73 }
74
75 }