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.

AbstractColorPickerConnector.java 3.4KB

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