| /** |
| * Copyright (c) 2011, 2014 - 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 |
| * Hans Georg Glöckler - Initial implementation |
| */ |
| package org.eclipse.osbp.runtime.web.vaadin.components.fields; |
| |
| import java.text.DecimalFormatSymbols; |
| |
| import org.eclipse.osbp.runtime.web.vaadin.components.converter.DecimalConverter; |
| |
| import com.vaadin.data.util.converter.Converter; |
| |
| // TODO: Auto-generated Javadoc |
| /** |
| * A decimalfield specific for redvoodo. |
| */ |
| @SuppressWarnings("serial") |
| public class DecimalField extends TextField { |
| |
| /** The Constant NEGATIVE_VALUE. */ |
| private static final String NEGATIVE_VALUE = "lun-negative-value"; |
| |
| /** The converter. */ |
| private DecimalConverter converter; |
| |
| /** The mark negative. */ |
| private boolean markNegative; |
| |
| /** |
| * Instantiates a new decimal field. |
| */ |
| public DecimalField() { |
| this(null); |
| } |
| |
| /** |
| * Instantiates a new decimal field. |
| * |
| * @param caption |
| * the caption |
| */ |
| public DecimalField(String caption) { |
| this(caption, null); |
| } |
| |
| /** |
| * Instantiates a new decimal field. |
| * |
| * @param caption |
| * the caption |
| * @param converter |
| * the converter |
| */ |
| public DecimalField(String caption, DecimalConverter converter) { |
| super(caption); |
| |
| setNullRepresentation(""); |
| setNullSettingAllowed(false); |
| |
| // Important: Is responsible that the Converter is used in the Field |
| setConverter(converter); |
| } |
| |
| /** |
| * Sets the converter. |
| * |
| * @param converter |
| * the new converter |
| */ |
| public void setConverter(DecimalConverter converter) { |
| this.converter = converter != null ? converter : createConverter(); |
| super.setConverter(this.converter); |
| } |
| |
| /* (non-Javadoc) |
| * @see com.vaadin.ui.AbstractField#setConverter(com.vaadin.data.util.converter.Converter) |
| */ |
| @Override |
| public void setConverter(Converter<String, ?> converter) { |
| if (converter instanceof DecimalConverter || converter == null) { |
| setConverter((DecimalConverter) converter); |
| } else { |
| throw new UnsupportedOperationException(); |
| } |
| } |
| |
| /** |
| * Creates a default converter. |
| * |
| * @return the decimal converter |
| */ |
| protected DecimalConverter createConverter() { |
| return new DecimalConverter(); |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see com.vaadin.ui.AbstractField#setConvertedValue(java.lang.Object) |
| */ |
| @Override |
| public void setConvertedValue(Object value) { |
| boolean oldReadonly = isReadOnly(); |
| try { |
| setReadOnly(false); |
| super.setConvertedValue(value); |
| } finally { |
| setReadOnly(oldReadonly); |
| } |
| } |
| |
| /** |
| * Sets the Symbols which are used to Format. |
| * |
| * @param decimalFormatSymbols |
| * the new decimal format symbols |
| */ |
| public void setDecimalFormatSymbols( |
| DecimalFormatSymbols decimalFormatSymbols) { |
| converter.setDecimalFormatSymbols(decimalFormatSymbols); |
| |
| markAsDirty(); |
| } |
| |
| /** |
| * Returns the currently used decimal format symbols. |
| * |
| * @return the decimal format symbols |
| */ |
| public DecimalFormatSymbols getDecimalFormatSymbols() { |
| return converter.getDecimalFormatSymbols(); |
| } |
| |
| /** |
| * Returns true, if grouping is used. False otherwise. |
| * |
| * @return true, if is use grouping |
| */ |
| public boolean isUseGrouping() { |
| return converter.isUseGrouping(); |
| } |
| |
| /** |
| * Set true, if grouping should be used. False otherwise. |
| * |
| * @param useGrouping |
| * the new use grouping |
| */ |
| public void setUseGrouping(boolean useGrouping) { |
| converter.setUseGrouping(useGrouping); |
| |
| markAsDirty(); |
| } |
| |
| /** |
| * Returns the precision of that decimal field. |
| * |
| * @return the precision |
| */ |
| public int getPrecision() { |
| return converter.getPrecision(); |
| } |
| |
| /** |
| * Sets the precision of that decimal field. |
| * |
| * @param precision |
| * the new precision |
| */ |
| public void setPrecision(int precision) { |
| converter.setPrecision(precision); |
| |
| markAsDirty(); |
| } |
| |
| /** |
| * True, if negative values should become marked. |
| * |
| * @param markNegative |
| * the new mark negative |
| */ |
| public void setMarkNegative(boolean markNegative) { |
| this.markNegative = markNegative; |
| |
| handleNegative(); |
| } |
| |
| /** |
| * Returns true, if negative values should become marked. |
| * |
| * @return true, if is mark negative |
| */ |
| public boolean isMarkNegative() { |
| return markNegative; |
| } |
| |
| /* (non-Javadoc) |
| * @see com.vaadin.ui.AbstractTextField#setInternalValue(java.lang.String) |
| */ |
| protected void setInternalValue(String newValue) { |
| super.setInternalValue(newValue); |
| |
| handleNegative(); |
| } |
| |
| /** |
| * Is called to handle the negative marker. |
| */ |
| protected void handleNegative() { |
| removeStyleName(NEGATIVE_VALUE); |
| |
| if (!isMarkNegative()) { |
| return; |
| } |
| |
| // try to find out if value is negative |
| if (getPropertyDataSource() == null || isBuffered() || isModified()) { |
| String value = getInternalValue(); |
| try { |
| double result = converter.convertToModel(value, Double.class, |
| getLocale()); |
| if (result < 0) { |
| addStyleName(NEGATIVE_VALUE); |
| } |
| } catch (Exception e) { |
| // nothing to do |
| } |
| } else { |
| Object value = getPropertyDataSource().getValue(); |
| if (value != null) { |
| double result = (Double) value; |
| if (result < 0) { |
| addStyleName(NEGATIVE_VALUE); |
| } |
| } |
| } |
| } |
| } |