01: /*
02: * Copyright 2006 Ethan Nicholas. All rights reserved.
03: * Use is subject to license terms.
04: */
05: package jaxx.types;
06:
07: import java.awt.*;
08: import java.util.*;
09:
10: public class InsetsConverter implements TypeConverter {
11: public String getJavaCode(Object object) {
12: Insets insets = (Insets) object;
13: return "new Insets(" + insets.top + ", " + insets.left + ", "
14: + insets.bottom + ", " + insets.right + ")";
15: }
16:
17: public Object convertFromString(String string, Class type) {
18: if (type != Insets.class)
19: throw new IllegalArgumentException("unsupported type: "
20: + type);
21: StringTokenizer tokenizer = new StringTokenizer(string, ",");
22: int count = tokenizer.countTokens();
23: if (count == 1) {
24: int i = Integer.parseInt(tokenizer.nextToken().trim());
25: return new Insets(i, i, i, i);
26: } else if (count == 4) {
27: int[] insets = new int[count];
28: for (int i = 0; tokenizer.hasMoreTokens(); i++)
29: insets[i] = Integer.parseInt(tokenizer.nextToken()
30: .trim());
31: return new Insets(insets[0], insets[1], insets[2],
32: insets[3]);
33: } else
34: throw new IllegalArgumentException(
35: "unable to convert string '" + string
36: + "' to Insets");
37: }
38: }
|