/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.odysseus.calyxo.forms.convert;
import java.text.Format;
import java.text.ParseException;
import java.text.NumberFormat;
import java.util.Locale;
/**
* Number converter.
*
* @author Christoph Beck
*/
public abstract class NumberConverter extends FormatBasedConverter {
private boolean groupingUsed = true;
private boolean parseIntegerOnly = false;
private int minimumFractionDigits = 0;
private int maximumFractionDigits = 8;
/**
* Default constructor.
*/
protected NumberConverter() {
super();
}
/**
* Create and adapt new number format instance
*/
protected Format createFormat(Locale locale) {
NumberFormat f = NumberFormat.getInstance(locale);
adapt(f);
return f;
}
/**
* Adapt number format instance (sets <code>groupingUsed</code>,
* <code>parseIntegerOnly</code>, <code>minimumFractionDigits</code> and
* <code>maximumFractionDigits</code> properties).
*/
protected void adapt(NumberFormat format) {
format.setGroupingUsed(groupingUsed);
format.setParseIntegerOnly(parseIntegerOnly);
format.setMinimumFractionDigits(minimumFractionDigits);
format.setMaximumFractionDigits(maximumFractionDigits);
}
/**
* Get the maximumFractionDigits property
*/
public int getMaximumFractionDigits() {
return maximumFractionDigits;
}
/**
* Get the minimumFractionDigits property
*/
public int getMinimumFractionDigits() {
return minimumFractionDigits;
}
/**
* Get the parseIntegerOnly property
*/
public boolean isParseIntegerOnly() {
return parseIntegerOnly;
}
/**
* Get the groupingUsed property
*/
public boolean getGroupingUsed() {
return groupingUsed;
}
/**
* Parse method.
*/
public Object parse(String value) throws ParseException {
return super.parse(value == null ? null : value.trim());
}
/**
* Set the maximumFractionDigits property
*/
public void setMaximumFractionDigits(int value) {
maximumFractionDigits = value;
}
/**
* Set the minimumFractionDigits property
*/
public void setMinimumFractionDigits(int value) {
minimumFractionDigits = value;
}
/**
* Set the parseIntegerOnly property
*/
public void setParseIntegerOnly(boolean value) {
parseIntegerOnly = value;
}
/**
* Set the groupingUsed property
*/
public void setGroupingUsed(boolean value) {
groupingUsed = value;
}
}