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.

ColorPicker.java 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.demo.colorpicker;
  5. import java.util.Map;
  6. import com.itmill.toolkit.terminal.PaintException;
  7. import com.itmill.toolkit.terminal.PaintTarget;
  8. import com.itmill.toolkit.ui.AbstractField;
  9. public class ColorPicker extends AbstractField {
  10. public ColorPicker() {
  11. super();
  12. setValue(new String("white"));
  13. }
  14. /** The property value of the field is an Integer. */
  15. public Class getType() {
  16. return String.class;
  17. }
  18. /** Tag is the UIDL element name for client-server communications. */
  19. public String getTag() {
  20. return "colorpicker";
  21. }
  22. /** Set the currently selected color. */
  23. public void setColor(String newcolor) {
  24. // Sets the color name as the property of the component.
  25. // Setting the property will automatically cause repainting of
  26. // the component with paintContent().
  27. setValue(newcolor);
  28. }
  29. /** Retrieve the currently selected color. */
  30. public String getColor() {
  31. return (String) getValue();
  32. }
  33. /** Paint (serialize) the component for the client. */
  34. public void paintContent(PaintTarget target) throws PaintException {
  35. // Superclass writes any common attributes in the paint target.
  36. super.paintContent(target);
  37. // Add the currently selected color as a variable in the paint
  38. // target.
  39. target.addVariable(this, "colorname", getColor());
  40. }
  41. /** Deserialize changes received from client. */
  42. public void changeVariables(Object source, Map variables) {
  43. // Sets the currently selected color
  44. if (variables.containsKey("colorname") && !isReadOnly()) {
  45. final String newValue = (String) variables.get("colorname");
  46. // Changing the property of the component will
  47. // trigger a ValueChangeEvent
  48. setValue(newValue, true);
  49. }
  50. }
  51. }