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.3KB

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