| /** |
| * |
| * Copyright (c) 2011, 2016 - 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.ecview.extension.presentation.vaadin.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.StringToFloatConverter; |
| |
| /** |
| * A converter used to format and parse Decimal values. |
| */ |
| @SuppressWarnings("serial") |
| public class DecimalFloatConverter extends StringToFloatConverter { |
| |
| /** 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 float converter. |
| */ |
| public DecimalFloatConverter() { |
| this(false); |
| } |
| |
| /** |
| * Instantiates a new decimal float converter. |
| * |
| * @param integerInstance |
| * the integer instance |
| */ |
| public DecimalFloatConverter(boolean integerInstance) { |
| this.integerInstance = integerInstance; |
| this.numberFormatPattern = getDefaultFormat(); |
| this.decimalFormatSymbols = getDefaultFormatSymbols(); |
| this.precision = getDefaultPrecision(); |
| 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 precision. |
| * |
| * @return the default precision |
| */ |
| protected int getDefaultPrecision() { |
| return 2; |
| } |
| |
| /** |
| * 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.00"; |
| } |
| |
| /** |
| * 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) |
| */ |
| 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)); |
| } |
| } catch (IllegalArgumentException e) { |
| String msg = String.format( |
| "formatter %s is invalid for decimal numbers: %s", |
| numberFormatPattern, e.getLocalizedMessage()); |
| throw new ConversionException(msg); |
| } |
| |
| if (integerInstance) { |
| result.setParseIntegerOnly(true); |
| result.setRoundingMode(RoundingMode.HALF_EVEN); |
| } |
| } else { |
| if (integerInstance) { |
| result = NumberFormat.getIntegerInstance(locale); |
| } else { |
| result = NumberFormat.getNumberInstance(locale); |
| } |
| } |
| |
| result.setGroupingUsed(useGrouping); |
| |
| return result; |
| } |
| |
| } |