Skip to content

Commit

Permalink
Convert Slider to extend the new AbstractField
Browse files Browse the repository at this point in the history
Change-Id: I1961963baac3a6546edb908582b323c481b15bfd
  • Loading branch information
Artur- authored and Vaadin Code Review committed Aug 9, 2016
1 parent f696db8 commit d41411e
Show file tree
Hide file tree
Showing 8 changed files with 358 additions and 273 deletions.
179 changes: 72 additions & 107 deletions 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
Expand All @@ -21,7 +21,6 @@
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;
Expand All @@ -30,10 +29,10 @@

/**
* 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() {

Expand All @@ -45,11 +44,11 @@ public void valueChanged(double value) {
* 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);
Expand All @@ -69,9 +68,10 @@ public void valueChanged(double value) {
};

/**
* 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();
Expand All @@ -81,12 +81,12 @@ public Slider() {

/**
* 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();
Expand All @@ -95,7 +95,7 @@ public Slider(String caption) {

/**
* Create a new slider with the given range and resolution.
*
*
* @param min
* The minimum value of the slider
* @param max
Expand All @@ -105,18 +105,20 @@ public Slider(String caption) {
*/
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();
Expand All @@ -126,15 +128,14 @@ public Slider(int min, int max) {
}

/**
* 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);
Expand All @@ -152,18 +153,18 @@ public SliderState getState(boolean markAsDirty) {
}

/**
* 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() {
return getState(false).maxValue;
}

/**
* 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
*/
Expand All @@ -181,18 +182,18 @@ public void setMax(double max) {
}

/**
* 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() {
return getState(false).minValue;
}

/**
* 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
*/
Expand All @@ -210,8 +211,8 @@ public void setMin(double min) {
}

/**
* 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}
*/
Expand All @@ -220,10 +221,10 @@ public SliderOrientation getOrientation() {
}

/**
* 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}
*/
Expand All @@ -232,10 +233,11 @@ public void setOrientation(SliderOrientation orientation) {
}

/**
* 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;
Expand All @@ -244,11 +246,12 @@ public int getResolution() {
/**
* 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) {
Expand All @@ -258,29 +261,6 @@ public void setResolution(int resolution) {
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();
Expand All @@ -294,33 +274,32 @@ private double getRoundedValue(Double value) {
}

@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 {

Expand All @@ -329,8 +308,9 @@ public class ValueOutOfBoundsException extends RuntimeException {
/**
* 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]",
Expand All @@ -340,30 +320,14 @@ public ValueOutOfBoundsException(Double 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);
Expand All @@ -372,9 +336,9 @@ public void readDesign(Element design, DesignContext context) {
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);
}
}

Expand All @@ -396,4 +360,5 @@ protected Collection<String> getCustomAttributes() {
result.add("vertical");
return result;
}

}

0 comments on commit d41411e

Please sign in to comment.