diff options
author | Sauli Tähkäpää <sauli@vaadin.com> | 2015-04-03 23:39:41 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2015-05-26 08:22:34 +0000 |
commit | 1ac6c591a652200075fff540242b556dbd75c8b3 (patch) | |
tree | 2247c1ca70da839a15750ae6d223ad4544e4edbc /server | |
parent | e2678791bb028746f1b78aa232bef4240ebffa92 (diff) | |
download | vaadin-framework-1ac6c591a652200075fff540242b556dbd75c8b3.tar.gz vaadin-framework-1ac6c591a652200075fff540242b556dbd75c8b3.zip |
Remove value casting to integer in Slider.setValue (#17370)
- Added decimal rounding also to min/max boundaries.
- Rearranged also resolution/min/max value setting in constructor.
Change-Id: Ia90fd27b95f727673c16a90fed1130d98b915ea9
Diffstat (limited to 'server')
-rw-r--r-- | server/src/com/vaadin/ui/Slider.java | 58 | ||||
-rw-r--r-- | server/tests/src/com/vaadin/tests/server/component/slider/SliderTest.java | 56 |
2 files changed, 86 insertions, 28 deletions
diff --git a/server/src/com/vaadin/ui/Slider.java b/server/src/com/vaadin/ui/Slider.java index 40a4047d53..2e00628eb0 100644 --- a/server/src/com/vaadin/ui/Slider.java +++ b/server/src/com/vaadin/ui/Slider.java @@ -104,9 +104,9 @@ public class Slider extends AbstractField<Double> { */ public Slider(double min, double max, int resolution) { this(); - setMin(min); - setMax(max); setResolution(resolution); + setMax(max); + setMin(min); } /** @@ -167,14 +167,15 @@ public class Slider extends AbstractField<Double> { * The new maximum slider value */ public void setMax(double max) { - getState().maxValue = max; + double roundedMax = getRoundedValue(max); + getState().maxValue = roundedMax; - if (getMin() > max) { - getState().minValue = max; + if (getMin() > roundedMax) { + getState().minValue = roundedMax; } - if (getValue() > max) { - setValue(max); + if (getValue() > roundedMax) { + setValue(roundedMax); } } @@ -195,14 +196,15 @@ public class Slider extends AbstractField<Double> { * The new minimum slider value */ public void setMin(double min) { - getState().minValue = min; + double roundedMin = getRoundedValue(min); + getState().minValue = roundedMin; - if (getMax() < min) { - getState().maxValue = min; + if (getMax() < roundedMin) { + getState().maxValue = roundedMin; } - if (getValue() < min) { - setValue(min); + if (getValue() < roundedMin) { + setValue(roundedMin); } } @@ -268,28 +270,28 @@ public class Slider extends AbstractField<Double> { */ @Override protected void setValue(Double value, boolean repaintIsNotNeeded) { - final double v = value.doubleValue(); - final int resolution = getResolution(); - double newValue; - - if (resolution > 0) { - // Round up to resolution - newValue = Math.floor(v * Math.pow(10, resolution)); - newValue = newValue / Math.pow(10, resolution); - if (getMin() > newValue || getMax() < newValue) { - throw new ValueOutOfBoundsException(newValue); - } - } else { - newValue = (int) v; - if (getMin() > newValue || getMax() < newValue) { - throw new ValueOutOfBoundsException(newValue); - } + double newValue = getRoundedValue(value); + + if (getMin() > newValue || getMax() < newValue) { + throw new ValueOutOfBoundsException(newValue); } getState().value = newValue; super.setValue(newValue, repaintIsNotNeeded); } + private double getRoundedValue(Double value) { + final double v = value.doubleValue(); + final int resolution = getResolution(); + + double ratio = Math.pow(10, resolution); + if(v >= 0) { + return Math.floor(v * ratio) / ratio; + } else { + return Math.ceil(v * ratio) / ratio; + } + } + @Override public void setValue(Double newFieldValue) { super.setValue(newFieldValue); diff --git a/server/tests/src/com/vaadin/tests/server/component/slider/SliderTest.java b/server/tests/src/com/vaadin/tests/server/component/slider/SliderTest.java index d2e2654cbc..7f20f29648 100644 --- a/server/tests/src/com/vaadin/tests/server/component/slider/SliderTest.java +++ b/server/tests/src/com/vaadin/tests/server/component/slider/SliderTest.java @@ -76,4 +76,60 @@ public class SliderTest { assertThat(slider.getValue(), is(99.01234567891234567890123456789)); } + + @Test + public void doublesCanBeUsedAsLimits() { + Slider slider = new Slider(1.5, 2.5, 1); + + assertThat(slider.getMin(), is(1.5)); + assertThat(slider.getValue(), is(1.5)); + assertThat(slider.getMax(), is(2.5)); + } + + @Test + public void valuesGreaterThanIntMaxValueCanBeUsed() { + double minValue = (double)Integer.MAX_VALUE + 1; + + Slider s = new Slider(minValue, minValue + 1, 0); + + assertThat(s.getValue(), is(minValue)); + } + + @Test + public void negativeValuesCanBeUsed() { + Slider slider = new Slider(-0.7, 1.0, 0); + + slider.setValue(-0.4); + + assertThat(slider.getValue(), is(-0.0)); + } + + @Test + public void boundariesAreRounded() { + Slider slider = new Slider(1.5, 2.5, 0); + + slider.setValue(1.0); + + assertThat(slider.getValue(), is(1.0)); + assertThat(slider.getMin(), is(1.0)); + assertThat(slider.getMax(), is(2.0)); + } + + @Test + public void valueWithSmallerPrecisionCanBeUsed() { + Slider slider = new Slider(0, 100, 10); + + slider.setValue(1.2); + + assertThat(slider.getValue(), is(1.2)); + } + + @Test + public void valueWithLargerPrecisionCanBeUsed() { + Slider slider = new Slider(0, 100, 2); + + slider.setValue(1.2345); + + assertThat(slider.getValue(), is(1.23)); + } } |