Search in sources :

Example 1 with AbstractFieldState

use of com.vaadin.shared.AbstractFieldState 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)

Example 2 with AbstractFieldState

use of com.vaadin.shared.AbstractFieldState in project cuba by cuba-platform.

the class CubaOrderedActionsLayoutConnector method updateCaptionInternal.

@Override
protected void updateCaptionInternal(ComponentConnector child) {
    // CAUTION copied from superclass
    CubaOrderedLayoutSlot slot = (CubaOrderedLayoutSlot) getWidget().getSlot(child.getWidget());
    String caption = child.getState().caption;
    URLReference iconUrl = child.getState().resources.get(ComponentConstants.ICON_RESOURCE);
    String iconUrlString = iconUrl != null ? iconUrl.getURL() : null;
    Icon icon = child.getConnection().getIcon(iconUrlString);
    List<String> styles = child.getState().styles;
    String error = child.getState().errorMessage;
    boolean showError = error != null;
    if (child.getState() instanceof AbstractFieldState) {
        AbstractFieldState abstractFieldState = (AbstractFieldState) child.getState();
        showError = showError && !abstractFieldState.hideErrors;
    }
    boolean required = false;
    if (child instanceof AbstractFieldConnector) {
        required = ((AbstractFieldConnector) child).isRequired();
    }
    boolean enabled = child.isEnabled();
    if (slot.hasCaption() && null == caption) {
        slot.setCaptionResizeListener(null);
    }
    // Haulmont API
    boolean contextHelpIconEnabled = isContextHelpIconEnabled(child.getState());
    // Haulmont API
    slot.setCaption(caption, contextHelpIconEnabled, icon, styles, error, showError, required, enabled, child.getState().captionAsHtml);
    AriaHelper.handleInputRequired(child.getWidget(), required);
    AriaHelper.handleInputInvalid(child.getWidget(), showError);
    AriaHelper.bindCaption(child.getWidget(), slot.getCaptionElement());
    if (slot.hasCaption()) {
        CaptionPosition pos = slot.getCaptionPosition();
        slot.setCaptionResizeListener(slotCaptionResizeListener);
        if (child.isRelativeHeight() && (pos == CaptionPosition.TOP || pos == CaptionPosition.BOTTOM)) {
            getWidget().updateCaptionOffset(slot.getCaptionElement());
        } else if (child.isRelativeWidth() && (pos == CaptionPosition.LEFT || pos == CaptionPosition.RIGHT)) {
            getWidget().updateCaptionOffset(slot.getCaptionElement());
        }
    }
}
Also used : URLReference(com.vaadin.shared.communication.URLReference) AbstractFieldState(com.vaadin.shared.AbstractFieldState) AbstractFieldConnector(com.vaadin.client.ui.AbstractFieldConnector) Icon(com.vaadin.client.ui.Icon) CaptionPosition(com.vaadin.client.ui.orderedlayout.CaptionPosition)

Aggregations

AbstractFieldConnector (com.vaadin.client.ui.AbstractFieldConnector)2 AbstractFieldState (com.vaadin.shared.AbstractFieldState)2 ComponentConnector (com.vaadin.client.ComponentConnector)1 Icon (com.vaadin.client.ui.Icon)1 ImageIcon (com.vaadin.client.ui.ImageIcon)1 CaptionPosition (com.vaadin.client.ui.orderedlayout.CaptionPosition)1 URLReference (com.vaadin.shared.communication.URLReference)1