001/*
002 * #%L
003 * GwtMaterial
004 * %%
005 * Copyright (C) 2015 - 2017 GwtMaterialDesign
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 * 
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 * 
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package gwt.material.design.client.base.mixin;
021
022import com.google.gwt.user.client.ui.Widget;
023import gwt.material.design.client.base.HasTooltip;
024import gwt.material.design.client.base.MaterialWidget;
025import gwt.material.design.client.constants.Position;
026import gwt.material.design.client.ui.MaterialTooltip;
027
028/**
029 * Mixin for the {@link MaterialTooltip} component.
030 *
031 * @author gilberto-torrezan
032 * @see HasTooltip
033 * @see MaterialWidget
034 */
035public class TooltipMixin<H extends Widget & HasTooltip> extends AbstractMixin<H> implements HasTooltip {
036
037    protected MaterialTooltip tooltip;
038
039    public TooltipMixin(H uiObject) {
040        super(uiObject);
041    }
042
043    @Override
044    public void setUiObject(H uiObject) {
045        super.setUiObject(uiObject);
046
047        if (tooltip != null) {
048            tooltip.setWidget(uiObject);
049        } else if (uiObject.isAttached()) {
050            initialize();
051        }
052    }
053
054    protected void initialize() {
055        if (tooltip == null) {
056            tooltip = new MaterialTooltip(uiObject);
057        }
058    }
059
060    @Override
061    public String getTooltip() {
062        return tooltip == null ? null : tooltip.getText();
063    }
064
065    @Override
066    public void setTooltip(String text) {
067        initialize();
068        tooltip.setText(text);
069    }
070
071    @Override
072    public Position getTooltipPosition() {
073        return tooltip == null ? null : tooltip.getPosition();
074    }
075
076    @Override
077    public void setTooltipPosition(Position position) {
078        initialize();
079        tooltip.setPosition(position);
080    }
081
082    @Override
083    public int getTooltipDelayMs() {
084        return tooltip == null ? 0 : tooltip.getDelayMs();
085    }
086
087    @Override
088    public void setTooltipDelayMs(int delayMs) {
089        initialize();
090        tooltip.setDelayMs(delayMs);
091    }
092
093    @Override
094    public void setTooltipHTML(String html) {
095        initialize();
096        tooltip.setHtml(html);
097    }
098
099    @Override
100    public String getTooltipHTML() {
101        return tooltip.getHtml();
102    }
103}