/*
 * Copyright (c) 2013 Haulmont Technology Ltd. All Rights Reserved.
 * Haulmont Technology proprietary and confidential.
 * Use is subject to license terms.
 */

package com.haulmont.cuba.web.toolkit.ui.client.caption;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.EventListener;
import com.haulmont.cuba.web.toolkit.ui.client.fieldgrouplayout.CubaFieldGroupLayoutComponentSlot;
import com.vaadin.client.*;

/**
 * @author artamonov
 * @version $Id$
 */
public class CubaCaptionWidget extends VCaption {
    public static final String CLASSNAME = "cuba-caption";

    protected Element toolTipIndicator;

    protected CubaFieldGroupLayoutComponentSlot fieldGroupSlot = null;

    public CubaCaptionWidget(ComponentConnector component, ApplicationConnection client) {
        super(component, client);

        initWidget();
    }

    public void initWidget() {
        addStyleName(CLASSNAME);
    }

    @Override
    public boolean updateCaption() {
        boolean updateResult = super.updateCaption();

        addStyleName(CLASSNAME);

        if (fieldGroupSlot != null)
            fieldGroupSlot.captionUpdated(this);

        return updateResult;
    }

    @Override
    protected void enableFieldDescription() {
        if (toolTipIndicator == null) {
            toolTipIndicator = DOM.createDiv();
            toolTipIndicator.setClassName("cuba-tooltip-button");

            DOM.insertChild(getElement(), toolTipIndicator, getDescriptionInsertPosition());
            DOM.sinkEvents(toolTipIndicator, Event.ONCLICK);
            DOM.setEventListener(toolTipIndicator, new EventListener() {
                @Override
                public void onBrowserEvent(Event event) {
                    // vaadin7 tooltips
                }
            });
        }
    }

    @Override
    protected void disableFieldDescription() {
        if (toolTipIndicator != null) {
            toolTipIndicator.removeFromParent();
            toolTipIndicator = null;
        }
    }

    @Override
    protected void enableFieldRequired() {
        DOM.sinkEvents(requiredFieldIndicator, Event.ONCLICK);
        DOM.setEventListener(requiredFieldIndicator, new EventListener() {
            @Override
            public void onBrowserEvent(Event event) {
                // vaadin7 tooltips
            }
        });
    }

    @Override
    public int getRenderedWidth() {
        int renderedWidth = super.getRenderedWidth();
        if (toolTipIndicator != null) {
            renderedWidth += Util.getRequiredWidth(toolTipIndicator);
        }
        return renderedWidth;
    }

    @Override
    public Element getTextElement() {
        return super.getTextElement();
    }

    public Element getTooltipElement() {
        return toolTipIndicator;
    }

    public Element getRequiredIndicatorElement() {
        return requiredFieldIndicator;
    }

    @Override
    protected int getInsertPosition(InsertPosition element) {
        int pos = super.getInsertPosition(element);

        if (toolTipIndicator != null) {
            pos++;
        }

        return pos;
    }

    protected int getDescriptionInsertPosition() {
        return super.getInsertPosition(null);
    }

    public CubaFieldGroupLayoutComponentSlot getFieldGroupSlot() {
        return fieldGroupSlot;
    }

    public void setFieldGroupSlot(CubaFieldGroupLayoutComponentSlot fieldGroupSlot) {
        this.fieldGroupSlot = fieldGroupSlot;
    }
}
@@ -302,98 +302,8 @@
 nt;%0A
-import com.google.gwt.user.client.Event;%0Aimport com.google.gwt.user.client.EventListener;%0A
 impo
@@ -422,17 +422,148 @@
 .client.
-*
+ApplicationConnection;%0Aimport com.vaadin.client.ComponentConnector;%0Aimport com.vaadin.client.Util;%0Aimport com.vaadin.client.VCaption
 ;%0A%0A/**%0A 
@@ -1638,287 +1638,27 @@
     
-DOM.sinkEvents(toolTipIndicator, Event.ONCLICK);%0A            DOM.setEventListener(toolTipIndicator, new EventListener() %7B%0A                @Override%0A                public void onBrowserEvent(Event event) %7B%0A                    // vaadin7 tooltips%0A                %7D%0A            %7D);
+// vaadin7 tooltips
 %0A   
@@ -1936,230 +1936,8 @@
 ) %7B%0A
-        DOM.sinkEvents(requiredFieldIndicator, Event.ONCLICK);%0A        DOM.setEventListener(requiredFieldIndicator, new EventListener() %7B%0A            @Override%0A            public void onBrowserEvent(Event event) %7B%0A        
     
@@ -1964,34 +1964,8 @@
 ips%0A
-            %7D%0A        %7D);%0A
     

Fix tooltips enabling for CubaCaptionWidget #PL-1809