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