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.

Slider.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Copyright 2000-2014 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 org.jsoup.nodes.Attributes;
  19. import org.jsoup.nodes.Element;
  20. import com.vaadin.shared.ui.slider.SliderOrientation;
  21. import com.vaadin.shared.ui.slider.SliderServerRpc;
  22. import com.vaadin.shared.ui.slider.SliderState;
  23. import com.vaadin.ui.declarative.DesignAttributeHandler;
  24. import com.vaadin.ui.declarative.DesignContext;
  25. /**
  26. * A component for selecting a numerical value within a range.
  27. *
  28. * @author Vaadin Ltd.
  29. */
  30. public class Slider extends AbstractField<Double> {
  31. private SliderServerRpc rpc = new SliderServerRpc() {
  32. @Override
  33. public void valueChanged(double value) {
  34. /*
  35. * Client side updates the state before sending the event so we need
  36. * to make sure the cached state is updated to match the client. If
  37. * we do not do this, a reverting setValue() call in a listener will
  38. * not cause the new state to be sent to the client.
  39. *
  40. * See #12133.
  41. */
  42. getUI().getConnectorTracker().getDiffState(Slider.this)
  43. .put("value", value);
  44. try {
  45. setValue(value, true);
  46. } catch (final ValueOutOfBoundsException e) {
  47. // Convert to nearest bound
  48. double out = e.getValue().doubleValue();
  49. if (out < getState().minValue) {
  50. out = getState().minValue;
  51. }
  52. if (out > getState().maxValue) {
  53. out = getState().maxValue;
  54. }
  55. Slider.super.setValue(new Double(out), false);
  56. }
  57. }
  58. };
  59. /**
  60. * Default slider constructor. Sets all values to defaults and the slide
  61. * handle at minimum value.
  62. *
  63. */
  64. public Slider() {
  65. super();
  66. registerRpc(rpc);
  67. super.setValue(new Double(getState().minValue));
  68. }
  69. /**
  70. * Create a new slider with the caption given as parameter.
  71. *
  72. * The range of the slider is set to 0-100 and only integer values are
  73. * allowed.
  74. *
  75. * @param caption
  76. * The caption for this slider (e.g. "Volume").
  77. */
  78. public Slider(String caption) {
  79. this();
  80. setCaption(caption);
  81. }
  82. /**
  83. * Create a new slider with the given range and resolution.
  84. *
  85. * @param min
  86. * The minimum value of the slider
  87. * @param max
  88. * The maximum value of the slider
  89. * @param resolution
  90. * The number of digits after the decimal point.
  91. */
  92. public Slider(double min, double max, int resolution) {
  93. this();
  94. setResolution(resolution);
  95. setMax(max);
  96. setMin(min);
  97. }
  98. /**
  99. * Create a new slider with the given range that only allows integer values.
  100. *
  101. * @param min
  102. * The minimum value of the slider
  103. * @param max
  104. * The maximum value of the slider
  105. */
  106. public Slider(int min, int max) {
  107. this();
  108. setMin(min);
  109. setMax(max);
  110. setResolution(0);
  111. }
  112. /**
  113. * Create a new slider with the given caption and range that only allows
  114. * integer values.
  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
  137. *
  138. * @return the largest value the slider can have
  139. */
  140. public double getMax() {
  141. return getState(false).maxValue;
  142. }
  143. /**
  144. * Set 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
  162. *
  163. * @return the smallest value the slider can have
  164. */
  165. public double getMin() {
  166. return getState(false).minValue;
  167. }
  168. /**
  169. * Set 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. * Get 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. * Set 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. * Get the current resolution of the slider. The resolution is the number of
  207. * digits after the decimal point.
  208. *
  209. * @return resolution
  210. */
  211. public int getResolution() {
  212. return getState(false).resolution;
  213. }
  214. /**
  215. * Set a new resolution for the slider. The resolution is the number of
  216. * digits after the decimal point.
  217. *
  218. * @throws IllegalArgumentException
  219. * if resolution is negative.
  220. *
  221. * @param resolution
  222. */
  223. public void setResolution(int resolution) {
  224. if (resolution < 0) {
  225. throw new IllegalArgumentException(
  226. "Cannot set a negative resolution to Slider");
  227. }
  228. getState().resolution = resolution;
  229. }
  230. /**
  231. * Sets the value of the slider.
  232. *
  233. * @param value
  234. * The new value of the slider.
  235. * @param repaintIsNotNeeded
  236. * If true, client-side is not requested to repaint itself.
  237. * @throws ValueOutOfBoundsException
  238. * If the given value is not inside the range of the slider.
  239. * @see #setMin(double) {@link #setMax(double)}
  240. */
  241. @Override
  242. protected void setValue(Double value, boolean repaintIsNotNeeded) {
  243. double newValue = getRoundedValue(value);
  244. if (getMin() > newValue || getMax() < newValue) {
  245. throw new ValueOutOfBoundsException(newValue);
  246. }
  247. getState().value = newValue;
  248. super.setValue(newValue, repaintIsNotNeeded);
  249. }
  250. private double getRoundedValue(Double value) {
  251. final double v = value.doubleValue();
  252. final int resolution = getResolution();
  253. double ratio = Math.pow(10, resolution);
  254. if (v >= 0) {
  255. return Math.floor(v * ratio) / ratio;
  256. } else {
  257. return Math.ceil(v * ratio) / ratio;
  258. }
  259. }
  260. @Override
  261. public void setValue(Double newFieldValue) {
  262. super.setValue(newFieldValue);
  263. getState().value = newFieldValue;
  264. }
  265. /*
  266. * Overridden to keep the shared state in sync with the AbstractField
  267. * internal value. Should be removed once AbstractField is refactored to use
  268. * shared state.
  269. *
  270. * See tickets #10921 and #11064.
  271. */
  272. @Override
  273. protected void setInternalValue(Double newValue) {
  274. super.setInternalValue(newValue);
  275. if (newValue == null) {
  276. newValue = 0.0;
  277. }
  278. getState().value = newValue;
  279. }
  280. /**
  281. * Thrown when the value of the slider is about to be set to a value that is
  282. * outside the valid range of the slider.
  283. *
  284. * @author Vaadin Ltd.
  285. *
  286. */
  287. public class ValueOutOfBoundsException extends RuntimeException {
  288. private final Double value;
  289. /**
  290. * Constructs an <code>ValueOutOfBoundsException</code> with the
  291. * specified detail message.
  292. *
  293. * @param valueOutOfBounds
  294. */
  295. public ValueOutOfBoundsException(Double valueOutOfBounds) {
  296. super(String.format("Value %s is out of bounds: [%s, %s]",
  297. valueOutOfBounds, getMin(), getMax()));
  298. value = valueOutOfBounds;
  299. }
  300. /**
  301. * Gets the value that is outside the valid range of the slider.
  302. *
  303. * @return the value that is out of bounds
  304. */
  305. public Double getValue() {
  306. return value;
  307. }
  308. }
  309. @Override
  310. public Class<Double> getType() {
  311. return Double.class;
  312. }
  313. @Override
  314. public void clear() {
  315. super.setValue(Double.valueOf(getState().minValue));
  316. }
  317. @Override
  318. public boolean isEmpty() {
  319. // Slider is never really "empty"
  320. return false;
  321. }
  322. @Override
  323. public void readDesign(Element design, DesignContext context) {
  324. super.readDesign(design, context);
  325. Attributes attr = design.attributes();
  326. if (attr.hasKey("vertical")) {
  327. setOrientation(SliderOrientation.VERTICAL);
  328. }
  329. if (attr.hasKey("value")) {
  330. Double newFieldValue = DesignAttributeHandler.readAttribute(
  331. "value", attr, Double.class);
  332. setValue(newFieldValue, false, true);
  333. }
  334. }
  335. @Override
  336. public void writeDesign(Element design, DesignContext context) {
  337. super.writeDesign(design, context);
  338. if (getOrientation() == SliderOrientation.VERTICAL) {
  339. design.attr("vertical", true);
  340. }
  341. Slider defaultSlider = context.getDefaultInstance(this);
  342. DesignAttributeHandler.writeAttribute(this, "value",
  343. design.attributes(), defaultSlider);
  344. }
  345. @Override
  346. protected Collection<String> getCustomAttributes() {
  347. Collection<String> result = super.getCustomAttributes();
  348. result.add("orientation");
  349. return result;
  350. }
  351. }