01: /*
02: * Copyright 2004, 2005, 2006 Odysseus Software GmbH
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package de.odysseus.calyxo.forms.convert;
17:
18: import java.text.DecimalFormat;
19: import java.text.Format;
20: import java.text.ParseException;
21:
22: import java.math.BigDecimal;
23:
24: import org.apache.commons.logging.Log;
25: import org.apache.commons.logging.LogFactory;
26:
27: import de.odysseus.calyxo.base.util.PropertyUtils;
28:
29: /**
30: * BigDecimal converter.
31: *
32: * @author Christoph Beck
33: * @author Oliver Stuhr
34: */
35: public class BigDecimalConverter extends NumberConverter {
36: private static final boolean parseBigDecimal = PropertyUtils
37: .getPropertyDescriptor(DecimalFormat.class,
38: "parseBigDecimal") != null;
39:
40: static {
41: Log log = LogFactory.getLog(BigDecimalConverter.class);
42: if (!parseBigDecimal) {
43: log
44: .warn("DecimalFormat.parseBigDecimal not available, will parse via double.");
45: }
46: }
47:
48: /**
49: * Default constructor.
50: */
51: public BigDecimalConverter() {
52: super ();
53: }
54:
55: protected void setFormat(Format format) {
56: if (format instanceof DecimalFormat && parseBigDecimal) {
57: try {
58: PropertyUtils.setProperty(format, "parseBigDecimal",
59: Boolean.TRUE);
60: } catch (Exception e) {
61: // should not happen
62: }
63: }
64: super .setFormat(format);
65: }
66:
67: public BigDecimal getDefault() {
68: return (BigDecimal) getParsedNullOrEmpty();
69: }
70:
71: public void setDefault(BigDecimal value) {
72: setParsedNullOrEmpty(value);
73: }
74:
75: /**
76: * Format method.
77: */
78: public String format(Object value) {
79: return super .format((BigDecimal) value);
80: }
81:
82: /**
83: * Parse method.
84: */
85: public Object parse(String value) throws ParseException {
86: Number d = (Number) super .parse(value);
87: if (d == null || d instanceof BigDecimal)
88: return d;
89: return new BigDecimal(d.doubleValue());
90: }
91: }
|