blob: fd22d6c793e0150a20c1b4f9736d2f8fe73d9e15 [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;
// TODO: Auto-generated Javadoc
/**
* A converter to format and parse Integer values.
*/
@SuppressWarnings("serial")
public class NumberConverter extends StringToNumberConverter {
/** 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;
/**
* Instantiates a new number converter.
*/
public NumberConverter() {
this.numberFormatPattern = getDefaultFormat();
this.decimalFormatSymbols = getDefaultFormatSymbols();
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();
}
/**
* Returns the default value for default format.
*
* @return the default format
*/
protected String getDefaultFormat() {
return "##,##0";
}
/**
* 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;
}
/* (non-Javadoc)
* @see com.vaadin.data.util.converter.AbstractStringToNumberConverter#getFormat(java.util.Locale)
*/
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));
}
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 {
result = NumberFormat.getIntegerInstance(locale);
}
result.setGroupingUsed(useGrouping);
return result;
}
}