01: /*
02: * @(#)InsetsConverter.java 4/12/2005
03: *
04: * Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
05: */
06: package com.jidesoft.converter;
07:
08: import java.awt.*;
09:
10: /**
11: * Converter which converts Insets to String and converts it back.
12: */
13: public class InsetsConverter extends ArrayConverter {
14: InsetsConverter() {
15: super ("; ", 4, Integer.class);
16: }
17:
18: public String toString(Object object, ConverterContext context) {
19: if (object instanceof Insets) {
20: Insets Insets = (Insets) object;
21: return arrayToString(new Object[] { Insets.top,
22: Insets.left, Insets.bottom, Insets.right }, context);
23: } else {
24: return "";
25: }
26: }
27:
28: public boolean supportToString(Object object,
29: ConverterContext context) {
30: return true;
31: }
32:
33: public Object fromString(String string, ConverterContext context) {
34: if (string == null || string.trim().length() == 0) {
35: return null;
36: }
37: Object[] objects = arrayFromString(string, context);
38: int top = 0, left = 0, bottom = 0, right = 0;
39: if (objects.length >= 1 && objects[0] instanceof Integer) {
40: top = (Integer) objects[0];
41: }
42: if (objects.length >= 2 && objects[1] instanceof Integer) {
43: left = (Integer) objects[1];
44: }
45: if (objects.length >= 3 && objects[2] instanceof Integer) {
46: bottom = (Integer) objects[2];
47: }
48: if (objects.length >= 4 && objects[3] instanceof Integer) {
49: right = (Integer) objects[3];
50: }
51: return new Insets(top, left, bottom, right);
52: }
53:
54: public boolean supportFromString(String string,
55: ConverterContext context) {
56: return true;
57: }
58: }
|