Skip to content

Commit

Permalink
#268 fixed, hopefully (#272)
Browse files Browse the repository at this point in the history
also, numbers entered with scientific notation now do not lose formatting on field blur
  • Loading branch information
vaadin-miki authored Feb 14, 2021
1 parent e6ace79 commit 740e95b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,16 @@ protected StringBuilder buildRegularExpression(StringBuilder builder, DecimalFor
return builder;
}

private void onFieldBlurred(BlurNotifier.BlurEvent<TextField> 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<TextField> event) {
this.updateFieldValue();
// fire event
this.getEventBus().fireEvent(new BlurEvent<>(this, event.isFromClient()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,18 @@ 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
// and yes, Character.isDigit actually supports more than 0 to 9, let's hope this omission does not bite back
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 {
Expand All @@ -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.
Expand Down

0 comments on commit 740e95b

Please sign in to comment.