01: /*
02: ItsNat Java Web Application Framework
03: Copyright (C) 2007 Innowhere Software Services S.L., Spanish Company
04: Author: Jose Maria Arranz Santamaria
05:
06: This program is free software: you can redistribute it and/or modify
07: it under the terms of the GNU Affero General Public License as published by
08: the Free Software Foundation, either version 3 of the License, or
09: (at your option) any later version. See the GNU Affero General Public
10: License for more details. See the copy of the GNU Affero General Public License
11: included in this program. If not, see <http://www.gnu.org/licenses/>.
12: */
13:
14: package org.itsnat.impl.comp;
15:
16: import org.itsnat.comp.ItsNatFormattedTextField;
17: import java.lang.reflect.Constructor;
18: import java.text.ParseException;
19:
20: /**
21: *
22: * @author jmarranz
23: */
24: public class ItsNatFormatterDefaultImpl implements
25: ItsNatFormattedTextField.ItsNatFormatter {
26: /** Creates a new instance of ItsNatFormatterDefaultImpl */
27: public ItsNatFormatterDefaultImpl() {
28: }
29:
30: public Class getValueClass(ItsNatFormattedTextField comp) {
31: // Devuelve la clase del valor actual
32: Object value = comp.getValue();
33: if (value != null)
34: return value.getClass();
35: else
36: return null;
37: }
38:
39: public Object stringToValue(String str,
40: ItsNatFormattedTextField comp) throws ParseException {
41: Class vc = getValueClass(comp);
42:
43: if (vc != null) {
44: Constructor cons;
45:
46: try {
47: cons = vc.getConstructor(new Class[] { String.class });
48: } catch (NoSuchMethodException nsme) {
49: cons = null;
50: }
51:
52: if (cons != null) {
53: try {
54: return cons.newInstance(new Object[] { str });
55: } catch (Throwable ex) {
56: throw new ParseException("Error creating instance",
57: 0);
58: }
59: }
60: }
61: return str;
62: }
63:
64: public String valueToString(Object value,
65: ItsNatFormattedTextField comp) throws ParseException {
66: if (value == null) {
67: return "";
68: }
69: return value.toString();
70: }
71:
72: }
|