2 * Copyright 2000-2018 Vaadin Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
16 package com.vaadin.v7.client.ui.colorpicker;
18 import com.google.gwt.event.dom.client.ClickHandler;
19 import com.google.gwt.event.dom.client.HasClickHandlers;
20 import com.vaadin.client.communication.StateChangeEvent;
21 import com.vaadin.v7.client.ui.AbstractLegacyComponentConnector;
22 import com.vaadin.v7.shared.ui.colorpicker.ColorPickerState;
25 * An abstract class that defines default implementation for a color picker
31 public abstract class AbstractColorPickerConnector
32 extends AbstractLegacyComponentConnector implements ClickHandler {
34 private static final String DEFAULT_WIDTH_STYLE = "v-default-caption-width";
37 public ColorPickerState getState() {
38 return (ColorPickerState) super.getState();
42 public boolean delegateCaptionHandling() {
47 public void onStateChanged(StateChangeEvent stateChangeEvent) {
48 // NOTE: this method is called after @DelegateToWidget
49 super.onStateChanged(stateChangeEvent);
50 if (stateChangeEvent.hasPropertyChanged("color")) {
53 if (getState().showDefaultCaption && (getState().caption == null
54 || "".equals(getState().caption))) {
56 setCaption(getState().color);
59 if (stateChangeEvent.hasPropertyChanged("caption")
60 || stateChangeEvent.hasPropertyChanged("htmlContentAllowed")
61 || stateChangeEvent.hasPropertyChanged("showDefaultCaption")) {
63 setCaption(getCaption());
64 refreshDefaultCaptionStyle();
71 if (getWidget() instanceof HasClickHandlers) {
72 ((HasClickHandlers) getWidget()).addClickHandler(this);
77 * Get caption for the color picker widget.
81 protected String getCaption() {
82 if (getState().showDefaultCaption && (getState().caption == null
83 || "".equals(getState().caption))) {
84 return getState().color;
86 return getState().caption;
90 * Add/remove default caption style.
92 protected void refreshDefaultCaptionStyle() {
93 if (getState().showDefaultCaption
94 && (getState().caption == null || getState().caption.isEmpty())
95 && getState().width.isEmpty()) {
96 getWidget().addStyleName(DEFAULT_WIDTH_STYLE);
98 getWidget().removeStyleName(DEFAULT_WIDTH_STYLE);
103 * Set caption of the color picker widget.
107 protected abstract void setCaption(String caption);
110 * Update the widget to show the currently selected color.
112 protected abstract void refreshColor();