01: /**
02: * Copyright 2006 Webmedia Group Ltd.
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: **/package org.araneaframework.uilib.form.converter;
16:
17: import java.math.BigDecimal;
18: import org.araneaframework.uilib.form.Converter;
19: import org.araneaframework.uilib.support.UiLibMessages;
20: import org.araneaframework.uilib.util.MessageUtil;
21:
22: /**
23: * Converts <code>String</code> to <code>BigDecimal</code> and back.
24: *
25: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
26: *
27: */
28: public class StringToBigDecimalConverter extends BaseConverter {
29:
30: /**
31: * Converts <code>String</code> to <code>BigDecimal</code>.
32: */
33: public Object convertNotNull(Object data) {
34: Object result = null;
35: try {
36: result = new BigDecimal((String) data);
37: } catch (NumberFormatException e) {
38: addError(MessageUtil.localizeAndFormat(
39: UiLibMessages.NOT_A_NUMBER, MessageUtil.localize(
40: getLabel(), getEnvironment()),
41: getEnvironment()));
42: }
43: return result;
44: }
45:
46: /**
47: * Converts <code>BigDecimal</code> to <code>String</code>.
48: */
49: public Object reverseConvertNotNull(Object data) {
50: return data.toString();
51: }
52:
53: /**
54: * Returns <code>new StringToBigDecimalConverter()</code>
55: */
56: public Converter newConverter() {
57: return new StringToBigDecimalConverter();
58: }
59: }
|