001: /*
002: * Copyright 2004, 2005, 2006 Odysseus Software GmbH
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package de.odysseus.calyxo.forms.convert;
017:
018: import java.text.Format;
019: import java.text.ParseException;
020: import java.text.NumberFormat;
021:
022: import java.util.Locale;
023:
024: /**
025: * Number converter.
026: *
027: * @author Christoph Beck
028: */
029: public abstract class NumberConverter extends FormatBasedConverter {
030: private boolean groupingUsed = true;
031: private boolean parseIntegerOnly = false;
032: private int minimumFractionDigits = 0;
033: private int maximumFractionDigits = 8;
034:
035: /**
036: * Default constructor.
037: */
038: protected NumberConverter() {
039: super ();
040: }
041:
042: /**
043: * Create and adapt new number format instance
044: */
045: protected Format createFormat(Locale locale) {
046: NumberFormat f = NumberFormat.getInstance(locale);
047: adapt(f);
048: return f;
049: }
050:
051: /**
052: * Adapt number format instance (sets <code>groupingUsed</code>,
053: * <code>parseIntegerOnly</code>, <code>minimumFractionDigits</code> and
054: * <code>maximumFractionDigits</code> properties).
055: */
056: protected void adapt(NumberFormat format) {
057: format.setGroupingUsed(groupingUsed);
058: format.setParseIntegerOnly(parseIntegerOnly);
059: format.setMinimumFractionDigits(minimumFractionDigits);
060: format.setMaximumFractionDigits(maximumFractionDigits);
061: }
062:
063: /**
064: * Get the maximumFractionDigits property
065: */
066: public int getMaximumFractionDigits() {
067: return maximumFractionDigits;
068: }
069:
070: /**
071: * Get the minimumFractionDigits property
072: */
073: public int getMinimumFractionDigits() {
074: return minimumFractionDigits;
075: }
076:
077: /**
078: * Get the parseIntegerOnly property
079: */
080: public boolean isParseIntegerOnly() {
081: return parseIntegerOnly;
082: }
083:
084: /**
085: * Get the groupingUsed property
086: */
087: public boolean getGroupingUsed() {
088: return groupingUsed;
089: }
090:
091: /**
092: * Parse method.
093: */
094: public Object parse(String value) throws ParseException {
095: return super .parse(value == null ? null : value.trim());
096: }
097:
098: /**
099: * Set the maximumFractionDigits property
100: */
101: public void setMaximumFractionDigits(int value) {
102: maximumFractionDigits = value;
103: }
104:
105: /**
106: * Set the minimumFractionDigits property
107: */
108: public void setMinimumFractionDigits(int value) {
109: minimumFractionDigits = value;
110: }
111:
112: /**
113: * Set the parseIntegerOnly property
114: */
115: public void setParseIntegerOnly(boolean value) {
116: parseIntegerOnly = value;
117: }
118:
119: /**
120: * Set the groupingUsed property
121: */
122: public void setGroupingUsed(boolean value) {
123: groupingUsed = value;
124: }
125: }
|