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/src/com/vaadin | |
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/src/com/vaadin')
-rw-r--r-- | server/src/com/vaadin/ui/Slider.java | 58 |
1 files changed, 30 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); |