001package org.gwtbootstrap3.client.ui;
002
003/*
004 * #%L
005 * GwtBootstrap3
006 * %%
007 * Copyright (C) 2013 - 2015 GwtBootstrap3
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 org.gwtbootstrap3.client.ui.base.AbstractTooltip;
024
025import com.google.gwt.core.client.JavaScriptObject;
026import com.google.gwt.dom.client.Element;
027import com.google.gwt.user.client.ui.Widget;
028
029/**
030 * Basic implementation for the Bootstrap tooltip
031 * <p/>
032 * <a href="http://getbootstrap.com/javascript/#tooltips">Bootstrap Documentation</a>
033 * <p/>
034 * <p/>
035 * <h3>UiBinder example</h3>
036 * <p/>
037 * 
038 * <pre>
039 * {@code
040 * <b:Tooltip text="...">
041 *    ...
042 * </b:Tooltip>
043 * }
044 * </pre>
045 * <p/>
046 *
047 * @author Steven Jardine
048 */
049public class Tooltip extends AbstractTooltip {
050
051    /**
052     * Creates the empty Popover
053     */
054    public Tooltip() {
055        super("bs.tooltip");
056    }
057
058    /**
059     * Creates the tooltip with given title. Remember to set the content and widget as well.
060     *
061     * @param title title for the tooltip
062     */
063    public Tooltip(final String title) {
064        this();
065        setTitle(title);
066    }
067
068    /**
069     * Creates the tooltip around this widget
070     *
071     * @param w widget for the tooltip
072     */
073    public Tooltip(final Widget w) {
074        this();
075        setWidget(w);
076    }
077
078    /**
079     * Creates the tooltip around this widget with given title and content.
080     *
081     * @param w widget for the tooltip
082     * @param title title for the tooltip
083     */
084    public Tooltip(final Widget w, final String title) {
085        this();
086        setWidget(w);
087        setTitle(title);
088    }
089
090    /**
091     * Call the native tooltip method with the given argument.
092     *
093     * @param e the {@link Element}.
094     * @param arg the arg
095     */
096    private native void call(final Element e, final String arg) /*-{
097        $wnd.jQuery(e).tooltip(arg);
098    }-*/;
099
100    /** {@inheritDoc} */
101    @Override
102    protected void call(String arg) {
103        call(getWidget().getElement(), arg);
104    }
105
106    /** {@inheritDoc} */
107    @Override
108    public void init() {
109        Element element = getWidget().getElement();
110        JavaScriptObject baseOptions = createOptions(element, isAnimated(), isHtml(), getSelector(),
111                getTrigger().getCssName(), getShowDelayMs(), getHideDelayMs(), getContainer(), prepareTemplate(), 
112                getViewportSelector(), getViewportPadding());
113        tooltip(element, baseOptions);
114        bindJavaScriptEvents(element);
115        setInitialized(true);
116    }
117
118    /**
119     * Create the tooltip.
120     */
121    private native void tooltip(Element e, JavaScriptObject options) /*-{
122        $wnd.jQuery(e).tooltip(options);
123    }-*/;
124
125    /** {@inheritDoc} */
126    @Override
127    protected void updateTitleWhenShowing() {
128        updateTitleWhenShowing(getWidget().getElement());
129    }
130
131    /**
132     * Update the title. This should only be called when the title is already showing. It causes a small flicker but
133     * updates the title immediately.
134     *
135     * @param e the tooltip {@link Element}.
136     */
137    private native void updateTitleWhenShowing(Element e) /*-{
138        $wnd.jQuery(e).tooltip('fixTitle').tooltip('show');
139    }-*/;
140
141}