From bec9d22deba540bb5bfaeb88700efd4f313adf05 Mon Sep 17 00:00:00 2001 From: Simon Barner <barner@fortiss.org> Date: Wed, 28 Nov 2018 14:56:50 +0100 Subject: [PATCH] PropertySectionBase.TextToDoubleValidator -> databinding.NumberValidator * Rename singleton instance to NUMBER_VALIDATOR * Use java.text.NumberFormat instead of the one from com.ibm.icu * Disallow using 1000er group separator into to avoid confusions with decimal separator * Accept empty input, as well as leading and trailing spaces Issue-Ref: 3582 Issue-Url: https://af3-developer.fortiss.org/issues/3582 --- .../tooling/kernel/ui/databinding/.ratings | 1 + .../ui/databinding/NumberValidator.java | 62 +++++++++++++++++++ .../tooling/kernel/ui/extension/base/.ratings | 2 +- .../extension/base/PropertySectionBase.java | 46 +++----------- 4 files changed, 71 insertions(+), 40 deletions(-) create mode 100644 org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/databinding/NumberValidator.java diff --git a/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/databinding/.ratings b/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/databinding/.ratings index 3243a3aa7..6cd0fe6ea 100644 --- a/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/databinding/.ratings +++ b/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/databinding/.ratings @@ -5,3 +5,4 @@ AbstractTextCellDatabindingEditingSupport.java 8728e78f399a63c11f020fa447c002f42 FloatValidator.java 7a32cb83604838ae17d3da751ce003731a055b05 GREEN IntValidator.java de2d93012a863ab26118453413ca09e7764124a8 GREEN NumberPositiveZeroValidator.java 4f555462dc532a2e88f2842701a48e3991f7ef9d GREEN +NumberValidator.java 28871dc5823ad14466273995599d3cd74fe69512 YELLOW diff --git a/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/databinding/NumberValidator.java b/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/databinding/NumberValidator.java new file mode 100644 index 000000000..28871dc58 --- /dev/null +++ b/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/databinding/NumberValidator.java @@ -0,0 +1,62 @@ +/*-------------------------------------------------------------------------+ +| Copyright 2018 fortiss GmbH | +| | +| Licensed under the Apache License, Version 2.0 (the "License"); | +| you may not use this file except in compliance with the License. | +| You may obtain a copy of the License at | +| | +| http://www.apache.org/licenses/LICENSE-2.0 | +| | +| Unless required by applicable law or agreed to in writing, software | +| distributed under the License is distributed on an "AS IS" BASIS, | +| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | +| See the License for the specific language governing permissions and | +| limitations under the License. | ++--------------------------------------------------------------------------*/ +package org.fortiss.tooling.kernel.ui.databinding; + +import static org.eclipse.core.databinding.validation.ValidationStatus.cancel; +import static org.eclipse.core.databinding.validation.ValidationStatus.ok; + +import java.text.NumberFormat; +import java.text.ParsePosition; + +import org.eclipse.core.databinding.validation.IValidator; +import org.eclipse.core.runtime.IStatus; + +/** Validates if a given input is a valid {@link Number}. */ +public class NumberValidator implements IValidator { + + /** Singleton instance. */ + public static final NumberValidator NUMBER_VALIDATOR = new NumberValidator(); + + /** The number format used in the current locale. */ + private final NumberFormat format; + + /** Constructor. */ + protected NumberValidator() { + format = NumberFormat.getNumberInstance(); + format.setGroupingUsed(false); + } + + /** {@inheritDoc} */ + @Override + public IStatus validate(Object value) { + String input = value != null ? value.toString().trim() : ""; + if(input.isEmpty()) { + return ok(); + } + + ParsePosition p = new ParsePosition(0); + format.parse(input, p); + int errorIndex = p.getErrorIndex(); + // In case the input as a valid prefix, the error index will not indicate a problem. + // Therefore, check if the entire input was parsed. + if(errorIndex == -1 && p.getIndex() < input.length()) { + errorIndex = p.getIndex(); + } + + return errorIndex == -1 ? ok() + : cancel("Double number format validation failed at position " + errorIndex + "."); + } +} diff --git a/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/extension/base/.ratings b/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/extension/base/.ratings index a2f016219..bad8716d3 100644 --- a/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/extension/base/.ratings +++ b/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/extension/base/.ratings @@ -8,7 +8,7 @@ ModelEditorBindingBase.java 4c5ac569c0b6e7678fc8191096b26dfd09fdcb98 GREEN ModelElementHandlerBase.java 384727748f125c9d43f19d9c0eba4ba1be5a7a26 GREEN MultiEObjectActionBase.java 9e237d8ea640c4194e4877af4a9cfce88698e543 GREEN NamedCommentedModelElementHandlerBase.java 681b98b50b362f01abb7a36f108f4f11b9e51829 GREEN -PropertySectionBase.java c545ad187b8fcff8fc9e592a923b7c0b1522133b YELLOW +PropertySectionBase.java 247ddc3eee4737ae36623d138ee397da6d93fdf6 YELLOW TutorialStepUIAtomicBase.java cea2a158158b476de2108d2309afcf47f217b6d9 GREEN TutorialStepUIAtomicWithWhitelistBase.java a9788ae514f62d27169c737ef59fb583234b5d43 GREEN TutorialStepUICompositeBase.java 8225210eacb5b88de47d78280c5819f572f00ffa GREEN diff --git a/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/extension/base/PropertySectionBase.java b/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/extension/base/PropertySectionBase.java index c545ad187..247ddc3ee 100644 --- a/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/extension/base/PropertySectionBase.java +++ b/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/extension/base/PropertySectionBase.java @@ -21,20 +21,17 @@ import static org.eclipse.core.databinding.conversion.NumberToStringConverter.fr import static org.eclipse.core.databinding.conversion.StringToNumberConverter.toBigDecimal; import static org.eclipse.core.databinding.conversion.StringToNumberConverter.toDouble; import static org.eclipse.core.databinding.conversion.StringToNumberConverter.toInteger; -import static org.eclipse.core.databinding.validation.ValidationStatus.cancel; -import static org.eclipse.core.databinding.validation.ValidationStatus.ok; +import static org.fortiss.tooling.kernel.ui.databinding.NumberValidator.NUMBER_VALIDATOR; import static org.fortiss.tooling.kernel.ui.util.DataBindingUtils.DECORATION_KEY; import static org.fortiss.tooling.kernel.ui.util.DataBindingUtils.performComplexTextBinding; import static org.fortiss.tooling.kernel.ui.util.WidgetsFactory.createTextWithUndo; import java.math.BigDecimal; -import java.text.ParsePosition; import org.conqat.ide.commons.ui.databinding.validate.NumberPositiveValidator; import org.conqat.ide.commons.ui.databinding.validate.TextToIntegerValidator; import org.eclipse.core.databinding.observable.value.IObservableValue; import org.eclipse.core.databinding.validation.IValidator; -import org.eclipse.core.runtime.IStatus; import org.eclipse.emf.databinding.EMFDataBindingContext; import org.eclipse.emf.ecore.EObject; import org.eclipse.gef.EditPart; @@ -69,35 +66,6 @@ import com.ibm.icu.text.NumberFormat; */ public abstract class PropertySectionBase extends AbstractPropertySection { - /** Validates if a given input is a valid {@link Number}. */ - private static class TextToDoubleValidator implements IValidator { - - /** Singleton instance. */ - public static final TextToDoubleValidator INSTANCE = new TextToDoubleValidator(); - - /** The number format used in the current locale. */ - private final NumberFormat format = NumberFormat.getNumberInstance(); - - /** {@inheritDoc} */ - @Override - public IStatus validate(Object value) { - assert (value != null); - - ParsePosition p = new ParsePosition(0); - String input = value.toString(); - format.parse(input, p); - int errorIndex = p.getErrorIndex(); - // In case the input as a valid prefix, the error index will not indicate a problem. - // Therefore, check if the entire input was parsed. - if(errorIndex == -1 && p.getIndex() < input.length()) { - errorIndex = p.getIndex(); - } - - return errorIndex == -1 ? ok() : cancel( - "Double number format validation failed at position " + errorIndex + "."); - } - } - /** Label width. */ public static final int PROPERTIES_LABEL_WIDTH = 150; @@ -342,10 +310,10 @@ public abstract class PropertySectionBase extends AbstractPropertySection { */ protected void bindBigDecimalValue(Control text, IObservableValue<?> observedValue, IValidator numberValidator) { - // TextToDoubleValidator checks format of fractional numbers and is hence also suitable for + // DoubleValidator checks format of fractional numbers and is hence also suitable for // BigDecimals performComplexTextBinding(dbc, text, observedValue, fromBigDecimal(), toBigDecimal(), - TextToDoubleValidator.INSTANCE, numberValidator); + NUMBER_VALIDATOR, numberValidator); } /** @@ -357,10 +325,10 @@ public abstract class PropertySectionBase extends AbstractPropertySection { NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMaximumFractionDigits(maxFractionDigits); - // TextToDoubleValidator checks format of fractional numbers and is hence also suitable for + // DoubleValidator checks format of fractional numbers and is hence also suitable for // BigDecimals performComplexTextBinding(dbc, text, observedValue, fromBigDecimal(nf), toBigDecimal(nf), - TextToDoubleValidator.INSTANCE, numberValidator); + NUMBER_VALIDATOR, numberValidator); } /** @@ -387,7 +355,7 @@ public abstract class PropertySectionBase extends AbstractPropertySection { protected void bindDoubleValue(Control text, IObservableValue<?> observedValue, IValidator numberValidator) { performComplexTextBinding(dbc, text, observedValue, fromDouble(false), toDouble(false), - TextToDoubleValidator.INSTANCE, numberValidator); + NUMBER_VALIDATOR, numberValidator); } /** @@ -400,7 +368,7 @@ public abstract class PropertySectionBase extends AbstractPropertySection { NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMaximumFractionDigits(maxFractionDigits); performComplexTextBinding(dbc, text, observedValue, fromDouble(nf, false), - toDouble(nf, false), TextToDoubleValidator.INSTANCE, numberValidator); + toDouble(nf, false), NUMBER_VALIDATOR, numberValidator); } /** Binds a positive integer value. */ -- GitLab