diff --git a/superfields/src/main/java/org/vaadin/miki/superfields/numbers/AbstractSuperNumberField.java b/superfields/src/main/java/org/vaadin/miki/superfields/numbers/AbstractSuperNumberField.java index b77e2282..e212296c 100644 --- a/superfields/src/main/java/org/vaadin/miki/superfields/numbers/AbstractSuperNumberField.java +++ b/superfields/src/main/java/org/vaadin/miki/superfields/numbers/AbstractSuperNumberField.java @@ -332,8 +332,16 @@ protected StringBuilder buildRegularExpression(StringBuilder builder, DecimalFor return builder; } - private void onFieldBlurred(BlurNotifier.BlurEvent event) { + /** + * This method is called when the field loses its focus. + * Do not overwrite it without a very good reason. + */ + protected void updateFieldValue() { this.setPresentationValue(this.getValue()); + } + + private void onFieldBlurred(BlurNotifier.BlurEvent event) { + this.updateFieldValue(); // fire event this.getEventBus().fireEvent(new BlurEvent<>(this, event.isFromClient())); } diff --git a/superfields/src/main/java/org/vaadin/miki/superfields/numbers/SuperBigDecimalField.java b/superfields/src/main/java/org/vaadin/miki/superfields/numbers/SuperBigDecimalField.java index 94eaea03..a5f8a3f9 100644 --- a/superfields/src/main/java/org/vaadin/miki/superfields/numbers/SuperBigDecimalField.java +++ b/superfields/src/main/java/org/vaadin/miki/superfields/numbers/SuperBigDecimalField.java @@ -134,6 +134,10 @@ public final boolean isScientificNotationEnabled() { return this.getExponentSeparator() > 0 && this.getMaximumExponentDigits() > 0; } + private boolean isValueInScientificNotation(String rawValue) { + return this.isScientificNotationEnabled() && rawValue.toUpperCase(this.getLocale()).contains(String.valueOf(this.getExponentSeparator()).toUpperCase(this.getLocale())); + } + @Override protected BigDecimal parseRawValue(String rawValue, DecimalFormat format) throws ParseException { // using scientific notation typing allows for some weird situations, e.g. when the last character can be E or - or + - in general, something outside of 0 to 9 @@ -141,7 +145,7 @@ protected BigDecimal parseRawValue(String rawValue, DecimalFormat format) throws if(!Character.isDigit(rawValue.charAt(rawValue.length()-1))) rawValue = rawValue + "0"; - if(this.isScientificNotationEnabled() && rawValue.toUpperCase(this.getLocale()).contains(String.valueOf(this.getExponentSeparator()).toUpperCase(this.getLocale()))) { + if(this.isValueInScientificNotation(rawValue)) { return new BigDecimal(rawValue.replace(format.getDecimalFormatSymbols().getDecimalSeparator(), '.')); } else { @@ -155,6 +159,21 @@ protected BigDecimal parseRawValue(String rawValue, DecimalFormat format) throws } } + @Override + protected void updateFieldValue() { + // this is here to fix #268 + // not a nice-looking fix, but it seems to work nice + if(this.isValueInScientificNotation(this.getRawValue())) { + try { + this.setValue(this.parseRawValue(this.getRawValue())); + } + catch (ParseException pe) { + super.updateFieldValue(); + } + } + else super.updateFieldValue(); + } + /** * Sets maximum allowed digits in exponent. If this number is greater than zero, it enables scientific notation input. * @param maximumExponentDigits Number of digits allowed in the exponent part of scientific notation.