blob: c7535d1e24718952e495a266c454934a2f8c9959 [file] [log] [blame]
/**
*
* 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.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale;
import org.apache.commons.lang.StringEscapeUtils;
import org.eclipse.osbp.ecview.core.common.services.IServiceRegistry;
import org.eclipse.osbp.ecview.extension.model.converter.YSimpleDecimalConverter;
import org.eclipse.osbp.runtime.web.vaadin.components.converter.DecimalConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class SimpleDecimalConverter.
*/
@SuppressWarnings("serial")
public class SimpleDecimalConverter extends DecimalConverter {
/** The Constant LOGGER. */
private static final Logger LOGGER = LoggerFactory
.getLogger(SimpleDecimalConverter.class);
/** The registry. */
private IServiceRegistry registry;
/** The y converter. */
private YSimpleDecimalConverter yConverter;
/**
* Instantiates a new simple decimal converter.
*
* @param registry
* the registry
* @param yConverter
* the y converter
*/
public SimpleDecimalConverter(IServiceRegistry registry,
YSimpleDecimalConverter yConverter) {
this.registry = registry;
this.yConverter = yConverter;
}
/**
* Instantiates a new simple decimal converter.
*/
public SimpleDecimalConverter() {
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.osbp.runtime.web.vaadin.components.converter.DecimalConverter
* #getFormat(java.util.Locale)
*/
@Override
protected NumberFormat getFormat(Locale locale) {
if (locale == null) {
locale = Locale.getDefault();
}
DecimalFormat result;
try {
result = new DecimalFormat(StringEscapeUtils.unescapeHtml(yConverter.getNumberFormatPattern()),
DecimalFormatSymbols.getInstance(locale));
} catch (IllegalArgumentException e) {
String msg = String.format(
"formatter %s is invalid for decimal numbers: %s",
yConverter.getNumberFormatPattern(), e.getLocalizedMessage());
throw new ConversionException(msg);
}
return result;
}
}