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);
}
}
});
}
}
}
}
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");
}
Aggregations