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