Search in sources :

Example 1 with ContextCallback

use of javax.faces.component.ContextCallback in project primefaces by primefaces.

the class CompositeUtils method invokeOnDeepestEditableValueHolder.

/**
 * Attention: This only supports cc:editableValueHolder which target a single component!
 *
 * @param context
 * @param composite
 * @param callback
 */
public static void invokeOnDeepestEditableValueHolder(FacesContext context, UIComponent composite, final ContextCallback callback) {
    if (composite instanceof EditableValueHolder) {
        callback.invokeContextCallback(context, composite);
        return;
    }
    BeanInfo info = (BeanInfo) composite.getAttributes().get(UIComponent.BEANINFO_KEY);
    List<AttachedObjectTarget> targets = (List<AttachedObjectTarget>) info.getBeanDescriptor().getValue(AttachedObjectTarget.ATTACHED_OBJECT_TARGETS_KEY);
    if (targets != null) {
        for (int i = 0; i < targets.size(); i++) {
            AttachedObjectTarget target = targets.get(i);
            if (target instanceof EditableValueHolderAttachedObjectTarget) {
                List<UIComponent> childs = target.getTargets(composite);
                if (childs == null || childs.isEmpty()) {
                    throw new FacesException("Cannot not resolve editableValueHolder target in composite component with id: \"" + composite.getClientId() + "\"");
                }
                if (childs.size() > 1) {
                    throw new FacesException("Only a single editableValueHolder target is supported in composite component with id: \"" + composite.getClientId() + "\"");
                }
                final UIComponent child = childs.get(0);
                composite.invokeOnComponent(context, composite.getClientId(context), new ContextCallback() {

                    @Override
                    public void invokeContextCallback(FacesContext context, UIComponent target) {
                        if (isComposite(child)) {
                            invokeOnDeepestEditableValueHolder(context, child, callback);
                        } else {
                            callback.invokeContextCallback(context, child);
                        }
                    }
                });
            }
        }
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) AttachedObjectTarget(javax.faces.view.AttachedObjectTarget) EditableValueHolderAttachedObjectTarget(javax.faces.view.EditableValueHolderAttachedObjectTarget) BeanInfo(java.beans.BeanInfo) UIComponent(javax.faces.component.UIComponent) FacesException(javax.faces.FacesException) EditableValueHolderAttachedObjectTarget(javax.faces.view.EditableValueHolderAttachedObjectTarget) ContextCallback(javax.faces.component.ContextCallback) List(java.util.List) EditableValueHolder(javax.faces.component.EditableValueHolder)

Example 2 with ContextCallback

use of javax.faces.component.ContextCallback in project primefaces by primefaces.

the class OutputLabelRenderer method encodeEnd.

@Override
public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
    final ResponseWriter writer = context.getResponseWriter();
    final OutputLabel label = (OutputLabel) component;
    final String clientId = label.getClientId(context);
    final String value = ComponentUtils.getValueToRender(context, label);
    final StringBuilder styleClass = SharedStringBuilder.get(context, SB_STYLE_CLASS);
    styleClass.append(OutputLabel.STYLE_CLASS);
    if (label.getStyleClass() != null) {
        styleClass.append(" ");
        styleClass.append(label.getStyleClass());
    }
    final EditableValueHolderState state = new EditableValueHolderState();
    final String indicateRequired = label.getIndicateRequired();
    boolean isAuto = "auto".equals(indicateRequired) || "autoSkipDisabled".equals(indicateRequired);
    String _for = label.getFor();
    if (!isValueBlank(_for)) {
        ContextCallback callback = new ContextCallback() {

            @Override
            public void invokeContextCallback(FacesContext context, UIComponent target) {
                if (target instanceof InputHolder) {
                    InputHolder inputHolder = ((InputHolder) target);
                    state.setClientId(inputHolder.getInputClientId());
                    inputHolder.setLabelledBy(clientId);
                } else {
                    state.setClientId(target.getClientId(context));
                }
                if (target instanceof UIInput) {
                    UIInput input = (UIInput) target;
                    if (value != null && (input.getAttributes().get("label") == null || input.getValueExpression("label") == null)) {
                        ValueExpression ve = label.getValueExpression("value");
                        if (ve != null) {
                            input.setValueExpression("label", ve);
                        } else {
                            String labelString = value;
                            int colonPos = labelString.lastIndexOf(':');
                            if (colonPos != -1) {
                                labelString = labelString.substring(0, colonPos);
                            }
                            input.getAttributes().put("label", labelString);
                        }
                    }
                    if (!input.isValid()) {
                        styleClass.append(" ui-state-error");
                    }
                    if (isAuto) {
                        boolean disabled = false;
                        if ("autoSkipDisabled".equals(indicateRequired)) {
                            disabled = Boolean.parseBoolean(String.valueOf(input.getAttributes().get("disabled"))) || Boolean.parseBoolean(String.valueOf(input.getAttributes().get("readonly")));
                        }
                        if (disabled) {
                            state.setRequired(false);
                        } else {
                            state.setRequired(input.isRequired());
                            // fallback if required=false
                            if (!state.isRequired()) {
                                PrimeApplicationContext applicationContext = PrimeApplicationContext.getCurrentInstance(context);
                                if (applicationContext.getConfig().isBeanValidationEnabled() && isBeanValidationDefined(input, context)) {
                                    state.setRequired(true);
                                }
                            }
                        }
                    }
                }
            }
        };
        UIComponent forComponent = SearchExpressionFacade.resolveComponent(context, label, _for);
        if (CompositeUtils.isComposite(forComponent)) {
            CompositeUtils.invokeOnDeepestEditableValueHolder(context, forComponent, callback);
        } else {
            callback.invokeContextCallback(context, forComponent);
        }
    }
    writer.startElement("label", label);
    writer.writeAttribute("id", clientId, "id");
    writer.writeAttribute("class", styleClass.toString(), "id");
    renderPassThruAttributes(context, label, HTML.LABEL_ATTRS);
    renderDomEvents(context, label, HTML.LABEL_EVENTS);
    if (!isValueBlank(_for)) {
        writer.writeAttribute("for", state.getClientId(), "for");
    }
    if (value != null) {
        if (label.isEscape()) {
            writer.writeText(value, "value");
        } else {
            writer.write(value);
        }
    }
    renderChildren(context, label);
    if ("true".equals(indicateRequired) || (isAuto && !isValueBlank(_for) && state.isRequired())) {
        encodeRequiredIndicator(writer, label);
    }
    writer.endElement("label");
}
Also used : FacesContext(javax.faces.context.FacesContext) PrimeApplicationContext(org.primefaces.context.PrimeApplicationContext) UIComponent(javax.faces.component.UIComponent) InputHolder(org.primefaces.component.api.InputHolder) UIInput(javax.faces.component.UIInput) ResponseWriter(javax.faces.context.ResponseWriter) ContextCallback(javax.faces.component.ContextCallback) ValueExpression(javax.el.ValueExpression)

Aggregations

ContextCallback (javax.faces.component.ContextCallback)2 UIComponent (javax.faces.component.UIComponent)2 FacesContext (javax.faces.context.FacesContext)2 BeanInfo (java.beans.BeanInfo)1 List (java.util.List)1 ValueExpression (javax.el.ValueExpression)1 FacesException (javax.faces.FacesException)1 EditableValueHolder (javax.faces.component.EditableValueHolder)1 UIInput (javax.faces.component.UIInput)1 ResponseWriter (javax.faces.context.ResponseWriter)1 AttachedObjectTarget (javax.faces.view.AttachedObjectTarget)1 EditableValueHolderAttachedObjectTarget (javax.faces.view.EditableValueHolderAttachedObjectTarget)1 InputHolder (org.primefaces.component.api.InputHolder)1 PrimeApplicationContext (org.primefaces.context.PrimeApplicationContext)1