| 1 |
/* |
| 2 |
* @(#) DefaultObjectConverter.java |
| 3 |
* |
| 4 |
* Copyright 2002 - 2003 JIDE Software. All rights reserved. |
| 5 |
*/ |
| 6 |
package com.jidesoft.converter; |
| 7 |
|
| 8 |
import javax.swing.*; |
| 9 |
import java.text.ParseException; |
| 10 |
|
| 11 |
|
| 12 |
/** |
| 13 |
* Default object converter. It converts an object to a String using either toString() |
| 14 |
* or the AbstractFormatter specified in the ConverterContex's userObject. |
| 15 |
* <p/> |
| 16 |
* For example, |
| 17 |
* <code><pre> |
| 18 |
* MaskFormatter mask = null; |
| 19 |
* try { |
| 20 |
* mask = new MaskFormatter("###-##-####"); |
| 21 |
* } |
| 22 |
* catch (ParseException e) { |
| 23 |
* e.printStackTrace(); |
| 24 |
* } |
| 25 |
* ConverterContext ssnConverterContext = new ConverterContext("SSN", mask); |
| 26 |
* </pre></code> |
| 27 |
* If so, it will use the MaskFormatter's stringToValue and valueToString methods to do the conversion. |
| 28 |
*/ |
| 29 |
public class DefaultObjectConverter implements ObjectConverter { |
| 30 |
public DefaultObjectConverter() { |
| 31 |
} |
| 32 |
|
| 33 |
public String toString(Object object, ConverterContext context) { |
| 34 |
if (context != null && context.getUserObject() instanceof JFormattedTextField.AbstractFormatter) { |
| 35 |
try { |
| 36 |
return ((JFormattedTextField.AbstractFormatter) context.getUserObject()).valueToString(object); |
| 37 |
} |
| 38 |
catch (ParseException e) { |
| 39 |
// ignore |
| 40 |
} |
| 41 |
} |
| 42 |
return object == null ? "" : object.toString(); |
| 43 |
} |
| 44 |
|
| 45 |
public boolean supportToString(Object object, ConverterContext context) { |
| 46 |
return true; |
| 47 |
} |
| 48 |
|
| 49 |
public Object fromString(String string, ConverterContext context) { |
| 50 |
if (context != null && context.getUserObject() instanceof JFormattedTextField.AbstractFormatter) { |
| 51 |
try { |
| 52 |
return ((JFormattedTextField.AbstractFormatter) context.getUserObject()).stringToValue(string); |
| 53 |
} |
| 54 |
catch (ParseException e) { |
| 55 |
// ignore |
| 56 |
} |
| 57 |
} |
| 58 |
return string; |
| 59 |
} |
| 60 |
|
| 61 |
public boolean supportFromString(String string, ConverterContext context) { |
| 62 |
return true; |
| 63 |
} |
| 64 |
} |