blob: a703b8fcf86544d44facfd85937393a24bb9588f [file] [log] [blame]
/**
* Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Florian Pirchner - Initial implementation
*/
package org.eclipse.osbp.runtime.web.vaadin.components.converter;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale;
import org.apache.commons.lang.StringEscapeUtils;
import com.vaadin.data.util.converter.StringToDoubleConverter;
// TODO: Auto-generated Javadoc
/**
* A converter used to format and parse Decimal values.
*/
@SuppressWarnings("serial")
public class DecimalDoubleConverter extends StringToDoubleConverter {
/** The integer instance. */
private boolean integerInstance;
/** The number format pattern. */
private String numberFormatPattern;
/** The use grouping. */
private boolean useGrouping;
/** The decimal format symbols. */
private DecimalFormatSymbols decimalFormatSymbols;
/** The custom format symbols. */
private boolean customFormatSymbols;
/** The precision. */
private int precision;
/**
* Instantiates a new decimal converter.
*/
public DecimalDoubleConverter() {
this(false);
}
/** Returns the default value for default format. */
protected String defaultFormat = "##,##0.00";
/** Returns the default value for precision. */
protected int defaultPrecision = 2;
/**
* Instantiates a new decimal converter.
*
* @param integerInstance
* the integer instance
*/
public DecimalDoubleConverter(boolean integerInstance) {
this.integerInstance = integerInstance;
this.numberFormatPattern = defaultFormat;
this.decimalFormatSymbols = getDefaultFormatSymbols();
this.precision = defaultPrecision;
this.useGrouping = getDefaultUseGrouping();
}
/**
* Returns the default value for use grouping.
*
* @return the default use grouping
*/
protected boolean getDefaultUseGrouping() {
return true;
}
/**
* Returns the default value for format symbols.
*
* @return the default format symbols
*/
protected DecimalFormatSymbols getDefaultFormatSymbols() {
return new DecimalFormatSymbols();
}
/**
* Sets the number format pattern that should be used to format the number.
*
* @param numberFormatPattern
* the numberFormatPattern to set
*/
protected void setNumberFormatPattern(String numberFormatPattern) {
this.numberFormatPattern = numberFormatPattern;
}
/**
* Sets the {@link DecimalFormatSymbols} that should be used by the
* formatter.
*
* @param decimalFormatSymbols
* the decimalFormatSymbols to set
*/
public void setDecimalFormatSymbols(
DecimalFormatSymbols decimalFormatSymbols) {
this.decimalFormatSymbols = decimalFormatSymbols;
if (decimalFormatSymbols != null) {
customFormatSymbols = true;
} else {
customFormatSymbols = false;
}
}
/**
* Returns the currently used number format pattern.
*
* @return the number format pattern
*/
public String getNumberFormatPattern() {
return numberFormatPattern;
}
/**
* Returns the currently used format symbols.
*
* @return the decimal format symbols
*/
public DecimalFormatSymbols getDecimalFormatSymbols() {
return decimalFormatSymbols;
}
/**
* If true, then grouping should be used. False otherwise. Default is true.
*
* @return true, if is use grouping
*/
public boolean isUseGrouping() {
return useGrouping;
}
/**
* If true, then grouping should be used. False otherwise. Default is true.
*
* @param useGrouping
* the new use grouping
*/
public void setUseGrouping(boolean useGrouping) {
this.useGrouping = useGrouping;
}
/**
* Returns the precision of that decimal field.
*
* @return the precision
*/
public int getPrecision() {
return precision;
}
/**
* Sets the precision of that decimal field.
*
* @param precision
* the new precision
*/
public void setPrecision(int precision) {
this.precision = precision;
updateNumberFormat();
}
/**
* Sets the number format pattern to be used for formatting.
*/
protected void updateNumberFormat() {
String format = "##,##0";
if (precision > 0) {
format = format.concat(".");
}
for (int i = 0; i < precision; i++) {
format = format.concat("0");
}
setNumberFormatPattern(format);
}
/*
* (non-Javadoc)
*
* @see
* com.vaadin.data.util.converter.AbstractStringToNumberConverter#getFormat
* (java.util.Locale)
*/
@Override
protected NumberFormat getFormat(Locale locale) {
if (locale == null) {
locale = Locale.getDefault();
}
NumberFormat result = null;
if (numberFormatPattern != null && !numberFormatPattern.equals("")) {
try {
if (decimalFormatSymbols != null && customFormatSymbols) {
result = new DecimalFormat(
StringEscapeUtils.unescapeHtml(numberFormatPattern),
decimalFormatSymbols);
} else {
result = new DecimalFormat(
StringEscapeUtils.unescapeHtml(numberFormatPattern),
DecimalFormatSymbols.getInstance(locale));
}
if (integerInstance) {
result.setParseIntegerOnly(true);
result.setRoundingMode(RoundingMode.HALF_EVEN);
}
} catch (IllegalArgumentException e) {
String msg = String.format(
"formatter %s is invalid for decimal numbers: %s",
numberFormatPattern, e.getLocalizedMessage());
throw new ConversionException(msg);
}
} else {
if (integerInstance) {
result = NumberFormat.getIntegerInstance(locale);
} else {
result = NumberFormat.getNumberInstance(locale);
}
}
result.setGroupingUsed(useGrouping);
return result;
}
}