Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

AbstractColorPickerConnector.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright 2000-2021 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.client.ui.colorpicker;
  17. import com.google.gwt.event.dom.client.ClickHandler;
  18. import com.google.gwt.event.dom.client.HasClickHandlers;
  19. import com.vaadin.client.communication.StateChangeEvent;
  20. import com.vaadin.client.ui.AbstractComponentConnector;
  21. import com.vaadin.shared.ui.colorpicker.AbstractColorPickerState;
  22. /**
  23. * An abstract class that defines default implementation for a color picker
  24. * connector.
  25. *
  26. * @since 7.0.0
  27. */
  28. public abstract class AbstractColorPickerConnector
  29. extends AbstractComponentConnector implements ClickHandler {
  30. private static final String DEFAULT_WIDTH_STYLE = "v-default-caption-width";
  31. @Override
  32. public AbstractColorPickerState getState() {
  33. return (AbstractColorPickerState) super.getState();
  34. }
  35. @Override
  36. public boolean delegateCaptionHandling() {
  37. return false;
  38. }
  39. @Override
  40. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  41. // NOTE: this method is called after @DelegateToWidget
  42. super.onStateChanged(stateChangeEvent);
  43. if (stateChangeEvent.hasPropertyChanged("color")) {
  44. refreshColor();
  45. if (getState().showDefaultCaption && (getState().caption == null
  46. || getState().caption.isEmpty())) {
  47. setCaption(getState().color);
  48. }
  49. }
  50. if (stateChangeEvent.hasPropertyChanged("caption")
  51. || stateChangeEvent.hasPropertyChanged("htmlContentAllowed")
  52. || stateChangeEvent.hasPropertyChanged("showDefaultCaption")) {
  53. setCaption(getCaption());
  54. refreshDefaultCaptionStyle();
  55. }
  56. }
  57. @Override
  58. public void init() {
  59. super.init();
  60. if (getWidget() instanceof HasClickHandlers) {
  61. ((HasClickHandlers) getWidget()).addClickHandler(this);
  62. }
  63. }
  64. /**
  65. * Get caption for the color picker widget.
  66. *
  67. * @return the caption
  68. */
  69. protected String getCaption() {
  70. if (getState().showDefaultCaption && (getState().caption == null
  71. || getState().caption.isEmpty())) {
  72. return getState().color;
  73. }
  74. return getState().caption;
  75. }
  76. /**
  77. * Add/remove default caption style.
  78. */
  79. protected void refreshDefaultCaptionStyle() {
  80. if (getState().showDefaultCaption
  81. && (getState().caption == null || getState().caption.isEmpty())
  82. && getState().width.isEmpty()) {
  83. getWidget().addStyleName(DEFAULT_WIDTH_STYLE);
  84. } else {
  85. getWidget().removeStyleName(DEFAULT_WIDTH_STYLE);
  86. }
  87. }
  88. /**
  89. * Set caption of the color picker widget.
  90. *
  91. * @param caption
  92. * the caption to set
  93. */
  94. protected abstract void setCaption(String caption);
  95. /**
  96. * Update the widget to show the currently selected color.
  97. */
  98. protected abstract void refreshColor();
  99. }