Search in sources :

Example 1 with AttachedObjectTarget

use of javax.faces.view.AttachedObjectTarget in project primefaces by primefaces.

the class AbstractBehaviorHandler method apply.

@Override
public void apply(FaceletContext faceletContext, UIComponent parent) throws IOException {
    if (!ComponentHandler.isNew(parent)) {
        return;
    }
    String eventName = getEventName();
    if (UIComponent.isCompositeComponent(parent)) {
        boolean tagApplied = false;
        if (parent instanceof ClientBehaviorHolder) {
            applyAttachedObject(faceletContext, parent);
            tagApplied = true;
        }
        BeanInfo componentBeanInfo = (BeanInfo) parent.getAttributes().get(UIComponent.BEANINFO_KEY);
        if (null == componentBeanInfo) {
            throw new TagException(tag, "Composite component does not have BeanInfo attribute");
        }
        BeanDescriptor componentDescriptor = componentBeanInfo.getBeanDescriptor();
        if (null == componentDescriptor) {
            throw new TagException(tag, "Composite component BeanInfo does not have BeanDescriptor");
        }
        List<AttachedObjectTarget> targetList = (List<AttachedObjectTarget>) componentDescriptor.getValue(AttachedObjectTarget.ATTACHED_OBJECT_TARGETS_KEY);
        if (null == targetList && !tagApplied) {
            throw new TagException(tag, "Composite component does not support behavior events");
        }
        boolean supportedEvent = false;
        if (targetList != null) {
            for (int i = 0; i < targetList.size(); i++) {
                AttachedObjectTarget target = targetList.get(i);
                if (target instanceof BehaviorHolderAttachedObjectTarget) {
                    BehaviorHolderAttachedObjectTarget behaviorTarget = (BehaviorHolderAttachedObjectTarget) target;
                    if ((null != eventName && eventName.equals(behaviorTarget.getName())) || (null == eventName && behaviorTarget.isDefaultEvent())) {
                        supportedEvent = true;
                        break;
                    }
                }
            }
        }
        if (supportedEvent) {
            // Workaround to implementation specific composite component handlers
            FacesContext context = FacesContext.getCurrentInstance();
            PrimeEnvironment environment = PrimeApplicationContext.getCurrentInstance(context).getEnvironment();
            if (environment.isMojarra()) {
                addAttachedObjectHandlerToMojarra(environment, parent);
            } else {
                addAttachedObjectHandlerToMyFaces(parent, faceletContext);
            }
        } else {
            if (!tagApplied) {
                throw new TagException(tag, "Composite component does not support event " + eventName);
            }
        }
    } else if (parent instanceof ClientBehaviorHolder) {
        applyAttachedObject(faceletContext, parent);
    } else {
        throw new TagException(tag, "Unable to attach behavior to non-ClientBehaviorHolder parent");
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) BehaviorHolderAttachedObjectTarget(javax.faces.view.BehaviorHolderAttachedObjectTarget) AttachedObjectTarget(javax.faces.view.AttachedObjectTarget) BeanInfo(java.beans.BeanInfo) BehaviorHolderAttachedObjectTarget(javax.faces.view.BehaviorHolderAttachedObjectTarget) ClientBehaviorHolder(javax.faces.component.behavior.ClientBehaviorHolder) BeanDescriptor(java.beans.BeanDescriptor) PrimeEnvironment(org.primefaces.config.PrimeEnvironment) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with AttachedObjectTarget

use of javax.faces.view.AttachedObjectTarget 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)

Aggregations

BeanInfo (java.beans.BeanInfo)2 List (java.util.List)2 FacesContext (javax.faces.context.FacesContext)2 AttachedObjectTarget (javax.faces.view.AttachedObjectTarget)2 BeanDescriptor (java.beans.BeanDescriptor)1 ArrayList (java.util.ArrayList)1 FacesException (javax.faces.FacesException)1 ContextCallback (javax.faces.component.ContextCallback)1 EditableValueHolder (javax.faces.component.EditableValueHolder)1 UIComponent (javax.faces.component.UIComponent)1 ClientBehaviorHolder (javax.faces.component.behavior.ClientBehaviorHolder)1 BehaviorHolderAttachedObjectTarget (javax.faces.view.BehaviorHolderAttachedObjectTarget)1 EditableValueHolderAttachedObjectTarget (javax.faces.view.EditableValueHolderAttachedObjectTarget)1 PrimeEnvironment (org.primefaces.config.PrimeEnvironment)1