01: /*
02: * @(#) IntegerConverter.java
03: *
04: * Copyright 2002 - 2003 JIDE Software. All rights reserved.
05: */
06: package com.jidesoft.converter;
07:
08: import java.text.DecimalFormat;
09: import java.text.NumberFormat;
10:
11: /**
12: * Converter which converts Number to String and converts it back. You can pass in
13: * a NumberFormat as UserObject of ConverterContext if you want to control the
14: * format of the number such as maximum decimal point etc.
15: */
16: abstract public class NumberConverter implements ObjectConverter {
17: private NumberFormat _numberFormat;
18:
19: /**
20: * Creates a number converter with no NumberFormat.
21: */
22: public NumberConverter() {
23: }
24:
25: /**
26: * Creates the number converter with a specified NumberFormat.
27: *
28: * @param format the number format.
29: */
30: public NumberConverter(NumberFormat format) {
31: _numberFormat = format;
32: }
33:
34: public String toString(Object object, ConverterContext context) {
35: // format on userOjbect has a higher priority.
36: try {
37: if (context == null
38: || context.getUserObject() == null
39: || !(context.getUserObject() instanceof NumberFormat)) {
40: return getNumberFormat().format(object);
41: } else {
42: NumberFormat format = (NumberFormat) context
43: .getUserObject();
44: return format.format(object);
45: }
46: } catch (IllegalArgumentException e) {
47: return "";
48: }
49: }
50:
51: public boolean supportToString(Object object,
52: ConverterContext context) {
53: return true;
54: }
55:
56: public void setNumberFormat(NumberFormat numberFormat) {
57: _numberFormat = numberFormat;
58: }
59:
60: protected NumberFormat getNumberFormat() {
61: if (_numberFormat == null) {
62: _numberFormat = DecimalFormat.getInstance();
63: }
64: return _numberFormat;
65: }
66: }
|