Search in sources :

Example 1 with ImageIcon

use of com.vaadin.client.ui.ImageIcon in project cuba by cuba-platform.

the class CubaCaptionWidget method updateCaption.

@Override
public boolean updateCaption() {
    ComponentConnector owner = getOwner();
    /* CAUTION copied from super class with small changes */
    boolean wasPlacedAfterComponent = placedAfterComponent;
    // Caption is placed after component unless there is some part which
    // moves it above.
    placedAfterComponent = captionPlacedAfterComponentByDefault;
    String style = CLASSNAME;
    if (ComponentStateUtil.hasStyles(owner.getState())) {
        for (String customStyle : owner.getState().styles) {
            style += " " + CLASSNAME + "-" + customStyle;
        }
    }
    if (!owner.isEnabled()) {
        style += " " + StyleConstants.DISABLED;
    }
    setStyleName(style);
    boolean hasIcon = owner.getState().resources.containsKey(ComponentConstants.ICON_RESOURCE);
    boolean showRequired = false;
    boolean showError = owner.getState().errorMessage != null;
    if (owner.getState() instanceof AbstractFieldState) {
        AbstractFieldState abstractFieldState = (AbstractFieldState) owner.getState();
        showError = showError && !abstractFieldState.hideErrors;
    }
    if (owner instanceof AbstractFieldConnector) {
        showRequired = ((AbstractFieldConnector) owner).isRequired();
    }
    if (icon != null) {
        getElement().removeChild(icon.getElement());
        icon = null;
    }
    if (hasIcon) {
        String uri = owner.getState().resources.get(ComponentConstants.ICON_RESOURCE).getURL();
        icon = getOwner().getConnection().getIcon(uri);
        if (icon instanceof ImageIcon) {
            // onload will set appropriate size later
            icon.setWidth("0");
            icon.setHeight("0");
        }
        DOM.insertChild(getElement(), icon.getElement(), getInsertPosition(InsertPosition.ICON));
        // Icon forces the caption to be above the component
        placedAfterComponent = false;
    }
    if (owner.getState().caption != null) {
        if (captionText == null) {
            captionText = DOM.createDiv();
            captionText.setClassName("v-captiontext");
            DOM.insertChild(getElement(), captionText, getInsertPosition(InsertPosition.CAPTION));
        }
        // Update caption text
        String c = owner.getState().caption;
        // A text forces the caption to be above the component.
        placedAfterComponent = false;
        if (c == null || c.trim().equals("")) {
            // that space is reserved.
            if (!hasIcon && !showRequired && !showError) {
                captionText.setInnerHTML(" ");
            }
        } else {
            setCaptionText(captionText, owner.getState());
        }
    } else if (captionText != null) {
        // Remove existing
        getElement().removeChild(captionText);
        captionText = null;
    }
    if (ComponentStateUtil.hasDescription(owner.getState()) && captionText != null) {
        addStyleDependentName("hasdescription");
    } else {
        removeStyleDependentName("hasdescription");
    }
    AriaHelper.handleInputRequired(owner.getWidget(), showRequired);
    if (showRequired) {
        if (requiredFieldIndicator == null) {
            requiredFieldIndicator = DOM.createDiv();
            requiredFieldIndicator.setClassName("v-required-field-indicator");
            requiredFieldIndicator.setInnerText("*");
            DOM.insertChild(getElement(), requiredFieldIndicator, getInsertPosition(InsertPosition.REQUIRED));
            // Hide the required indicator from assistive device
            Roles.getTextboxRole().setAriaHiddenState(requiredFieldIndicator, true);
        }
    } else if (requiredFieldIndicator != null) {
        // Remove existing
        requiredFieldIndicator.removeFromParent();
        requiredFieldIndicator = null;
    }
    if (isContextHelpIconEnabled(owner.getState())) {
        if (contextHelpIndicatorElement == null) {
            contextHelpIndicatorElement = DOM.createDiv();
            contextHelpIndicatorElement.setClassName(CONTEXT_HELP_CLASSNAME);
            DOM.insertChild(getElement(), contextHelpIndicatorElement, getContextHelpInsertPosition());
            if (clickHandlerRegistration == null) {
                clickHandlerRegistration = addClickHandler(this);
            }
        }
    } else {
        if (contextHelpIndicatorElement != null) {
            contextHelpIndicatorElement.removeFromParent();
            contextHelpIndicatorElement = null;
        }
        if (clickHandlerRegistration != null) {
            clickHandlerRegistration.removeHandler();
            clickHandlerRegistration = null;
        }
    }
    AriaHelper.handleInputInvalid(owner.getWidget(), showError);
    if (showError) {
        if (errorIndicatorElement == null) {
            errorIndicatorElement = DOM.createDiv();
            errorIndicatorElement.setInnerHTML(" ");
            errorIndicatorElement.setClassName("v-errorindicator");
            DOM.insertChild(getElement(), errorIndicatorElement, getInsertPosition(InsertPosition.ERROR));
            // Hide error indicator from assistive devices
            Roles.getTextboxRole().setAriaHiddenState(errorIndicatorElement, true);
        }
    } else if (errorIndicatorElement != null) {
        // Remove existing
        errorIndicatorElement.removeFromParent();
        errorIndicatorElement = null;
    }
    addStyleName(CLASSNAME);
    if (captionHolder != null) {
        captionHolder.captionUpdated(this);
    }
    return (wasPlacedAfterComponent != placedAfterComponent);
}
Also used : ImageIcon(com.vaadin.client.ui.ImageIcon) ComponentConnector(com.vaadin.client.ComponentConnector) AbstractFieldState(com.vaadin.shared.AbstractFieldState) AbstractFieldConnector(com.vaadin.client.ui.AbstractFieldConnector)

Aggregations

ComponentConnector (com.vaadin.client.ComponentConnector)1 AbstractFieldConnector (com.vaadin.client.ui.AbstractFieldConnector)1 ImageIcon (com.vaadin.client.ui.ImageIcon)1 AbstractFieldState (com.vaadin.shared.AbstractFieldState)1