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