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

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