Repository: wicket Updated Branches: refs/heads/wicket-6.x d50e18c73 -> b123b969e
WICKET-5986 NumberTextField<N> should use Models for minimum, maximum and step Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/b6ee11d0 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/b6ee11d0 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/b6ee11d0 Branch: refs/heads/wicket-6.x Commit: b6ee11d0df104edc34d6011295fe378471bc3b67 Parents: d50e18c Author: Martin Tzvetanov Grigorov <[email protected]> Authored: Sat Oct 3 15:03:31 2015 +0200 Committer: Martin Tzvetanov Grigorov <[email protected]> Committed: Sat Oct 3 15:11:33 2015 +0200 ---------------------------------------------------------------------- .../markup/html/form/NumberTextField.java | 80 +++++++++++++++----- .../markup/html/form/NumberTextFieldTest.java | 6 +- 2 files changed, 66 insertions(+), 20 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/b6ee11d0/wicket-core/src/main/java/org/apache/wicket/markup/html/form/NumberTextField.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/NumberTextField.java b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/NumberTextField.java index 9fd5be2..fe12e0c 100644 --- a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/NumberTextField.java +++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/NumberTextField.java @@ -20,6 +20,7 @@ import java.util.Locale; import org.apache.wicket.markup.ComponentTag; import org.apache.wicket.model.IModel; +import org.apache.wicket.model.Model; import org.apache.wicket.util.convert.ConversionException; import org.apache.wicket.util.convert.IConverter; import org.apache.wicket.util.lang.Objects; @@ -51,11 +52,11 @@ public class NumberTextField<N extends Number & Comparable<N>> extends TextField private RangeValidator<N> validator; - private N minimum; + private IModel<N> minimum; - private N maximum; + private IModel<N> maximum; - private N step; + private IModel<N> step; /** * Construct. @@ -111,8 +112,48 @@ public class NumberTextField<N extends Number & Comparable<N>> extends TextField super(id, model, type); validator = null; - minimum = null; - maximum = null; + minimum = Model.of((N)null); + maximum = Model.of((N)null); + step = Model.of((N)null); + } + + /** + * Sets the minimum allowed value + * + * @param minimum + * the minimum allowed value + * @return this instance + */ + public NumberTextField<N> setMinimum(final N minimum) + { + this.minimum = Model.of(minimum); + return this; + } + + /** + * Sets the maximum allowed value + * + * @param maximum + * the maximum allowed value + * @return this instance + */ + public NumberTextField<N> setMaximum(final N maximum) + { + this.maximum = Model.of(maximum); + return this; + } + + /** + * Sets the step attribute + * + * @param step + * the step attribute + * @return this instance + */ + public NumberTextField<N> setStep(final N step) + { + this.step = Model.of(step); + return this; } /** @@ -122,7 +163,7 @@ public class NumberTextField<N extends Number & Comparable<N>> extends TextField * the minimum allowed value * @return this instance */ - public NumberTextField<N> setMinimum(final N minimum) + public NumberTextField<N> setMinimum(final IModel<N> minimum) { this.minimum = minimum; return this; @@ -135,7 +176,7 @@ public class NumberTextField<N extends Number & Comparable<N>> extends TextField * the maximum allowed value * @return this instance */ - public NumberTextField<N> setMaximum(final N maximum) + public NumberTextField<N> setMaximum(final IModel<N> maximum) { this.maximum = maximum; return this; @@ -148,7 +189,7 @@ public class NumberTextField<N extends Number & Comparable<N>> extends TextField * the step attribute * @return this instance */ - public NumberTextField<N> setStep(final N step) + public NumberTextField<N> setStep(final IModel<N> step) { this.step = step; return this; @@ -165,9 +206,11 @@ public class NumberTextField<N extends Number & Comparable<N>> extends TextField validator = null; } - if (minimum != null || maximum != null) + final N min = minimum.getObject(); + final N max = maximum.getObject(); + if (min != null || max != null) { - validator = RangeValidator.range(minimum, maximum); + validator = RangeValidator.range(min, max); add(validator); } } @@ -189,33 +232,36 @@ public class NumberTextField<N extends Number & Comparable<N>> extends TextField IValueMap attributes = tag.getAttributes(); - if (minimum != null) + final N min = minimum.getObject(); + if (min != null) { - attributes.put("min", Objects.stringValue(minimum)); + attributes.put("min", Objects.stringValue(min)); } else { attributes.remove("min"); } - if (maximum != null) + final N max = maximum.getObject(); + if (max != null) { - attributes.put("max", Objects.stringValue(maximum)); + attributes.put("max", Objects.stringValue(max)); } else { attributes.remove("max"); } - if (step != null) + final N _step = step.getObject(); + if (_step != null) { - if (step.doubleValue() == ANY) + if (_step.doubleValue() == ANY) { attributes.put("step", "any"); } else { - attributes.put("step", Objects.stringValue(step)); + attributes.put("step", Objects.stringValue(_step)); } } else http://git-wip-us.apache.org/repos/asf/wicket/blob/b6ee11d0/wicket-core/src/test/java/org/apache/wicket/markup/html/form/NumberTextFieldTest.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/java/org/apache/wicket/markup/html/form/NumberTextFieldTest.java b/wicket-core/src/test/java/org/apache/wicket/markup/html/form/NumberTextFieldTest.java index bb951d5..e912382 100644 --- a/wicket-core/src/test/java/org/apache/wicket/markup/html/form/NumberTextFieldTest.java +++ b/wicket-core/src/test/java/org/apache/wicket/markup/html/form/NumberTextFieldTest.java @@ -43,9 +43,9 @@ public class NumberTextFieldTest extends WicketTestCase @Test public void convertBigDecimal() { - TestPage<BigDecimal> testPage = new TestPage<BigDecimal>(); + TestPage<BigDecimal> testPage = new TestPage<>(); testPage.textField.setType(BigDecimal.class); - testPage.textField.setMinimum(new BigDecimal("0.00")); + testPage.textField.setMinimum(Model.of(new BigDecimal("0.00"))); testPage.textField.setMaximum(new BigDecimal("100.00")); testPage.textField.setModelObject(new BigDecimal("0.00")); tester.startPage(testPage); @@ -111,7 +111,7 @@ public class NumberTextFieldTest extends WicketTestCase { TestPage<Double> testPage = new TestPage<Double>(); testPage.textField.setType(Double.class); - testPage.textField.setStep(NumberTextField.ANY); + testPage.textField.setStep(Model.of(NumberTextField.ANY)); testPage.textField.setModelObject(new Double("1000.0")); tester.startPage(testPage);
