<commit_before>/*
@VaadinApache2LicenseForJavaFiles@
 */
package com.vaadin.terminal.gwt.client.ui;

import java.util.Set;

import com.google.gwt.user.client.ui.Focusable;
import com.google.gwt.user.client.ui.HasEnabled;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.terminal.gwt.client.ApplicationConnection;
import com.vaadin.terminal.gwt.client.ComponentConnector;
import com.vaadin.terminal.gwt.client.ComponentContainerConnector;
import com.vaadin.terminal.gwt.client.ComponentState;
import com.vaadin.terminal.gwt.client.ConnectorMap;
import com.vaadin.terminal.gwt.client.LayoutManager;
import com.vaadin.terminal.gwt.client.TooltipInfo;
import com.vaadin.terminal.gwt.client.UIDL;
import com.vaadin.terminal.gwt.client.Util;
import com.vaadin.terminal.gwt.client.VConsole;
import com.vaadin.terminal.gwt.client.communication.SharedState;
import com.vaadin.terminal.gwt.client.communication.StateChangeEvent;
import com.vaadin.terminal.gwt.client.ui.root.RootConnector;

public abstract class AbstractComponentConnector extends AbstractConnector
        implements ComponentConnector {

    private ComponentContainerConnector parent;

    private Widget widget;

    // shared state from the server to the client
    private ComponentState state;

    private String lastKnownWidth = "";
    private String lastKnownHeight = "";

    /**
     * Default constructor
     */
    public AbstractComponentConnector() {
    }

    /**
     * Creates and returns the widget for this VPaintableWidget. This method
     * should only be called once when initializing the paintable.
     * 
     * @return
     */
    protected abstract Widget createWidget();

    /**
     * Returns the widget associated with this paintable. The widget returned by
     * this method must not changed during the life time of the paintable.
     * 
     * @return The widget associated with this paintable
     */
    public Widget getWidget() {
        if (widget == null) {
            widget = createWidget();
        }

        return widget;
    }

    /**
     * Returns the shared state object for this connector.
     * 
     * If overriding this method to return a more specific type, also
     * {@link #createState()} must be overridden.
     * 
     * @return current shared state (not null)
     */
    public ComponentState getState() {
        return state;
    }

    @Deprecated
    public static boolean isRealUpdate(UIDL uidl) {
        return !uidl.hasAttribute("cached");
    }

    @Override
    public void onStateChanged(StateChangeEvent stateChangeEvent) {
        super.onStateChanged(stateChangeEvent);

        ConnectorMap paintableMap = ConnectorMap.get(getConnection());

        if (getState().getDebugId() != null) {
            getWidget().getElement().setId(getState().getDebugId());
        } else {
            getWidget().getElement().removeAttribute("id");

        }

        setWidgetEnabled(isEnabled());
        /*
         * Disabled state may affect (override) tabindex so the order must be
         * first setting tabindex, then enabled state.
         */
        if (state instanceof TabIndexState && getWidget() instanceof Focusable) {
            ((Focusable) getWidget()).setTabIndex(((TabIndexState) state)
                    .getTabIndex());
        }

        // Style names
        String styleName = getStyleNames(getWidget().getStylePrimaryName());
        getWidget().setStyleName(styleName);

        // Update tooltip
        TooltipInfo tooltipInfo = paintableMap.getTooltipInfo(this, null);
        if (getState().hasDescription()) {
            tooltipInfo.setTitle(getState().getDescription());
        } else {
            tooltipInfo.setTitle(null);
        }
        // add error info to tooltip if present
        tooltipInfo.setErrorMessage(getState().getErrorMessage());

        // Set captions
        if (delegateCaptionHandling()) {
            ComponentContainerConnector parent = getParent();
            if (parent != null) {
                parent.updateCaption(this);
            } else if (!(this instanceof RootConnector)) {
                VConsole.error("Parent of connector "
                        + Util.getConnectorString(this)
                        + " is null. This is typically an indication of a broken component hierarchy");
            }
        }

        /*
         * updateComponentSize need to be after caption update so caption can be
         * taken into account
         */

        updateComponentSize();
    }

    public void setWidgetEnabled(boolean widgetEnabled) {
        if (getWidget() instanceof HasEnabled) {
            HasEnabled hasEnabled = (HasEnabled) getWidget();
            if (hasEnabled.isEnabled() != widgetEnabled) {
                hasEnabled.setEnabled(widgetEnabled);
            }
        }
    }

    private void updateComponentSize() {
        String newWidth = getState().getWidth();<commit_msg>Additional tweaks based on review (#8507)<commit_after>/*
@VaadinApache2LicenseForJavaFiles@
 */
package com.vaadin.terminal.gwt.client.ui;

import java.util.Set;

import com.google.gwt.user.client.ui.Focusable;
import com.google.gwt.user.client.ui.HasEnabled;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.terminal.gwt.client.ApplicationConnection;
import com.vaadin.terminal.gwt.client.ComponentConnector;
import com.vaadin.terminal.gwt.client.ComponentContainerConnector;
import com.vaadin.terminal.gwt.client.ComponentState;
import com.vaadin.terminal.gwt.client.ConnectorMap;
import com.vaadin.terminal.gwt.client.LayoutManager;
import com.vaadin.terminal.gwt.client.TooltipInfo;
import com.vaadin.terminal.gwt.client.UIDL;
import com.vaadin.terminal.gwt.client.Util;
import com.vaadin.terminal.gwt.client.VConsole;
import com.vaadin.terminal.gwt.client.communication.SharedState;
import com.vaadin.terminal.gwt.client.communication.StateChangeEvent;
import com.vaadin.terminal.gwt.client.ui.root.RootConnector;

public abstract class AbstractComponentConnector extends AbstractConnector
        implements ComponentConnector {

    private ComponentContainerConnector parent;

    private Widget widget;

    // shared state from the server to the client
    private ComponentState state;

    private String lastKnownWidth = "";
    private String lastKnownHeight = "";

    /**
     * Default constructor
     */
    public AbstractComponentConnector() {
    }

    /**
     * Creates and returns the widget for this VPaintableWidget. This method
     * should only be called once when initializing the paintable.
     * 
     * @return
     */
    protected abstract Widget createWidget();

    /**
     * Returns the widget associated with this paintable. The widget returned by
     * this method must not changed during the life time of the paintable.
     * 
     * @return The widget associated with this paintable
     */
    public Widget getWidget() {
        if (widget == null) {
            widget = createWidget();
        }

        return widget;
    }

    /**
     * Returns the shared state object for this connector.
     * 
     * If overriding this method to return a more specific type, also
     * {@link #createState()} must be overridden.
     * 
     * @return current shared state (not null)
     */
    public ComponentState getState() {
        return state;
    }

    @Deprecated
    public static boolean isRealUpdate(UIDL uidl) {
        return !uidl.hasAttribute("cached");
    }

    @Override
    public void onStateChanged(StateChangeEvent stateChangeEvent) {
        super.onStateChanged(stateChangeEvent);

        ConnectorMap paintableMap = ConnectorMap.get(getConnection());

        if (getState().getDebugId() != null) {
            getWidget().getElement().setId(getState().getDebugId());
        } else {
            getWidget().getElement().removeAttribute("id");

        }

        /*
         * Disabled state may affect (override) tabindex so the order must be
         * first setting tabindex, then enabled state.
         */
        if (state instanceof TabIndexState && getWidget() instanceof Focusable) {
            ((Focusable) getWidget()).setTabIndex(((TabIndexState) state)
                    .getTabIndex());
        }

        setWidgetEnabled(isEnabled());

        // Style names
        String styleName = getStyleNames(getWidget().getStylePrimaryName());
        getWidget().setStyleName(styleName);

        // Update tooltip
        TooltipInfo tooltipInfo = paintableMap.getTooltipInfo(this, null);
        if (getState().hasDescription()) {
            tooltipInfo.setTitle(getState().getDescription());
        } else {
            tooltipInfo.setTitle(null);
        }
        // add error info to tooltip if present
        tooltipInfo.setErrorMessage(getState().getErrorMessage());

        // Set captions
        if (delegateCaptionHandling()) {
            ComponentContainerConnector parent = getParent();
            if (parent != null) {
                parent.updateCaption(this);
            } else if (!(this instanceof RootConnector)) {
                VConsole.error("Parent of connector "
                        + Util.getConnectorString(this)
                        + " is null. This is typically an indication of a broken component hierarchy");
            }
        }

        /*
         * updateComponentSize need to be after caption update so caption can be
         * taken into account
         */

        updateComponentSize();
    }

    public void setWidgetEnabled(boolean widgetEnabled) {
        if (getWidget() instanceof HasEnabled) {
            ((HasEnabled) getWidget()).setEnabled(widgetEnabled);
        }
    }

    private void updateComponentSize() {
        String newWidth = getState().getWidth();
{'fraction_non_alphanumeric': 0.09598853868194843, 'fraction_numerical': 0.0006139991813344249, 'mean_word_length': 2.5956585724797647, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5650100', 'n_tokens_mistral': 2618, 'n_tokens_neox': 2502, 'n_words': 634}