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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package com.itmill.toolkit.ui;
  2. import java.util.Map;
  3. import com.itmill.toolkit.terminal.PaintException;
  4. import com.itmill.toolkit.terminal.PaintTarget;
  5. public class Slider extends AbstractField {
  6. public static final int ORIENTATION_HORIZONTAL = 0;
  7. public static final int ORIENTATION_VERTICAL = 1;
  8. /** Minimum value of slider */
  9. private double min = 0;
  10. /** Maximum value of slider */
  11. private double max = 100;
  12. /**
  13. * Resolution, how many digits are considered relevant after desimal point.
  14. * Must be a non-negative value
  15. */
  16. private int resolution = 0;
  17. /**
  18. * Slider orientation (horizontal==default/vertical).
  19. */
  20. private int orientation = ORIENTATION_HORIZONTAL;
  21. /**
  22. * Slider size in pixels.
  23. * In horizontal mode if set to -1, allow 100% with container.
  24. * In vertical mode if set to -1, default height 120 pixels.
  25. */
  26. private int size = -1;
  27. /**
  28. * Handle size in percents related to base size.
  29. * Must be a value between 1-100.
  30. */
  31. private int handleSize = -1;
  32. /**
  33. * Show arrows that can be pressed to slide the
  34. * handle in some increments (client-side
  35. * implementation decides the increment).
  36. */
  37. private boolean arrows = true;
  38. public Slider() {
  39. super();
  40. super.setValue(new Double(min));
  41. }
  42. public Slider(String caption) {
  43. this();
  44. setCaption(caption);
  45. }
  46. public Slider(double min, double max, int resolution) {
  47. this();
  48. setMin(min);
  49. setMax(max);
  50. setResolution(resolution);
  51. }
  52. public Slider(int min, int max) {
  53. this();
  54. setMin(min);
  55. setMax(max);
  56. setResolution(0);
  57. }
  58. public Slider(String caption, int min, int max) {
  59. this(min, max);
  60. setCaption(caption);
  61. }
  62. public double getMax() {
  63. return max;
  64. }
  65. /**
  66. * Set the maximum value of the Slider. As a side-effect nullifies the "values" Set.
  67. * @param max
  68. */
  69. public void setMax(double max) {
  70. this.max = max;
  71. try {
  72. if((new Float(getValue().toString())).floatValue() > max)
  73. super.setValue(new Float(min));
  74. } catch(ClassCastException e) {
  75. super.setValue(new Float(max));
  76. }
  77. if(handleSize == -1)
  78. handleSize = (int) ((max-min)/max*10 + (max-min)/10);
  79. requestRepaint();
  80. }
  81. public double getMin() {
  82. return min;
  83. }
  84. /**
  85. * Set the minimum value of the Slider. As a side-effect nullifies the "values" Set.
  86. * @param max
  87. */
  88. public void setMin(double min) {
  89. this.min = min;
  90. try {
  91. if((new Double(getValue().toString())).doubleValue() < min)
  92. super.setValue(new Double(min));
  93. } catch(ClassCastException e) {
  94. super.setValue(new Double(min));
  95. }
  96. if(handleSize == -1)
  97. handleSize = (int) ((max-min)/max*10 + (max-min)/10);
  98. requestRepaint();
  99. }
  100. public int getOrientation() {
  101. return orientation;
  102. }
  103. public void setOrientation(int orientation) {
  104. this.orientation = orientation;
  105. requestRepaint();
  106. }
  107. public int getResolution() {
  108. return resolution;
  109. }
  110. public void setResolution(int resolution) {
  111. if(resolution < 0)
  112. return;
  113. this.resolution = resolution;
  114. requestRepaint();
  115. }
  116. public void setValue(Double value, boolean repaintIsNotNeeded) throws ValueOutOfBoundsException {
  117. double v = new Double(value.toString()).doubleValue();
  118. double newValue;
  119. if(resolution>0) {
  120. // Round up to resolution
  121. newValue = (int) (v * (double) Math.pow(10, resolution));
  122. newValue = newValue / (double) Math.pow(10, resolution);
  123. if(min > newValue || max < newValue)
  124. throw new ValueOutOfBoundsException(value);
  125. } else {
  126. newValue = (int) v;
  127. if(min > newValue || max < newValue)
  128. throw new ValueOutOfBoundsException(value);
  129. }
  130. super.setValue(new Double(newValue), repaintIsNotNeeded);
  131. }
  132. public void setValue(Double value) throws ValueOutOfBoundsException {
  133. setValue(value, false);
  134. }
  135. public int getSize() {
  136. return size;
  137. }
  138. public void setSize(int size) {
  139. this.size = size;
  140. requestRepaint();
  141. }
  142. public int getHandleSize() {
  143. return handleSize;
  144. }
  145. public void setHandleSize(int handleSize) {
  146. if(handleSize > 100 || handleSize < 1)
  147. return;
  148. this.handleSize = handleSize;
  149. requestRepaint();
  150. }
  151. public void setArrows(boolean visible) {
  152. arrows = visible;
  153. requestRepaint();
  154. }
  155. public boolean arrowsVisible() {
  156. return arrows;
  157. }
  158. public String getTag() {
  159. return "slider";
  160. }
  161. public void paintContent(PaintTarget target) throws PaintException {
  162. super.paintContent(target);
  163. target.addAttribute("min", min);
  164. target.addAttribute("max", max);
  165. target.addAttribute("resolution", resolution);
  166. if(resolution > 0)
  167. target.addVariable(this, "value", ((Double)getValue()).doubleValue());
  168. else
  169. target.addVariable(this, "value", ((Double)getValue()).intValue());
  170. if(orientation == ORIENTATION_VERTICAL)
  171. target.addAttribute("vertical", true);
  172. if(arrows)
  173. target.addAttribute("arrows", true);
  174. if(size > -1)
  175. target.addAttribute("size", size);
  176. target.addAttribute("hsize", handleSize);
  177. }
  178. /**
  179. * Invoked when the value of a variable has changed. Slider listeners are
  180. * notified if the slider value has changed.
  181. *
  182. * @param source
  183. * @param variables
  184. */
  185. public void changeVariables(Object source, Map variables) {
  186. if (variables.containsKey("value")) {
  187. Object newValue = variables.get("value");
  188. if(resolution > 0)
  189. newValue = new Double(newValue.toString());
  190. else
  191. newValue = new Integer(newValue.toString());
  192. if(newValue != null && newValue != getValue() && !newValue.equals(getValue())) {
  193. setValue(newValue, true);
  194. }
  195. }
  196. }
  197. public class ValueOutOfBoundsException extends Exception {
  198. /**
  199. * Serial generated by Eclipse.
  200. */
  201. private static final long serialVersionUID = -6451298598644446340L;
  202. private Double value;
  203. /**
  204. * Constructs an <code>ValueOutOfBoundsException</code> with the specified
  205. * detail message.
  206. *
  207. * @param valueOutOfBounds
  208. */
  209. public ValueOutOfBoundsException(Double valueOutOfBounds) {
  210. this.value = valueOutOfBounds;
  211. }
  212. public Double getValue() {
  213. return this.value;
  214. }
  215. }
  216. public Class getType() {
  217. return Double.class;
  218. }
  219. }