diff options
author | Artur Signell <artur@vaadin.com> | 2016-08-08 15:56:33 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2016-08-09 11:08:53 +0000 |
commit | d41411e0f51f29c87b1631e06d46db3399af3266 (patch) | |
tree | 4ea567b0e5d6c4f017f82da00b32972faef556b5 /server | |
parent | f696db8036fc520f3bd97ef75e933ff8b45e1ce2 (diff) | |
download | vaadin-framework-d41411e0f51f29c87b1631e06d46db3399af3266.tar.gz vaadin-framework-d41411e0f51f29c87b1631e06d46db3399af3266.zip |
Convert Slider to extend the new AbstractField
Change-Id: I1961963baac3a6546edb908582b323c481b15bfd
Diffstat (limited to 'server')
3 files changed, 166 insertions, 226 deletions
diff --git a/server/src/main/java/com/vaadin/ui/Slider.java b/server/src/main/java/com/vaadin/ui/Slider.java index fbb9aa292a..a55f6ed41d 100644 --- a/server/src/main/java/com/vaadin/ui/Slider.java +++ b/server/src/main/java/com/vaadin/ui/Slider.java @@ -1,12 +1,12 @@ /* * 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 @@ -21,7 +21,6 @@ import java.util.Collection; import org.jsoup.nodes.Attributes; import org.jsoup.nodes.Element; -import com.vaadin.legacy.ui.LegacyAbstractField; import com.vaadin.shared.ui.slider.SliderOrientation; import com.vaadin.shared.ui.slider.SliderServerRpc; import com.vaadin.shared.ui.slider.SliderState; @@ -30,10 +29,10 @@ import com.vaadin.ui.declarative.DesignContext; /** * A component for selecting a numerical value within a range. - * + * * @author Vaadin Ltd. */ -public class Slider extends LegacyAbstractField<Double> { +public class Slider extends AbstractField<Double> { private SliderServerRpc rpc = new SliderServerRpc() { @@ -45,11 +44,11 @@ public class Slider extends LegacyAbstractField<Double> { * 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); + getUI().getConnectorTracker().getDiffState(Slider.this).put("value", + value); try { setValue(value, true); @@ -69,9 +68,10 @@ public class Slider extends LegacyAbstractField<Double> { }; /** - * Default slider constructor. Sets all values to defaults and the slide - * handle at minimum value. - * + * Default slider constructor. + * <p> + * The range of the slider is set to 0-100 and only integer values are + * allowed. */ public Slider() { super(); @@ -81,12 +81,12 @@ public class Slider extends LegacyAbstractField<Double> { /** * Create a new slider with the caption given as parameter. - * + * <p> * 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"). + * the caption for this slider (e.g. "Volume") */ public Slider(String caption) { this(); @@ -95,7 +95,7 @@ public class Slider extends LegacyAbstractField<Double> { /** * Create a new slider with the given range and resolution. - * + * * @param min * The minimum value of the slider * @param max @@ -105,18 +105,20 @@ public class Slider extends LegacyAbstractField<Double> { */ public Slider(double min, double max, int resolution) { this(); + // Need to set resolution first in order to not round min and max + // to the default resolution (0) setResolution(resolution); setMax(max); setMin(min); } /** - * Create a new slider with the given range that only allows integer values. - * + * Create a new slider with the given range of integers. + * * @param min - * The minimum value of the slider + * the minimum value of the slider * @param max - * The maximum value of the slider + * the maximum value of the slider */ public Slider(int min, int max) { this(); @@ -126,15 +128,14 @@ public class Slider extends LegacyAbstractField<Double> { } /** - * Create a new slider with the given caption and range that only allows - * integer values. - * + * Creates a new slider with the given caption and integer range. + * * @param caption - * The caption for the slider + * the caption for the slider * @param min - * The minimum value of the slider + * the minimum value of the slider * @param max - * The maximum value of the slider + * the maximum value of the slider */ public Slider(String caption, int min, int max) { this(min, max); @@ -152,8 +153,8 @@ public class Slider extends LegacyAbstractField<Double> { } /** - * Gets the maximum slider value - * + * Gets the maximum slider value. The default value is 100.0. + * * @return the largest value the slider can have */ public double getMax() { @@ -161,9 +162,9 @@ public class Slider extends LegacyAbstractField<Double> { } /** - * Set the maximum slider value. If the current value of the slider is + * Sets 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 */ @@ -181,8 +182,8 @@ public class Slider extends LegacyAbstractField<Double> { } /** - * Gets the minimum slider value - * + * Gets the minimum slider value. The default value is 0.0. + * * @return the smallest value the slider can have */ public double getMin() { @@ -190,9 +191,9 @@ public class Slider extends LegacyAbstractField<Double> { } /** - * Set the minimum slider value. If the current value of the slider is + * Sets 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 */ @@ -210,8 +211,8 @@ public class Slider extends LegacyAbstractField<Double> { } /** - * Get the current orientation of the slider (horizontal or vertical). - * + * Gets the current orientation of the slider (horizontal or vertical). + * * @return {@link SliderOrientation#HORIZONTAL} or * {@link SliderOrientation#VERTICAL} */ @@ -220,10 +221,10 @@ public class Slider extends LegacyAbstractField<Double> { } /** - * Set the orientation of the slider. - * + * Sets the orientation of the slider. + * * @param orientation - * The new orientation, either + * the new orientation, either * {@link SliderOrientation#HORIZONTAL} or * {@link SliderOrientation#VERTICAL} */ @@ -232,10 +233,11 @@ public class Slider extends LegacyAbstractField<Double> { } /** - * Get the current resolution of the slider. The resolution is the number of - * digits after the decimal point. - * - * @return resolution + * Gets the resolution of the slider. The resolution is the number of digits + * after the decimal point. The default resolution is 0 (only integers + * allowed). + * + * @return resolution the number of digits after the decimal point */ public int getResolution() { return getState(false).resolution; @@ -244,11 +246,12 @@ public class Slider extends LegacyAbstractField<Double> { /** * 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 + * the number of digits after the decimal point */ public void setResolution(int resolution) { if (resolution < 0) { @@ -258,29 +261,6 @@ public class Slider extends LegacyAbstractField<Double> { 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(); @@ -294,33 +274,32 @@ public class Slider extends LegacyAbstractField<Double> { } @Override - public void setValue(Double newFieldValue) { - super.setValue(newFieldValue); - getState().value = newFieldValue; + protected void doSetValue(Double newValue) { + double trimmedValue; + if (newValue == null) { + trimmedValue = 0.0; + } else { + trimmedValue = getRoundedValue(newValue); + } + + if (getMin() > trimmedValue || getMax() < trimmedValue) { + throw new ValueOutOfBoundsException(trimmedValue); + } + + getState().value = trimmedValue; } - /* - * Overridden to keep the shared state in sync with the LegacyAbstractField - * internal value. Should be removed once LegacyAbstractField 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; + public Double getValue() { + return getState().value; } /** * 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 { @@ -329,8 +308,9 @@ public class Slider extends LegacyAbstractField<Double> { /** * Constructs an <code>ValueOutOfBoundsException</code> with the * specified detail message. - * + * * @param valueOutOfBounds + * the value of the slider */ public ValueOutOfBoundsException(Double valueOutOfBounds) { super(String.format("Value %s is out of bounds: [%s, %s]", @@ -340,7 +320,7 @@ public class Slider extends LegacyAbstractField<Double> { /** * Gets the value that is outside the valid range of the slider. - * + * * @return the value that is out of bounds */ public Double getValue() { @@ -349,22 +329,6 @@ public class Slider extends LegacyAbstractField<Double> { } @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(); @@ -372,9 +336,9 @@ public class Slider extends LegacyAbstractField<Double> { setOrientation(SliderOrientation.VERTICAL); } if (attr.hasKey("value")) { - Double newFieldValue = DesignAttributeHandler.readAttribute( - "value", attr, Double.class); - setValue(newFieldValue, false, true); + Double newFieldValue = DesignAttributeHandler.readAttribute("value", + attr, Double.class); + setValue(newFieldValue); } } @@ -396,4 +360,5 @@ public class Slider extends LegacyAbstractField<Double> { result.add("vertical"); return result; } + } diff --git a/server/src/main/java/com/vaadin/ui/components/colorpicker/ColorPickerPopup.java b/server/src/main/java/com/vaadin/ui/components/colorpicker/ColorPickerPopup.java index 86ac0111ad..fcbeb43005 100644 --- a/server/src/main/java/com/vaadin/ui/components/colorpicker/ColorPickerPopup.java +++ b/server/src/main/java/com/vaadin/ui/components/colorpicker/ColorPickerPopup.java @@ -1,12 +1,12 @@ /* * 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 @@ -25,8 +25,6 @@ import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; -import com.vaadin.data.Property.ValueChangeEvent; -import com.vaadin.data.Property.ValueChangeListener; import com.vaadin.shared.ui.MarginInfo; import com.vaadin.shared.ui.colorpicker.Color; import com.vaadin.ui.AbstractColorPicker.Coordinates2Color; @@ -45,11 +43,11 @@ import com.vaadin.ui.Window; /** * A component that represents color selection popup within a color picker. - * + * * @since 7.0.0 */ -public class ColorPickerPopup extends Window implements ClickListener, - ColorChangeListener, ColorSelector { +public class ColorPickerPopup extends Window + implements ClickListener, ColorChangeListener, ColorSelector { private static final String STYLENAME = "v-colorpicker-popup"; @@ -256,7 +254,7 @@ public class ColorPickerPopup extends Window implements ClickListener, /** * Creates the RGB tab. - * + * * @return the component */ private Component createRGBTab(Color color) { @@ -281,42 +279,33 @@ public class ColorPickerPopup extends Window implements ClickListener, blueSlider = createRGBSlider("Blue", "blue"); setRgbSliderValues(color); - redSlider.addValueChangeListener(new ValueChangeListener() { - @Override - public void valueChange(ValueChangeEvent event) { - double red = (Double) event.getProperty().getValue(); - if (!updatingColors) { - Color newColor = new Color((int) red, selectedColor - .getGreen(), selectedColor.getBlue()); - setColor(newColor); - } + redSlider.addValueChangeListener(e -> { + double red = e.getValue(); + if (!updatingColors) { + Color newColor = new Color((int) red, selectedColor.getGreen(), + selectedColor.getBlue()); + setColor(newColor); } }); sliders.addComponent(redSlider); - greenSlider.addValueChangeListener(new ValueChangeListener() { - @Override - public void valueChange(ValueChangeEvent event) { - double green = (Double) event.getProperty().getValue(); - if (!updatingColors) { - Color newColor = new Color(selectedColor.getRed(), - (int) green, selectedColor.getBlue()); - setColor(newColor); - } + greenSlider.addValueChangeListener(e -> { + double green = e.getValue(); + if (!updatingColors) { + Color newColor = new Color(selectedColor.getRed(), (int) green, + selectedColor.getBlue()); + setColor(newColor); } }); sliders.addComponent(greenSlider); - blueSlider.addValueChangeListener(new ValueChangeListener() { - @Override - public void valueChange(ValueChangeEvent event) { - double blue = (Double) event.getProperty().getValue(); - if (!updatingColors) { - Color newColor = new Color(selectedColor.getRed(), - selectedColor.getGreen(), (int) blue); - setColor(newColor); - } + blueSlider.addValueChangeListener(e -> { + double blue = e.getValue(); + if (!updatingColors) { + Color newColor = new Color(selectedColor.getRed(), + selectedColor.getGreen(), (int) blue); + setColor(newColor); } }); sliders.addComponent(blueSlider); @@ -337,7 +326,7 @@ public class ColorPickerPopup extends Window implements ClickListener, /** * Creates the hsv tab. - * + * * @return the component */ private Component createHSVTab(Color color) { @@ -367,30 +356,28 @@ public class ColorPickerPopup extends Window implements ClickListener, hueSlider.addStyleName("hue-slider"); hueSlider.setWidth("220px"); hueSlider.setImmediate(true); - hueSlider.addValueChangeListener(new ValueChangeListener() { - @Override - public void valueChange(ValueChangeEvent event) { - if (!updatingColors) { - float hue = (Float.parseFloat(event.getProperty() - .getValue().toString())) / 360f; - float saturation = (Float.parseFloat(saturationSlider - .getValue().toString())) / 100f; - float value = (Float.parseFloat(valueSlider.getValue() - .toString())) / 100f; - - // Set the color - Color color = new Color(Color.HSVtoRGB(hue, saturation, - value)); - setColor(color); - - /* - * Set the background color of the hue gradient. This has to - * be done here since in the conversion the base color - * information is lost when color is black/white - */ - Color bgColor = new Color(Color.HSVtoRGB(hue, 1f, 1f)); - hsvGradient.setBackgroundColor(bgColor); - } + hueSlider.addValueChangeListener(event -> { + if (!updatingColors) { + float hue = (Float.parseFloat(event.getValue().toString())) + / 360f; + float saturation = (Float + .parseFloat(saturationSlider.getValue().toString())) + / 100f; + float value = (Float + .parseFloat(valueSlider.getValue().toString())) / 100f; + + // Set the color + Color newColor = new Color( + Color.HSVtoRGB(hue, saturation, value)); + setColor(newColor); + + /* + * Set the background color of the hue gradient. This has to be + * done here since in the conversion the base color information + * is lost when color is black/white + */ + Color bgColor = new Color(Color.HSVtoRGB(hue, 1f, 1f)); + hsvGradient.setBackgroundColor(bgColor); } }); sliders.addComponent(hueSlider); @@ -398,20 +385,17 @@ public class ColorPickerPopup extends Window implements ClickListener, saturationSlider.setStyleName("hsv-slider"); saturationSlider.setWidth("220px"); saturationSlider.setImmediate(true); - saturationSlider.addValueChangeListener(new ValueChangeListener() { - @Override - public void valueChange(ValueChangeEvent event) { - if (!updatingColors) { - float hue = (Float.parseFloat(hueSlider.getValue() - .toString())) / 360f; - float saturation = (Float.parseFloat(event.getProperty() - .getValue().toString())) / 100f; - float value = (Float.parseFloat(valueSlider.getValue() - .toString())) / 100f; - Color color = new Color(Color.HSVtoRGB(hue, saturation, - value)); - setColor(color); - } + saturationSlider.addValueChangeListener(event -> { + if (!updatingColors) { + float hue = (Float.parseFloat(hueSlider.getValue().toString())) + / 360f; + float saturation = (Float + .parseFloat(event.getValue().toString())) / 100f; + float value = (Float + .parseFloat(valueSlider.getValue().toString())) / 100f; + Color newColor = new Color( + Color.HSVtoRGB(hue, saturation, value)); + setColor(newColor); } }); sliders.addComponent(saturationSlider); @@ -419,21 +403,19 @@ public class ColorPickerPopup extends Window implements ClickListener, valueSlider.setStyleName("hsv-slider"); valueSlider.setWidth("220px"); valueSlider.setImmediate(true); - valueSlider.addValueChangeListener(new ValueChangeListener() { - @Override - public void valueChange(ValueChangeEvent event) { - if (!updatingColors) { - float hue = (Float.parseFloat(hueSlider.getValue() - .toString())) / 360f; - float saturation = (Float.parseFloat(saturationSlider - .getValue().toString())) / 100f; - float value = (Float.parseFloat(event.getProperty() - .getValue().toString())) / 100f; - - Color color = new Color(Color.HSVtoRGB(hue, saturation, - value)); - setColor(color); - } + valueSlider.addValueChangeListener(event -> { + if (!updatingColors) { + float hue = (Float.parseFloat(hueSlider.getValue().toString())) + / 360f; + float saturation = (Float + .parseFloat(saturationSlider.getValue().toString())) + / 100f; + float value = (Float.parseFloat(event.getValue().toString())) + / 100f; + + Color newColor = new Color( + Color.HSVtoRGB(hue, saturation, value)); + setColor(newColor); } }); @@ -445,7 +427,7 @@ public class ColorPickerPopup extends Window implements ClickListener, /** * Creates the select tab. - * + * * @return the component */ private Component createSelectTab() { @@ -504,7 +486,7 @@ public class ColorPickerPopup extends Window implements ClickListener, /** * Gets the history. - * + * * @return the history */ public ColorPickerHistory getHistory() { @@ -535,7 +517,7 @@ public class ColorPickerPopup extends Window implements ClickListener, /** * Gets the color history. - * + * * @return the color history */ public List<Color> getColorHistory() { @@ -568,10 +550,10 @@ public class ColorPickerPopup extends Window implements ClickListener, blueSlider.setValue(((Integer) color.getBlue()).doubleValue()); greenSlider.setValue(((Integer) color.getGreen()).doubleValue()); } catch (ValueOutOfBoundsException e) { - getLogger().log( - Level.WARNING, + getLogger().log(Level.WARNING, "Unable to set RGB color value to " + color.getRed() + "," - + color.getGreen() + "," + color.getBlue(), e); + + color.getGreen() + "," + color.getBlue(), + e); } } @@ -581,10 +563,8 @@ public class ColorPickerPopup extends Window implements ClickListener, saturationSlider.setValue(((Float) (hsv[1] * 100f)).doubleValue()); valueSlider.setValue(((Float) (hsv[2] * 100f)).doubleValue()); } catch (ValueOutOfBoundsException e) { - getLogger().log( - Level.WARNING, - "Unable to set HSV color value to " + hsv[0] + "," + hsv[1] - + "," + hsv[2], e); + getLogger().log(Level.WARNING, "Unable to set HSV color value to " + + hsv[0] + "," + hsv[1] + "," + hsv[2], e); } } @@ -600,7 +580,7 @@ public class ColorPickerPopup extends Window implements ClickListener, /** * Checks the visibility of the given tab - * + * * @param tab * The tab to check * @return true if tab is visible, false otherwise @@ -617,7 +597,7 @@ public class ColorPickerPopup extends Window implements ClickListener, /** * How many tabs are visible - * + * * @return The number of tabs visible */ private int tabsNumVisible() { @@ -639,7 +619,7 @@ public class ColorPickerPopup extends Window implements ClickListener, /** * Set RGB tab visibility - * + * * @param visible * The visibility of the RGB tab */ @@ -655,7 +635,7 @@ public class ColorPickerPopup extends Window implements ClickListener, /** * Set HSV tab visibility - * + * * @param visible * The visibility of the HSV tab */ @@ -671,7 +651,7 @@ public class ColorPickerPopup extends Window implements ClickListener, /** * Set Swatches tab visibility - * + * * @param visible * The visibility of the Swatches tab */ @@ -687,7 +667,7 @@ public class ColorPickerPopup extends Window implements ClickListener, /** * Set the History visibility - * + * * @param visible */ public void setHistoryVisible(boolean visible) { @@ -697,7 +677,7 @@ public class ColorPickerPopup extends Window implements ClickListener, /** * Set the preview visibility - * + * * @param visible */ public void setPreviewVisible(boolean visible) { @@ -765,7 +745,8 @@ public class ColorPickerPopup extends Window implements ClickListener, public Color calculate(int x, int y) { float saturation = 1f - (y / 220.0f); float value = (x / 220.0f); - float hue = Float.parseFloat(hueSlider.getValue().toString()) / 360f; + float hue = Float.parseFloat(hueSlider.getValue().toString()) + / 360f; Color color = new Color(Color.HSVtoRGB(hue, saturation, value)); return color; diff --git a/server/src/test/java/com/vaadin/tests/server/component/slider/SliderTest.java b/server/src/test/java/com/vaadin/tests/server/component/slider/SliderTest.java index 8c093fdf72..0ce40cad39 100644 --- a/server/src/test/java/com/vaadin/tests/server/component/slider/SliderTest.java +++ b/server/src/test/java/com/vaadin/tests/server/component/slider/SliderTest.java @@ -4,10 +4,10 @@ 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; +import com.vaadin.ui.Slider.ValueOutOfBoundsException; public class SliderTest { @@ -41,8 +41,8 @@ public class SliderTest { slider.setValue(-1.0); } catch (Slider.ValueOutOfBoundsException e) { - assertThat(e.getMessage(), - containsString("Value -1.0 is out of bounds: [0.0, 100.0]")); + assertThat(e.getMessage(), containsString( + "Value -1.0 is out of bounds: [0.0, 100.0]")); } } @@ -55,16 +55,10 @@ public class SliderTest { assertThat(slider.getValue(), is(5.0)); } - @Test + @Test(expected = ValueOutOfBoundsException.class) 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 - } + s.setValue(20.0); } @Test |