You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.ui;
  17. import java.util.Collection;
  18. import java.util.Objects;
  19. import org.jsoup.nodes.Attributes;
  20. import org.jsoup.nodes.Element;
  21. import com.vaadin.shared.ui.slider.SliderOrientation;
  22. import com.vaadin.shared.ui.slider.SliderServerRpc;
  23. import com.vaadin.shared.ui.slider.SliderState;
  24. import com.vaadin.ui.declarative.DesignAttributeHandler;
  25. import com.vaadin.ui.declarative.DesignContext;
  26. import elemental.json.Json;
  27. /**
  28. * A component for selecting a numerical value within a range.
  29. *
  30. * @author Vaadin Ltd.
  31. */
  32. public class Slider extends AbstractField<Double> {
  33. private SliderServerRpc rpc = (double value) -> {
  34. /*
  35. * Client side updates the state before sending the event so we need to
  36. * make sure the cached state is updated to match the client. If we do
  37. * not do this, a reverting setValue() call in a listener will not cause
  38. * the new state to be sent to the client.
  39. *
  40. * See #12133.
  41. */
  42. updateDiffstate("value", Json.create(value));
  43. try {
  44. setValue(value, true);
  45. } catch (final ValueOutOfBoundsException e) {
  46. // Convert to nearest bound
  47. double out = e.getValue().doubleValue();
  48. if (out < getState().minValue) {
  49. out = getState().minValue;
  50. }
  51. if (out > getState().maxValue) {
  52. out = getState().maxValue;
  53. }
  54. Slider.super.setValue(new Double(out), false);
  55. }
  56. };
  57. /**
  58. * Default slider constructor.
  59. * <p>
  60. * The range of the slider is set to 0-100 and only integer values are
  61. * allowed.
  62. */
  63. public Slider() {
  64. super();
  65. registerRpc(rpc);
  66. super.setValue(new Double(getState().minValue));
  67. }
  68. /**
  69. * Create a new slider with the caption given as parameter.
  70. * <p>
  71. * The range of the slider is set to 0-100 and only integer values are
  72. * allowed.
  73. *
  74. * @param caption
  75. * the caption for this slider (e.g. "Volume")
  76. */
  77. public Slider(String caption) {
  78. this();
  79. setCaption(caption);
  80. }
  81. /**
  82. * Create a new slider with the given range and resolution.
  83. *
  84. * @param min
  85. * The minimum value of the slider
  86. * @param max
  87. * The maximum value of the slider
  88. * @param resolution
  89. * The number of digits after the decimal point.
  90. */
  91. public Slider(double min, double max, int resolution) {
  92. this();
  93. // Need to set resolution first in order to not round min and max
  94. // to the default resolution (0)
  95. setResolution(resolution);
  96. setMax(max);
  97. setMin(min);
  98. }
  99. /**
  100. * Create a new slider with the given range of integers.
  101. *
  102. * @param min
  103. * the minimum value of the slider
  104. * @param max
  105. * the maximum value of the slider
  106. */
  107. public Slider(int min, int max) {
  108. this();
  109. setMin(min);
  110. setMax(max);
  111. setResolution(0);
  112. }
  113. /**
  114. * Creates a new slider with the given caption and integer range.
  115. *
  116. * @param caption
  117. * the caption for the slider
  118. * @param min
  119. * the minimum value of the slider
  120. * @param max
  121. * the maximum value of the slider
  122. */
  123. public Slider(String caption, int min, int max) {
  124. this(min, max);
  125. setCaption(caption);
  126. }
  127. @Override
  128. public SliderState getState() {
  129. return (SliderState) super.getState();
  130. }
  131. @Override
  132. public SliderState getState(boolean markAsDirty) {
  133. return (SliderState) super.getState(markAsDirty);
  134. }
  135. /**
  136. * Gets the maximum slider value. The default value is 100.0.
  137. *
  138. * @return the largest value the slider can have
  139. */
  140. public double getMax() {
  141. return getState(false).maxValue;
  142. }
  143. /**
  144. * Sets the maximum slider value. If the current value of the slider is
  145. * larger than this, the value is set to the new maximum.
  146. *
  147. * @param max
  148. * The new maximum slider value
  149. */
  150. public void setMax(double max) {
  151. double roundedMax = getRoundedValue(max);
  152. getState().maxValue = roundedMax;
  153. if (getMin() > roundedMax) {
  154. getState().minValue = roundedMax;
  155. }
  156. if (getValue() > roundedMax) {
  157. setValue(roundedMax);
  158. }
  159. }
  160. /**
  161. * Gets the minimum slider value. The default value is 0.0.
  162. *
  163. * @return the smallest value the slider can have
  164. */
  165. public double getMin() {
  166. return getState(false).minValue;
  167. }
  168. /**
  169. * Sets the minimum slider value. If the current value of the slider is
  170. * smaller than this, the value is set to the new minimum.
  171. *
  172. * @param min
  173. * The new minimum slider value
  174. */
  175. public void setMin(double min) {
  176. double roundedMin = getRoundedValue(min);
  177. getState().minValue = roundedMin;
  178. if (getMax() < roundedMin) {
  179. getState().maxValue = roundedMin;
  180. }
  181. if (getValue() < roundedMin) {
  182. setValue(roundedMin);
  183. }
  184. }
  185. /**
  186. * Gets the current orientation of the slider (horizontal or vertical).
  187. *
  188. * @return {@link SliderOrientation#HORIZONTAL} or
  189. * {@link SliderOrientation#VERTICAL}
  190. */
  191. public SliderOrientation getOrientation() {
  192. return getState(false).orientation;
  193. }
  194. /**
  195. * Sets the orientation of the slider.
  196. *
  197. * @param orientation
  198. * the new orientation, either
  199. * {@link SliderOrientation#HORIZONTAL} or
  200. * {@link SliderOrientation#VERTICAL}
  201. */
  202. public void setOrientation(SliderOrientation orientation) {
  203. getState().orientation = orientation;
  204. }
  205. /**
  206. * Gets the resolution of the slider. The resolution is the number of digits
  207. * after the decimal point. The default resolution is 0 (only integers
  208. * allowed).
  209. *
  210. * @return resolution the number of digits after the decimal point
  211. */
  212. public int getResolution() {
  213. return getState(false).resolution;
  214. }
  215. /**
  216. * Set a new resolution for the slider. The resolution is the number of
  217. * digits after the decimal point.
  218. *
  219. * @throws IllegalArgumentException
  220. * if resolution is negative.
  221. *
  222. * @param resolution
  223. * the number of digits after the decimal point
  224. */
  225. public void setResolution(int resolution) {
  226. if (resolution < 0) {
  227. throw new IllegalArgumentException(
  228. "Cannot set a negative resolution to Slider");
  229. }
  230. getState().resolution = resolution;
  231. }
  232. /**
  233. * Sets the slider to update its value when the user clicks on it.
  234. * By default, the slider value is updated by dragging the slider's handle
  235. * or clicking arrows.
  236. *
  237. * @param updateValueOnClick
  238. * {@code true} to update the value of the slider on click,
  239. * {@code false} otherwise.
  240. * @since 8.8
  241. */
  242. public void setUpdateValueOnClick(boolean updateValueOnClick) {
  243. getState().updateValueOnClick = updateValueOnClick;
  244. }
  245. /**
  246. * Returns whether the slider updates its value on user click.
  247. *
  248. * @return {@code true} if the Slider updates its value on click. By
  249. * default, returns {@code false}
  250. * @since 8.8
  251. */
  252. public boolean isUpdateValueOnClick() {
  253. return getState(false).updateValueOnClick;
  254. }
  255. private double getRoundedValue(Double value) {
  256. final double v = value.doubleValue();
  257. final int resolution = getResolution();
  258. double ratio = Math.pow(10, resolution);
  259. if (v >= 0) {
  260. return Math.floor(v * ratio) / ratio;
  261. } else {
  262. return Math.ceil(v * ratio) / ratio;
  263. }
  264. }
  265. @Override
  266. protected void doSetValue(Double newValue) {
  267. double trimmedValue;
  268. if (newValue == null) {
  269. trimmedValue = 0.0;
  270. } else {
  271. trimmedValue = getRoundedValue(newValue);
  272. }
  273. if (getMin() > trimmedValue || getMax() < trimmedValue) {
  274. throw new ValueOutOfBoundsException(trimmedValue);
  275. }
  276. getState().value = trimmedValue;
  277. }
  278. /**
  279. * Sets the value of this object. If the new value is not equal to
  280. * {@code getValue()}, fires a {@link ValueChangeEvent}. Throws
  281. * {@code NullPointerException} if the value is null.
  282. *
  283. * @param value
  284. * the new value, not {@code null}
  285. * @throws NullPointerException
  286. * if {@code value} is {@code null}
  287. */
  288. @Override
  289. public void setValue(Double value) {
  290. Objects.requireNonNull(value, "Value cannot be null");
  291. super.setValue(value);
  292. }
  293. @Override
  294. public Double getValue() {
  295. return getState(false).value;
  296. }
  297. @Override
  298. public Double getEmptyValue() {
  299. return getMin();
  300. }
  301. /**
  302. * Thrown when the value of the slider is about to be set to a value that is
  303. * outside the valid range of the slider.
  304. *
  305. * @author Vaadin Ltd.
  306. *
  307. */
  308. public class ValueOutOfBoundsException extends RuntimeException {
  309. private final Double value;
  310. /**
  311. * Constructs an <code>ValueOutOfBoundsException</code> with the
  312. * specified detail message.
  313. *
  314. * @param valueOutOfBounds
  315. * the value of the slider
  316. */
  317. public ValueOutOfBoundsException(Double valueOutOfBounds) {
  318. super(String.format("Value %s is out of bounds: [%s, %s]",
  319. valueOutOfBounds, getMin(), getMax()));
  320. value = valueOutOfBounds;
  321. }
  322. /**
  323. * Gets the value that is outside the valid range of the slider.
  324. *
  325. * @return the value that is out of bounds
  326. */
  327. public Double getValue() {
  328. return value;
  329. }
  330. }
  331. @Override
  332. public void readDesign(Element design, DesignContext context) {
  333. super.readDesign(design, context);
  334. Attributes attr = design.attributes();
  335. if (attr.hasKey("vertical")) {
  336. setOrientation(SliderOrientation.VERTICAL);
  337. }
  338. if (attr.hasKey("value")) {
  339. Double newFieldValue = DesignAttributeHandler.readAttribute("value",
  340. attr, Double.class);
  341. setValue(newFieldValue);
  342. }
  343. }
  344. @Override
  345. public void writeDesign(Element design, DesignContext context) {
  346. super.writeDesign(design, context);
  347. if (getOrientation() == SliderOrientation.VERTICAL) {
  348. design.attr("vertical", true);
  349. }
  350. Slider defaultSlider = context.getDefaultInstance(this);
  351. DesignAttributeHandler.writeAttribute(this, "value",
  352. design.attributes(), defaultSlider, context);
  353. }
  354. @Override
  355. protected Collection<String> getCustomAttributes() {
  356. Collection<String> result = super.getCustomAttributes();
  357. result.add("orientation");
  358. result.add("vertical");
  359. return result;
  360. }
  361. }