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.

ColorPickerPreview.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.ui.components.colorpicker;
  17. import java.util.Objects;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20. import com.vaadin.data.HasValue;
  21. import com.vaadin.server.AbstractErrorMessage.ContentMode;
  22. import com.vaadin.server.ErrorMessage;
  23. import com.vaadin.server.UserError;
  24. import com.vaadin.shared.Registration;
  25. import com.vaadin.shared.ui.ErrorLevel;
  26. import com.vaadin.shared.ui.colorpicker.Color;
  27. import com.vaadin.ui.Component;
  28. import com.vaadin.ui.CssLayout;
  29. import com.vaadin.ui.HasComponents;
  30. import com.vaadin.ui.TextField;
  31. /**
  32. * A component that represents color selection preview within a color picker.
  33. *
  34. * @since 7.0.0
  35. */
  36. public class ColorPickerPreview extends CssLayout implements HasValue<Color> {
  37. private static final Logger LOGGER = Logger
  38. .getLogger(ColorPickerPreview.class.getName());
  39. private static final String STYLE_DARK_COLOR = "v-textfield-dark";
  40. private static final String STYLE_LIGHT_COLOR = "v-textfield-light";
  41. /** The color. */
  42. private Color color;
  43. /** The field. */
  44. private final TextField field;
  45. /** The old value. */
  46. private String oldValue;
  47. private Registration valueChangeListenerRegistration = null;
  48. private boolean readOnly;
  49. private ColorPickerPreview() {
  50. setStyleName("v-colorpicker-preview");
  51. field = new TextField();
  52. field.setSizeFull();
  53. field.setStyleName("v-colorpicker-preview-textfield");
  54. field.setData(this);
  55. valueChangeListenerRegistration = field
  56. .addValueChangeListener(this::valueChange);
  57. addComponent(field);
  58. }
  59. /**
  60. * Instantiates a new color picker preview.
  61. */
  62. public ColorPickerPreview(Color color) {
  63. this();
  64. setValue(color);
  65. }
  66. /**
  67. * Sets the value of this object. If the new value is not equal to
  68. * {@code getValue()}, fires a {@link ValueChangeEvent}. Throws
  69. * {@code NullPointerException} if the value is null.
  70. *
  71. * @param color
  72. * the new value, not {@code null}
  73. * @throws NullPointerException
  74. * if {@code color} is {@code null}
  75. */
  76. @Override
  77. public void setValue(Color color) {
  78. Objects.requireNonNull(color, "color cannot be null");
  79. this.color = color;
  80. // Unregister listener
  81. valueChangeListenerRegistration.remove();
  82. String colorCSS = color.getCSS();
  83. field.setValue(colorCSS);
  84. field.setComponentError(null);
  85. oldValue = colorCSS;
  86. // Re-register listener
  87. valueChangeListenerRegistration = field
  88. .addValueChangeListener(this::valueChange);
  89. // Set the text color
  90. field.removeStyleName(STYLE_DARK_COLOR);
  91. field.removeStyleName(STYLE_LIGHT_COLOR);
  92. if (this.color.getRed() + this.color.getGreen()
  93. + this.color.getBlue() < 3 * 128) {
  94. field.addStyleName(STYLE_DARK_COLOR);
  95. } else {
  96. field.addStyleName(STYLE_LIGHT_COLOR);
  97. }
  98. markAsDirty();
  99. }
  100. @Override
  101. public Color getValue() {
  102. return color;
  103. }
  104. @Override
  105. public Registration addValueChangeListener(
  106. ValueChangeListener<Color> listener) {
  107. Objects.requireNonNull(listener, "listener cannot be null");
  108. return addListener(ValueChangeEvent.class, listener,
  109. ValueChangeListener.VALUE_CHANGE_METHOD);
  110. }
  111. private void valueChange(ValueChangeEvent<String> event) {
  112. ErrorMessage errorMessage = null;
  113. String value = event.getValue();
  114. value = Objects.toString(value, "").trim();
  115. Color oldColor = color;
  116. try {
  117. /*
  118. * Description of supported formats see
  119. * http://www.w3schools.com/cssref/css_colors_legal.asp
  120. */
  121. color = ColorUtil.stringToColor(value);
  122. oldValue = value;
  123. fireEvent(new ValueChangeEvent<>(this, oldColor,
  124. event.isUserOriginated()));
  125. } catch (NumberFormatException e) {
  126. // Pattern matching ensures the validity of
  127. // the input, this should never happen
  128. LOGGER.log(Level.INFO, e.getMessage());
  129. errorMessage = new UserError(getUserErrorText(value),
  130. ContentMode.TEXT, ErrorLevel.WARNING);
  131. }
  132. field.setComponentError(errorMessage);
  133. }
  134. @Override
  135. protected String getCss(Component c) {
  136. return "background: " + color.getCSS();
  137. }
  138. @Override
  139. public void setRequiredIndicatorVisible(boolean visible) {
  140. field.setRequiredIndicatorVisible(visible);
  141. }
  142. @Override
  143. public boolean isRequiredIndicatorVisible() {
  144. return field.isRequiredIndicatorVisible();
  145. }
  146. @Override
  147. public void setReadOnly(boolean readOnly) {
  148. this.readOnly = readOnly;
  149. updateColorComponents();
  150. }
  151. @Override
  152. public boolean isReadOnly() {
  153. return readOnly;
  154. }
  155. private void updateColorComponents() {
  156. iterator().forEachRemaining(this::updateColorComponents);
  157. }
  158. private void updateColorComponents(Component component) {
  159. if (component instanceof HasValue<?>) {
  160. ((HasValue<?>) component).setReadOnly(isReadOnly());
  161. }
  162. if (component instanceof HasComponents) {
  163. for (Component c : (HasComponents) component) {
  164. updateColorComponents(c);
  165. }
  166. }
  167. }
  168. /**
  169. * Get the client error message text for color input parsing error.
  170. *
  171. * @param value
  172. * input which caused the error
  173. * @return error message text
  174. */
  175. protected String getUserErrorText(String value) {
  176. return value.isEmpty() ? "Input cannot be empty"
  177. : "Input '".concat(value)
  178. .concat("' is not in any recognized format");
  179. }
  180. }