| 1 |
/* |
| 2 |
* @(#) DimensionConverter.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 Dimension to String and converts it back. |
| 12 |
*/ |
| 13 |
public class DimensionConverter extends ArrayConverter { |
| 14 |
DimensionConverter() { |
| 15 |
super("; ", 2, Integer.class); |
| 16 |
} |
| 17 |
|
| 18 |
public String toString(Object object, ConverterContext context) { |
| 19 |
if (object instanceof Dimension) { |
| 20 |
Dimension dim = (Dimension) object; |
| 21 |
return arrayToString(new Object[]{ |
| 22 |
dim.width, dim.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; |
| 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 |
return new Dimension(x, y); |
| 47 |
} |
| 48 |
|
| 49 |
public boolean supportFromString(String string, ConverterContext context) { |
| 50 |
return true; |
| 51 |
} |
| 52 |
} |