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 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 org.json.JSONException;
  18. import com.vaadin.shared.ui.slider.SliderOrientation;
  19. import com.vaadin.shared.ui.slider.SliderServerRpc;
  20. import com.vaadin.shared.ui.slider.SliderState;
  21. /**
  22. * A component for selecting a numerical value within a range.
  23. *
  24. * @author Vaadin Ltd.
  25. */
  26. public class Slider extends AbstractField<Double> {
  27. private SliderServerRpc rpc = new SliderServerRpc() {
  28. @Override
  29. public void valueChanged(double value) {
  30. /*
  31. * Client side updates the state before sending the event so we need
  32. * to make sure the cached state is updated to match the client. If
  33. * we do not do this, a reverting setValue() call in a listener will
  34. * not cause the new state to be sent to the client.
  35. *
  36. * See #12133.
  37. */
  38. try {
  39. getUI().getConnectorTracker().getDiffState(Slider.this)
  40. .put("value", value);
  41. } catch (JSONException e) {
  42. throw new RuntimeException(e);
  43. }
  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. setMin(min);
  95. setMax(max);
  96. setResolution(resolution);
  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. getState().maxValue = max;
  152. if (getValue() > max) {
  153. setValue(max);
  154. }
  155. }
  156. /**
  157. * Gets the minimum slider value
  158. *
  159. * @return the smallest value the slider can have
  160. */
  161. public double getMin() {
  162. return getState(false).minValue;
  163. }
  164. /**
  165. * Set the minimum slider value. If the current value of the slider is
  166. * smaller than this, the value is set to the new minimum.
  167. *
  168. * @param max
  169. * The new minimum slider value
  170. */
  171. public void setMin(double min) {
  172. getState().minValue = min;
  173. if (getValue() < min) {
  174. setValue(min);
  175. }
  176. }
  177. /**
  178. * Get the current orientation of the slider (horizontal or vertical).
  179. *
  180. * @return {@link SliderOrientation#HORIZONTAL} or
  181. * {@link SliderOrientation#VERTICAL}
  182. */
  183. public SliderOrientation getOrientation() {
  184. return getState(false).orientation;
  185. }
  186. /**
  187. * Set the orientation of the slider.
  188. *
  189. * @param orientation
  190. * The new orientation, either
  191. * {@link SliderOrientation#HORIZONTAL} or
  192. * {@link SliderOrientation#VERTICAL}
  193. */
  194. public void setOrientation(SliderOrientation orientation) {
  195. getState().orientation = orientation;
  196. }
  197. /**
  198. * Get the current resolution of the slider. The resolution is the number of
  199. * digits after the decimal point.
  200. *
  201. * @return resolution
  202. */
  203. public int getResolution() {
  204. return getState(false).resolution;
  205. }
  206. /**
  207. * Set a new resolution for the slider. The resolution is the number of
  208. * digits after the decimal point.
  209. *
  210. * @throws IllegalArgumentException
  211. * if resolution is negative.
  212. *
  213. * @param resolution
  214. */
  215. public void setResolution(int resolution) {
  216. if (resolution < 0) {
  217. throw new IllegalArgumentException(
  218. "Cannot set a negative resolution to Slider");
  219. }
  220. getState().resolution = resolution;
  221. }
  222. /**
  223. * Sets the value of the slider.
  224. *
  225. * @param value
  226. * The new value of the slider.
  227. * @param repaintIsNotNeeded
  228. * If true, client-side is not requested to repaint itself.
  229. * @throws ValueOutOfBoundsException
  230. * If the given value is not inside the range of the slider.
  231. * @see #setMin(double) {@link #setMax(double)}
  232. */
  233. @Override
  234. protected void setValue(Double value, boolean repaintIsNotNeeded) {
  235. final double v = value.doubleValue();
  236. final int resolution = getResolution();
  237. double newValue;
  238. if (resolution > 0) {
  239. // Round up to resolution
  240. newValue = (int) (v * Math.pow(10, resolution));
  241. newValue = newValue / Math.pow(10, resolution);
  242. if (getMin() > newValue || getMax() < newValue) {
  243. throw new ValueOutOfBoundsException(value);
  244. }
  245. } else {
  246. newValue = (int) v;
  247. if (getMin() > newValue || getMax() < newValue) {
  248. throw new ValueOutOfBoundsException(value);
  249. }
  250. }
  251. getState().value = newValue;
  252. super.setValue(newValue, repaintIsNotNeeded);
  253. }
  254. @Override
  255. public void setValue(Double newFieldValue) {
  256. super.setValue(newFieldValue);
  257. getState().value = newFieldValue;
  258. }
  259. /*
  260. * Overridden to keep the shared state in sync with the AbstractField
  261. * internal value. Should be removed once AbstractField is refactored to use
  262. * shared state.
  263. *
  264. * See tickets #10921 and #11064.
  265. */
  266. @Override
  267. protected void setInternalValue(Double newValue) {
  268. super.setInternalValue(newValue);
  269. if (newValue == null) {
  270. newValue = 0.0;
  271. }
  272. getState().value = newValue;
  273. }
  274. /**
  275. * Thrown when the value of the slider is about to be set to a value that is
  276. * outside the valid range of the slider.
  277. *
  278. * @author Vaadin Ltd.
  279. *
  280. */
  281. public class ValueOutOfBoundsException extends RuntimeException {
  282. private final Double value;
  283. /**
  284. * Constructs an <code>ValueOutOfBoundsException</code> with the
  285. * specified detail message.
  286. *
  287. * @param valueOutOfBounds
  288. */
  289. public ValueOutOfBoundsException(Double valueOutOfBounds) {
  290. value = valueOutOfBounds;
  291. }
  292. /**
  293. * Gets the value that is outside the valid range of the slider.
  294. *
  295. * @return the value that is out of bounds
  296. */
  297. public Double getValue() {
  298. return value;
  299. }
  300. }
  301. @Override
  302. public Class<Double> getType() {
  303. return Double.class;
  304. }
  305. }