| 1 |
/* |
| 2 |
* @(#) RectangleConverter.java |
| 3 |
* |
| 4 |
* Copyright 2002 - 2003 JIDE Software. All rights reserved. |
| 5 |
*/ |
| 6 |
package com.jidesoft.converter; |
| 7 |
|
| 8 |
import java.awt.*; |
| 9 |
|
| 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[]{ |
| 22 |
rectangle.x, rectangle.y, rectangle.width, rectangle.height |
| 23 |
}, context); |
| 24 |
} |
| 25 |
else { |
| 26 |
return ""; |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
public boolean supportToString(Object object, 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, ConverterContext context) { |
| 56 |
return true; |
| 57 |
} |
| 58 |
} |