Browse files

PIVOT-892 (more): The real problem with validating scientific notatio…

…n was the UPPER/lower case

of the exponent character.  So, moved the Locale into FormattedValidator and doing the Upper case
conversion in there for NumberFormats and also in DecimalValidator.  This means Double and Float
Validators can go back to being simple classes.

Also checking the Float ranges better, and allow Infinity and NaN values in there.

Update the TextInputValidatorTest program to allow basic float / double validation as well.


git-svn-id: https://svn.apache.org/repos/asf/pivot/trunk@1444910 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
1 parent cb3d261 commit 008ff8e54c98c1a6d893e2ba4c99265bcd9e2fdc Roger Lee Whitcomb committed Feb 11, 2013
View
12 tests/src/org/apache/pivot/tests/TextInputValidatorTest.java
@@ -23,6 +23,8 @@
import org.apache.pivot.wtk.TextInput;
import org.apache.pivot.wtk.TextInputListener;
import org.apache.pivot.wtk.Window;
+import org.apache.pivot.wtk.validation.DoubleValidator;
+import org.apache.pivot.wtk.validation.FloatValidator;
import org.apache.pivot.wtk.validation.FloatRangeValidator;
import org.apache.pivot.wtk.validation.IntRangeValidator;
import org.apache.pivot.wtk.validation.NotEmptyTextValidator;
@@ -34,6 +36,8 @@
*/
public class TextInputValidatorTest extends Application.Adapter {
private Window window = null;
+ private TextInput textinputDouble = null;
+ private TextInput textinputFloat = null;
private TextInput textinputFloatRange = null;
private Label invalidLabel = null;
private TextInput textinputIntRange = null;
@@ -47,12 +51,20 @@ public void startup(Display display, Map<String, String> properties) throws Exce
window = new Window((Component)bxmlSerializer.readObject(
getClass().getResource("text_input_validator_test.bxml")));
+ textinputDouble = (TextInput)bxmlSerializer.getNamespace().get("textinputDouble");
+ textinputFloat = (TextInput)bxmlSerializer.getNamespace().get("textinputFloat");
textinputFloatRange = (TextInput)bxmlSerializer.getNamespace().get("textinputFloatRange");
textinputIntRange = (TextInput)bxmlSerializer.getNamespace().get("textinputIntRange");
textinputDateRegex = (TextInput)bxmlSerializer.getNamespace().get("textinputDateRegex");
textinputCustomBoolean = (TextInput)bxmlSerializer.getNamespace().get("textinputCustomBoolean");
textinputNotEmptyText = (TextInput)bxmlSerializer.getNamespace().get("textinputNotEmptyText");
+ textinputDouble.setText("\u221E");
+ textinputDouble.setValidator(new DoubleValidator());
+
+ textinputFloat.setText("1.5");
+ textinputFloat.setValidator(new FloatValidator());
+
// standard float range model
textinputFloatRange.setText("0.5");
textinputFloatRange.setValidator(new FloatRangeValidator(0.3f, 2000f));
View
12 tests/src/org/apache/pivot/tests/text_input_validator_test.bxml
@@ -24,13 +24,23 @@ limitations under the License.
</columns>
<TablePane.Row height="-1">
+ <Label text="double"/>
+ <TextInput bxml:id="textinputDouble"/>
+ <Label text="valid value: any double: ~4.94e-324 to ~1.79e+308 or &#x00B1;&#x221E;"/>
+ </TablePane.Row>
+ <TablePane.Row height="-1">
<Label text="float"/>
+ <TextInput bxml:id="textinputFloat"/>
+ <Label text="valid value: any float: ~1.40e-45 to ~3.40e+38 or &#x00B1;&#x221E;"/>
+ </TablePane.Row>
+ <TablePane.Row height="-1">
+ <Label text="float range"/>
<TextInput bxml:id="textinputFloatRange"/>
<Label text="valid range: 0.3 .. 2000"/>
<Label bxml:id="invalidLabel"/>
</TablePane.Row>
<TablePane.Row height="-1">
- <Label text="int"/>
+ <Label text="int range"/>
<TextInput bxml:id="textinputIntRange"/>
<Label text="valid range: 0 .. 100"/>
</TablePane.Row>
View
8 wtk/src/org/apache/pivot/wtk/validation/DecimalValidator.java
@@ -27,17 +27,23 @@ public DecimalValidator(DecimalFormat format) {
super(format);
}
+ public DecimalValidator(DecimalFormat format, Locale locale) {
+ super(format, locale);
+ }
+
public DecimalValidator() {
super(NumberFormat.getInstance());
}
public DecimalValidator(Locale locale) {
- super(NumberFormat.getInstance(locale));
+ super(NumberFormat.getInstance(locale), locale);
}
/** helper method that wraps the ParseException in a RuntimeException. */
protected final Number parseNumber(String text) {
try {
+ // We have to upper case because of the exponent symbol
+ text = (locale == null) ? text.toUpperCase() : text.toUpperCase(locale);
return format.parse(text);
} catch (ParseException ex) {
// this should never happen
View
13 wtk/src/org/apache/pivot/wtk/validation/DoubleValidator.java
@@ -21,22 +21,13 @@
* A validator for a double value.
*/
public class DoubleValidator extends DecimalValidator {
- private Locale locale = null;
public DoubleValidator() {
- super(new DecimalFormat("0E0"));
+ super();
}
public DoubleValidator(Locale locale) {
- this();
- this.locale = locale;
- ((DecimalFormat)format).setDecimalFormatSymbols(new DecimalFormatSymbols(locale));
- }
-
- @Override
- public boolean isValid(String text) {
- // We have to upper case because of the exponent symbol
- return super.isValid(locale == null ? text.toUpperCase() : text.toUpperCase(locale));
+ super(locale);
}
}
View
20 wtk/src/org/apache/pivot/wtk/validation/FloatValidator.java
@@ -21,33 +21,33 @@
* A validator for a float value.
*/
public class FloatValidator extends DecimalValidator {
- private Locale locale = null;
public FloatValidator() {
- super(new DecimalFormat("0E0"));
+ super();
}
public FloatValidator(Locale locale) {
- this();
- this.locale = locale;
- ((DecimalFormat)format).setDecimalFormatSymbols(new DecimalFormatSymbols(locale));
+ super(locale);
}
@Override
public boolean isValid(String text) {
- // We have to upper case because of the exponent symbol
- if (!super.isValid(locale == null ? text.toUpperCase() : text.toUpperCase(locale)))
+ if (!super.isValid(text))
return false;
/*
* DecimalFormat will parse the number as a double. Make sure the
* resulting number is withing range for a float.
*/
Number number = parseNumber(text);
- if (number.doubleValue() > Float.MAX_VALUE) {
- return false;
+ double doubleValue = number.doubleValue();
+ if (doubleValue == Double.POSITIVE_INFINITY ||
+ doubleValue == Double.NEGATIVE_INFINITY ||
+ doubleValue == Double.NaN) {
+ return true;
}
- if (number.doubleValue() < -Float.MAX_VALUE) {
+ doubleValue = Math.abs(doubleValue);
+ if (doubleValue > Float.MAX_VALUE || doubleValue < Float.MIN_VALUE) {
return false;
}
return true;
View
13 wtk/src/org/apache/pivot/wtk/validation/FormattedValidator.java
@@ -14,7 +14,9 @@
package org.apache.pivot.wtk.validation;
import java.text.Format;
+import java.text.NumberFormat;
import java.text.ParsePosition;
+import java.util.Locale;
/**
* A validator for a {@link java.text.Format}'ed value.
@@ -26,14 +28,25 @@
*/
public class FormattedValidator<F extends Format> implements Validator {
protected final F format;
+ protected final Locale locale;
public FormattedValidator(F format) {
this.format = format;
+ this.locale = null;
+ }
+
+ public FormattedValidator(F format, Locale locale) {
+ this.format = format;
+ this.locale = locale;
}
@Override
public boolean isValid(String text) {
final ParsePosition pos = new ParsePosition(0);
+ if (format instanceof NumberFormat) {
+ // We have to upper case because of the exponent symbol
+ text = (locale == null) ? text.toUpperCase() : text.toUpperCase(locale);
+ }
Object obj = format.parseObject(text, pos);
// The text is only valid if we successfully parsed ALL of it. Don't want trailing bits of

0 comments on commit 008ff8e

Please sign in to comment.