001/*
002 *  Copyright 2012 GWT-Bootstrap
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package com.github.gwtbootstrap.client.ui;
017
018import com.github.gwtbootstrap.client.ui.base.HoverBase;
019import com.github.gwtbootstrap.client.ui.constants.Placement;
020import com.github.gwtbootstrap.client.ui.constants.Trigger;
021import com.github.gwtbootstrap.client.ui.constants.VisibilityChange;
022import com.google.gwt.dom.client.Element;
023import com.google.gwt.user.client.ui.Widget;
024
025//@formatter:off
026/**
027 * Link with a small tooltip with additional information.
028 * <p>
029 * Technically it's an html {@code <a>} tag. The text of the popup will be the
030 * content of the anchor's <code>title</code> attribute. 
031 * 
032 * @since 2.0.4.0
033 * 
034 * @author Dominik Mayer
035 * 
036 * @see <a href="http://twitter.github.com/bootstrap/javascript.html#tooltips">Bootstrap documentation</a>
037 * @see Popover
038 */
039//@formatter:on
040public class Tooltip extends HoverBase {
041
042        private String tooltip;
043
044        /**
045         * Creates an empty link without text and tooltip text.
046         */
047        public Tooltip() {
048                super();
049        }
050
051    /**
052         * Creates a link with the
053         * 
054         * @param tooltip get
055         */
056        public Tooltip(String tooltip) {
057                this();
058                setText(tooltip);
059        }
060
061        /**
062         * Sets the text that should appear in the tooltip.
063         * 
064         * @param tooltop
065         *            the text
066         */
067        public void setText(String tooltop) {
068                this.tooltip = tooltop;
069        }
070        
071        public String getText() {
072                return this.tooltip;
073        }
074
075        /**
076         * {@inheritDoc}
077         */
078        @Override
079        public void reconfigure() {
080                
081                removeDataIfExists();
082                
083                setDataAttribute(getWidget().getElement(), "original-title", tooltip);
084                
085                configure(getWidget().getElement(), getAnimation(), getPlacement().get(),
086                                getTrigger().get(), getShowDelay(), getHideDelay(), getContainer());
087        }
088
089        /**
090         * {@inheritDoc}
091         */
092        @Override
093        protected void changeVisibility(VisibilityChange visibilityChange) {
094                changeVisibility(getWidget().getElement(), visibilityChange.get());
095        }
096        
097        /**
098         * 
099         * @param e
100         * @param animated
101         * @param placement
102         * @param trigger
103         * @param showDelay
104         * @param hideDelay
105         */
106        public static void configure(Widget e,boolean animated,Placement placement,Trigger trigger,int showDelay,int hideDelay, String container) {
107                configure(e.getElement(), animated, placement.get(),trigger.get(), showDelay, hideDelay, container);
108        }
109
110        //@formatter:off
111        public static native void configure(String selector,String text, boolean animated,
112            String placement, String trigger, int showDelay, int hideDelay, String container) /*-{
113        $wnd.jQuery(selector).tooltip({
114            title : text,
115            animation : animated,
116            placement : placement,
117            trigger : trigger,
118            delay : {
119                show : showDelay,
120                hide : hideDelay
121            },
122            container: container
123        });
124    }-*/;
125        
126        public static native void configure(Element element, boolean animated,
127                        String placement, String trigger, int showDelay, int hideDelay, String container) /*-{
128                $wnd.jQuery(element).tooltip({
129                        animation : animated,
130                        placement : placement,
131                        trigger : trigger,
132                        delay : {
133                                show : showDelay,
134                                hide : hideDelay
135                        },
136            container: container
137                });
138        }-*/;
139
140        public static native void changeVisibility(Element e, String visibility) /*-{
141                $wnd.jQuery(e).tooltip(visibility);
142        }-*/;   
143        //@formatter:on
144
145        /**
146         * {@inheritDoc}
147         */
148        @Override
149        protected String getDataName() {
150                return "tooltip";
151        }
152
153    @Override
154    protected void removeDataIfExists(Element e, String dataName) {
155        doRemoveDataIfExists(e, dataName);
156    }
157
158    private native void doRemoveDataIfExists(Element e, String dataName) /*-{
159        var element = $wnd.jQuery(e);
160        if(element.data(dataName)) {
161            element.tooltip('destroy');
162        }
163    }-*/;
164}