01: /*
02: * @(#)NaturalNumberConverter.java 3/9/2005
03: *
04: * Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
05: */
06: package com.jidesoft.converter;
07:
08: import java.text.DecimalFormat;
09: import java.text.NumberFormat;
10: import java.text.ParseException;
11:
12: /**
13: * Converter which converts Integer to String and converts it back.
14: */
15: public class NaturalNumberConverter extends NumberConverter {
16: /**
17: * Default ConverterContext for NaturalNumberConverter.
18: */
19: public static ConverterContext CONTEXT = new ConverterContext(
20: "Natural Nunber");
21:
22: public NaturalNumberConverter() {
23: this (DecimalFormat.getIntegerInstance());
24: }
25:
26: public NaturalNumberConverter(NumberFormat format) {
27: super (format);
28: }
29:
30: public Object fromString(String string, ConverterContext context) {
31: try {
32: int value = getNumberFormat().parse(string).intValue();
33: if (value < 0) {
34: return 0;
35: } else {
36: return Integer.parseInt(string);
37: }
38: } catch (ParseException e) {
39: return null;
40: }
41: }
42:
43: public boolean supportFromString(String string,
44: ConverterContext context) {
45: return true;
46: }
47: }
|