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

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