diff options
author | Pekka Hyvönen <pekka@vaadin.com> | 2016-09-16 08:43:42 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2016-09-16 11:19:51 +0000 |
commit | 36ad322721e3548899cbca9516d91edabd252f2f (patch) | |
tree | a4829bc8e897b9485983cfa0faba92c3303bce90 /compatibility-server | |
parent | f9411b3238a89d34f5c5ac9d95fcc0abd1b25816 (diff) | |
download | vaadin-framework-36ad322721e3548899cbca9516d91edabd252f2f.tar.gz vaadin-framework-36ad322721e3548899cbca9516d91edabd252f2f.zip |
Added Slider into compatibility package
Change-Id: Icc4cfc64c0d0bf9993b138eec15a8a73cb0be2f0
Diffstat (limited to 'compatibility-server')
3 files changed, 615 insertions, 0 deletions
diff --git a/compatibility-server/src/main/java/com/vaadin/v7/ui/Slider.java b/compatibility-server/src/main/java/com/vaadin/v7/ui/Slider.java new file mode 100644 index 0000000000..81f17e0b79 --- /dev/null +++ b/compatibility-server/src/main/java/com/vaadin/v7/ui/Slider.java @@ -0,0 +1,399 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * 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 com.vaadin.v7.ui; + +import java.util.Collection; + +import org.jsoup.nodes.Attributes; +import org.jsoup.nodes.Element; + +import com.vaadin.ui.declarative.DesignAttributeHandler; +import com.vaadin.ui.declarative.DesignContext; +import com.vaadin.v7.shared.ui.slider.SliderOrientation; +import com.vaadin.v7.shared.ui.slider.SliderServerRpc; +import com.vaadin.v7.shared.ui.slider.SliderState;; + +/** + * A component for selecting a numerical value within a range. + * + * @author Vaadin Ltd. + */ +@Deprecated +public class Slider extends AbstractField<Double> { + + private SliderServerRpc rpc = new SliderServerRpc() { + + @Override + public void valueChanged(double value) { + + /* + * Client side updates the state before sending the event so we need + * to make sure the cached state is updated to match the client. If + * we do not do this, a reverting setValue() call in a listener will + * not cause the new state to be sent to the client. + * + * See #12133. + */ + getUI().getConnectorTracker().getDiffState(Slider.this).put("value", + value); + + try { + setValue(value, true); + } catch (final ValueOutOfBoundsException e) { + // Convert to nearest bound + double out = e.getValue().doubleValue(); + if (out < getState().minValue) { + out = getState().minValue; + } + if (out > getState().maxValue) { + out = getState().maxValue; + } + Slider.super.setValue(new Double(out), false); + } + } + + }; + + /** + * Default slider constructor. Sets all values to defaults and the slide + * handle at minimum value. + * + */ + public Slider() { + super(); + registerRpc(rpc); + super.setValue(new Double(getState().minValue)); + } + + /** + * Create a new slider with the caption given as parameter. + * + * The range of the slider is set to 0-100 and only integer values are + * allowed. + * + * @param caption + * The caption for this slider (e.g. "Volume"). + */ + public Slider(String caption) { + this(); + setCaption(caption); + } + + /** + * Create a new slider with the given range and resolution. + * + * @param min + * The minimum value of the slider + * @param max + * The maximum value of the slider + * @param resolution + * The number of digits after the decimal point. + */ + public Slider(double min, double max, int resolution) { + this(); + setResolution(resolution); + setMax(max); + setMin(min); + } + + /** + * Create a new slider with the given range that only allows integer values. + * + * @param min + * The minimum value of the slider + * @param max + * The maximum value of the slider + */ + public Slider(int min, int max) { + this(); + setMin(min); + setMax(max); + setResolution(0); + } + + /** + * Create a new slider with the given caption and range that only allows + * integer values. + * + * @param caption + * The caption for the slider + * @param min + * The minimum value of the slider + * @param max + * The maximum value of the slider + */ + public Slider(String caption, int min, int max) { + this(min, max); + setCaption(caption); + } + + @Override + public SliderState getState() { + return (SliderState) super.getState(); + } + + @Override + public SliderState getState(boolean markAsDirty) { + return (SliderState) super.getState(markAsDirty); + } + + /** + * Gets the maximum slider value + * + * @return the largest value the slider can have + */ + public double getMax() { + return getState(false).maxValue; + } + + /** + * Set the maximum slider value. If the current value of the slider is + * larger than this, the value is set to the new maximum. + * + * @param max + * The new maximum slider value + */ + public void setMax(double max) { + double roundedMax = getRoundedValue(max); + getState().maxValue = roundedMax; + + if (getMin() > roundedMax) { + getState().minValue = roundedMax; + } + + if (getValue() > roundedMax) { + setValue(roundedMax); + } + } + + /** + * Gets the minimum slider value + * + * @return the smallest value the slider can have + */ + public double getMin() { + return getState(false).minValue; + } + + /** + * Set the minimum slider value. If the current value of the slider is + * smaller than this, the value is set to the new minimum. + * + * @param min + * The new minimum slider value + */ + public void setMin(double min) { + double roundedMin = getRoundedValue(min); + getState().minValue = roundedMin; + + if (getMax() < roundedMin) { + getState().maxValue = roundedMin; + } + + if (getValue() < roundedMin) { + setValue(roundedMin); + } + } + + /** + * Get the current orientation of the slider (horizontal or vertical). + * + * @return {@link SliderOrientation#HORIZONTAL} or + * {@link SliderOrientation#VERTICAL} + */ + public SliderOrientation getOrientation() { + return getState(false).orientation; + } + + /** + * Set the orientation of the slider. + * + * @param orientation + * The new orientation, either + * {@link SliderOrientation#HORIZONTAL} or + * {@link SliderOrientation#VERTICAL} + */ + public void setOrientation(SliderOrientation orientation) { + getState().orientation = orientation; + } + + /** + * Get the current resolution of the slider. The resolution is the number of + * digits after the decimal point. + * + * @return resolution + */ + public int getResolution() { + return getState(false).resolution; + } + + /** + * Set a new resolution for the slider. The resolution is the number of + * digits after the decimal point. + * + * @throws IllegalArgumentException + * if resolution is negative. + * + * @param resolution + */ + public void setResolution(int resolution) { + if (resolution < 0) { + throw new IllegalArgumentException( + "Cannot set a negative resolution to Slider"); + } + getState().resolution = resolution; + } + + /** + * Sets the value of the slider. + * + * @param value + * The new value of the slider. + * @param repaintIsNotNeeded + * If true, client-side is not requested to repaint itself. + * @throws ValueOutOfBoundsException + * If the given value is not inside the range of the slider. + * @see #setMin(double) {@link #setMax(double)} + */ + @Override + protected void setValue(Double value, boolean repaintIsNotNeeded) { + 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); + getState().value = newFieldValue; + } + + /* + * Overridden to keep the shared state in sync with the AbstractField + * internal value. Should be removed once AbstractField is refactored to use + * shared state. + * + * See tickets #10921 and #11064. + */ + @Override + protected void setInternalValue(Double newValue) { + super.setInternalValue(newValue); + if (newValue == null) { + newValue = 0.0; + } + getState().value = newValue; + } + + /** + * Thrown when the value of the slider is about to be set to a value that is + * outside the valid range of the slider. + * + * @author Vaadin Ltd. + * + */ + public class ValueOutOfBoundsException extends RuntimeException { + + private final Double value; + + /** + * Constructs an <code>ValueOutOfBoundsException</code> with the + * specified detail message. + * + * @param valueOutOfBounds + */ + public ValueOutOfBoundsException(Double valueOutOfBounds) { + super(String.format("Value %s is out of bounds: [%s, %s]", + valueOutOfBounds, getMin(), getMax())); + value = valueOutOfBounds; + } + + /** + * Gets the value that is outside the valid range of the slider. + * + * @return the value that is out of bounds + */ + public Double getValue() { + return value; + } + } + + @Override + public Class<Double> getType() { + return Double.class; + } + + @Override + public void clear() { + super.setValue(Double.valueOf(getState().minValue)); + } + + @Override + public boolean isEmpty() { + // Slider is never really "empty" + return false; + } + + @Override + public void readDesign(Element design, DesignContext context) { + super.readDesign(design, context); + Attributes attr = design.attributes(); + if (attr.hasKey("vertical")) { + setOrientation(SliderOrientation.VERTICAL); + } + if (attr.hasKey("value")) { + Double newFieldValue = DesignAttributeHandler.readAttribute("value", + attr, Double.class); + setValue(newFieldValue, false, true); + } + } + + @Override + public void writeDesign(Element design, DesignContext context) { + super.writeDesign(design, context); + if (getOrientation() == SliderOrientation.VERTICAL) { + design.attr("vertical", true); + } + Slider defaultSlider = context.getDefaultInstance(this); + DesignAttributeHandler.writeAttribute(this, "value", + design.attributes(), defaultSlider); + } + + @Override + protected Collection<String> getCustomAttributes() { + Collection<String> result = super.getCustomAttributes(); + result.add("orientation"); + result.add("vertical"); + return result; + } +} diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/component/slider/SliderDeclarativeTest.java b/compatibility-server/src/test/java/com/vaadin/tests/server/component/slider/SliderDeclarativeTest.java new file mode 100644 index 0000000000..dcfb415810 --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/tests/server/component/slider/SliderDeclarativeTest.java @@ -0,0 +1,81 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * 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 com.vaadin.tests.server.component.slider; + +import org.junit.Test; + +import com.vaadin.shared.ui.slider.SliderOrientation; +import com.vaadin.tests.design.DeclarativeTestBase; +import com.vaadin.ui.Slider; + +/** + * Tests declarative support for implementations of {@link Slider}. + * + * @since + * @author Vaadin Ltd + */ +public class SliderDeclarativeTest extends DeclarativeTestBase<Slider> { + + @Test + public void testDefault() { + String design = "<vaadin-slider>"; + + Slider expected = new Slider(); + + testRead(design, expected); + testWrite(design, expected); + } + + @Test + public void testHorizontal() { + String design = "<vaadin-slider min=10 max=20 resolution=1 value=12.3>"; + + Slider expected = new Slider(); + expected.setMin(10.0); + expected.setMax(20.0); + expected.setResolution(1); + expected.setValue(12.3); + + testRead(design, expected); + testWrite(design, expected); + } + + @Test + public void testVertical() { + String design = "<vaadin-slider vertical>"; + + Slider expected = new Slider(); + expected.setOrientation(SliderOrientation.VERTICAL); + + testRead(design, expected); + testWrite(design, expected); + } + + @Test + public void testReadOnlyValue() { + String design = "<vaadin-slider readonly min=10 max=20 resolution=1 value=12.3>"; + + Slider expected = new Slider(); + expected.setMin(10.0); + expected.setMax(20.0); + expected.setResolution(1); + expected.setValue(12.3); + expected.setReadOnly(true); + + testRead(design, expected); + testWrite(design, expected); + } +} diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/component/slider/SliderTest.java b/compatibility-server/src/test/java/com/vaadin/tests/server/component/slider/SliderTest.java new file mode 100644 index 0000000000..8c093fdf72 --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/tests/server/component/slider/SliderTest.java @@ -0,0 +1,135 @@ +package com.vaadin.tests.server.component.slider; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.ui.Slider; + +public class SliderTest { + + @Test + public void minCannotBeLargerThanMax() { + Slider slider = new Slider(); + + slider.setMax(100); + slider.setMin(101); + + assertThat(slider.getMin(), is(101.0)); + assertThat(slider.getMax(), is(101.0)); + } + + @Test + public void maxCannotBeSmallerThanMin() { + Slider slider = new Slider(); + + slider.setMin(50); + slider.setMax(10); + + assertThat(slider.getMax(), is(10.0)); + assertThat(slider.getMin(), is(10.0)); + } + + @Test + public void valueOutOfBoundsExceptionMessageContainsBounds() { + Slider slider = new Slider(); + + try { + + slider.setValue(-1.0); + } catch (Slider.ValueOutOfBoundsException e) { + assertThat(e.getMessage(), + containsString("Value -1.0 is out of bounds: [0.0, 100.0]")); + } + } + + @Test + public void valueIsSet() { + Slider slider = new Slider(); + + slider.setValue(5.0); + + assertThat(slider.getValue(), is(5.0)); + } + + @Test + public void valueCannotBeOutOfBounds() { + Slider s = new Slider(0, 10); + + try { + s.setValue(20.0); + Assert.fail("Should throw out of bounds exception"); + } catch (Slider.ValueOutOfBoundsException e) { + // TODO: handle exception + } + } + + @Test + public void valueCanHaveLargePrecision() { + Slider slider = new Slider(); + slider.setResolution(20); + + slider.setValue(99.01234567891234567890123456789); + + 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)); + } +} |