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