Search in sources :

Example 1 with BehaviorHolderAttachedObjectTarget

use of jakarta.faces.view.BehaviorHolderAttachedObjectTarget in project myfaces by apache.

the class FaceletViewDeclarationLanguage method retargetAttachedObjects.

/**
 * In short words, this method take care of "target" an "attached object".
 * <ul>
 * <li>The "attached object" is instantiated by a tag handler.</li>
 * <li>The "target" is an object used as "marker", that exposes a List&lt;UIComponent&gt;</li>
 * </ul>
 * This method should be called from some composite component tag handler, after
 * all children of composite component has been applied.
 */
@Override
@SuppressWarnings("unchecked")
public void retargetAttachedObjects(FacesContext context, UIComponent topLevelComponent, List<AttachedObjectHandler> handlerList) {
    Assert.notNull(context, "context");
    Assert.notNull(topLevelComponent, "topLevelComponent");
    Assert.notNull(handlerList, "handlerList");
    BeanInfo compositeComponentMetadata = (BeanInfo) topLevelComponent.getAttributes().get(UIComponent.BEANINFO_KEY);
    if (compositeComponentMetadata == null) {
        log.severe("Composite component metadata not found for: " + topLevelComponent.getClientId(context));
        return;
    }
    BeanDescriptor compositeComponentDescriptor = compositeComponentMetadata.getBeanDescriptor();
    List<AttachedObjectTarget> targetList = (List<AttachedObjectTarget>) compositeComponentDescriptor.getValue(AttachedObjectTarget.ATTACHED_OBJECT_TARGETS_KEY);
    if (targetList == null || targetList.isEmpty()) {
        return;
    }
    for (int i = 0, size = handlerList.size(); i < size; i++) {
        AttachedObjectHandler currentHandler = handlerList.get(i);
        // In the spec javadoc this variable is referred as forAttributeValue, but
        // note it is also called curTargetName
        String forValue = currentHandler.getFor();
        // and ClientBehaviorHandler.apply
        for (int k = 0, targetsSize = targetList.size(); k < targetsSize; k++) {
            AttachedObjectTarget currentTarget = targetList.get(k);
            FaceletCompositionContext mctx = FaceletCompositionContext.getCurrentInstance();
            if ((forValue != null && forValue.equals(currentTarget.getName())) && ((currentTarget instanceof ActionSource2AttachedObjectTarget && currentHandler instanceof ActionSource2AttachedObjectHandler) || (currentTarget instanceof EditableValueHolderAttachedObjectTarget && currentHandler instanceof EditableValueHolderAttachedObjectHandler) || (currentTarget instanceof ValueHolderAttachedObjectTarget && currentHandler instanceof ValueHolderAttachedObjectHandler))) {
                // perf: getTargets return ArrayList - see getTargets implementations
                List<UIComponent> targets = currentTarget.getTargets(topLevelComponent);
                for (int l = 0, targetsCount = targets.size(); l < targetsCount; l++) {
                    UIComponent component = targets.get(l);
                    // its tag handler is applied.
                    if (UIComponent.isCompositeComponent(component)) {
                        // How we obtain the list of AttachedObjectHandler for
                        // the current composite component? It should be a component
                        // attribute or retrieved by a key inside component.getAttributes
                        // map. Since api does not specify any attribute, we suppose
                        // this is an implementation detail and it should be retrieved
                        // from component attribute map.
                        // But this is only the point of the iceberg, because we should
                        // define how we register attached object handlers in this list.
                        // ANS: see CompositeComponentResourceTagHandler.
                        // The current handler should be added to the list, to be chained.
                        // Note that the inner component should have a target with the same name
                        // as "for" attribute
                        mctx.addAttachedObjectHandler(component, currentHandler);
                        List<AttachedObjectHandler> handlers = mctx.getAttachedObjectHandlers(component);
                        retargetAttachedObjects(context, component, handlers);
                        handlers.remove(currentHandler);
                    } else {
                        currentHandler.applyAttachedObject(context, component);
                    }
                    if (mctx.isUsingPSSOnThisView() && mctx.isMarkInitialState()) {
                        component.markInitialState();
                    }
                }
            } else if ((currentTarget instanceof BehaviorHolderAttachedObjectTarget && currentHandler instanceof BehaviorHolderAttachedObjectHandler)) {
                String eventName = ((BehaviorHolderAttachedObjectHandler) currentHandler).getEventName();
                boolean isDefaultEvent = ((BehaviorHolderAttachedObjectTarget) currentTarget).isDefaultEvent();
                if ((eventName != null && eventName.equals(currentTarget.getName())) || (eventName == null && isDefaultEvent)) {
                    List<UIComponent> targets = currentTarget.getTargets(topLevelComponent);
                    for (int j = 0, targetssize = targets.size(); j < targetssize; j++) {
                        UIComponent component = targets.get(j);
                        // its tag handler is applied.
                        if (UIComponent.isCompositeComponent(component)) {
                            if (currentTarget instanceof ClientBehaviorAttachedObjectTarget) {
                                mctx.addAttachedObjectHandler(component, new ClientBehaviorRedirectBehaviorAttachedObjectHandlerWrapper((BehaviorHolderAttachedObjectHandler) currentHandler, ((ClientBehaviorAttachedObjectTarget) currentTarget).getEvent()));
                            } else {
                                mctx.addAttachedObjectHandler(component, currentHandler);
                            }
                            List<AttachedObjectHandler> handlers = mctx.getAttachedObjectHandlers(component);
                            retargetAttachedObjects(context, component, handlers);
                            handlers.remove(currentHandler);
                        } else {
                            if (currentHandler instanceof ClientBehaviorRedirectBehaviorAttachedObjectHandlerWrapper) {
                                ClientBehaviorRedirectBehaviorAttachedObjectHandlerWrapper wrapper = (ClientBehaviorRedirectBehaviorAttachedObjectHandlerWrapper) currentHandler;
                                currentHandler.applyAttachedObject(context, new ClientBehaviorRedirectEventComponentWrapper(topLevelComponent, component, wrapper.getWrappedEventName(), eventName));
                            } else {
                                currentHandler.applyAttachedObject(context, component);
                            }
                        }
                        if (mctx.isUsingPSSOnThisView() && mctx.isMarkInitialState()) {
                            component.markInitialState();
                        }
                    }
                }
            }
        }
    }
}
Also used : EditableValueHolderAttachedObjectHandler(jakarta.faces.view.EditableValueHolderAttachedObjectHandler) ValueHolderAttachedObjectHandler(jakarta.faces.view.ValueHolderAttachedObjectHandler) ValueHolderAttachedObjectTarget(jakarta.faces.view.ValueHolderAttachedObjectTarget) ActionSource2AttachedObjectTarget(jakarta.faces.view.ActionSource2AttachedObjectTarget) BehaviorHolderAttachedObjectTarget(jakarta.faces.view.BehaviorHolderAttachedObjectTarget) ClientBehaviorAttachedObjectTarget(org.apache.myfaces.view.facelets.tag.composite.ClientBehaviorAttachedObjectTarget) AttachedObjectTarget(jakarta.faces.view.AttachedObjectTarget) EditableValueHolderAttachedObjectTarget(jakarta.faces.view.EditableValueHolderAttachedObjectTarget) BeanInfo(java.beans.BeanInfo) ClientBehaviorRedirectBehaviorAttachedObjectHandlerWrapper(org.apache.myfaces.view.facelets.tag.composite.ClientBehaviorRedirectBehaviorAttachedObjectHandlerWrapper) UIComponent(jakarta.faces.component.UIComponent) BehaviorHolderAttachedObjectTarget(jakarta.faces.view.BehaviorHolderAttachedObjectTarget) ActionSource2AttachedObjectHandler(jakarta.faces.view.ActionSource2AttachedObjectHandler) AttachedObjectHandler(jakarta.faces.view.AttachedObjectHandler) EditableValueHolderAttachedObjectHandler(jakarta.faces.view.EditableValueHolderAttachedObjectHandler) ValueHolderAttachedObjectHandler(jakarta.faces.view.ValueHolderAttachedObjectHandler) BehaviorHolderAttachedObjectHandler(jakarta.faces.view.BehaviorHolderAttachedObjectHandler) ActionSource2AttachedObjectTarget(jakarta.faces.view.ActionSource2AttachedObjectTarget) ClientBehaviorRedirectEventComponentWrapper(org.apache.myfaces.view.facelets.tag.composite.ClientBehaviorRedirectEventComponentWrapper) EditableValueHolderAttachedObjectHandler(jakarta.faces.view.EditableValueHolderAttachedObjectHandler) ValueHolderAttachedObjectTarget(jakarta.faces.view.ValueHolderAttachedObjectTarget) EditableValueHolderAttachedObjectTarget(jakarta.faces.view.EditableValueHolderAttachedObjectTarget) BehaviorHolderAttachedObjectHandler(jakarta.faces.view.BehaviorHolderAttachedObjectHandler) BeanDescriptor(java.beans.BeanDescriptor) EditableValueHolderAttachedObjectTarget(jakarta.faces.view.EditableValueHolderAttachedObjectTarget) ClientBehaviorAttachedObjectTarget(org.apache.myfaces.view.facelets.tag.composite.ClientBehaviorAttachedObjectTarget) ActionSource2AttachedObjectHandler(jakarta.faces.view.ActionSource2AttachedObjectHandler) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with BehaviorHolderAttachedObjectTarget

use of jakarta.faces.view.BehaviorHolderAttachedObjectTarget in project mojarra by eclipse-ee4j.

the class BehaviorTagHandlerDelegateImpl method apply.

@Override
public void apply(FaceletContext ctx, UIComponent parent) throws IOException {
    // only process if it's been created
    if (parent == null || !(parent.getParent() == null)) {
        return;
    }
    ComponentSupport.copyPassthroughAttributes(ctx, parent, owner.getTag());
    if (UIComponent.isCompositeComponent(parent)) {
        // Check composite component event name:
        BeanInfo componentBeanInfo = (BeanInfo) parent.getAttributes().get(UIComponent.BEANINFO_KEY);
        if (null == componentBeanInfo) {
            throw new TagException(owner.getTag(), "Error: enclosing composite component does not have BeanInfo attribute");
        }
        BeanDescriptor componentDescriptor = componentBeanInfo.getBeanDescriptor();
        if (null == componentDescriptor) {
            throw new TagException(owner.getTag(), "Error: enclosing composite component BeanInfo does not have BeanDescriptor");
        }
        List<AttachedObjectTarget> targetList = (List<AttachedObjectTarget>) componentDescriptor.getValue(AttachedObjectTarget.ATTACHED_OBJECT_TARGETS_KEY);
        if (null == targetList) {
            throw new TagException(owner.getTag(), "Error: enclosing composite component does not support behavior events");
        }
        String eventName = owner.getEventName();
        boolean supportedEvent = false;
        for (AttachedObjectTarget target : targetList) {
            if (target instanceof BehaviorHolderAttachedObjectTarget) {
                BehaviorHolderAttachedObjectTarget behaviorTarget = (BehaviorHolderAttachedObjectTarget) target;
                if (null != eventName && eventName.equals(behaviorTarget.getName()) || null == eventName && behaviorTarget.isDefaultEvent()) {
                    supportedEvent = true;
                    break;
                }
            }
        }
        if (supportedEvent) {
            CompositeComponentTagHandler.getAttachedObjectHandlers(parent).add(owner);
        } else {
            throw new TagException(owner.getTag(), "Error: enclosing composite component does not support event " + eventName);
        }
    } else if (parent instanceof ClientBehaviorHolder) {
        owner.applyAttachedObject(ctx.getFacesContext(), parent);
    } else {
        throw new TagException(owner.getTag(), "Parent not an instance of ClientBehaviorHolder: " + parent);
    }
}
Also used : BehaviorHolderAttachedObjectTarget(jakarta.faces.view.BehaviorHolderAttachedObjectTarget) AttachedObjectTarget(jakarta.faces.view.AttachedObjectTarget) TagException(jakarta.faces.view.facelets.TagException) BeanDescriptor(java.beans.BeanDescriptor) BeanInfo(java.beans.BeanInfo) BehaviorHolderAttachedObjectTarget(jakarta.faces.view.BehaviorHolderAttachedObjectTarget) List(java.util.List) ClientBehaviorHolder(jakarta.faces.component.behavior.ClientBehaviorHolder)

Example 3 with BehaviorHolderAttachedObjectTarget

use of jakarta.faces.view.BehaviorHolderAttachedObjectTarget in project mojarra by eclipse-ee4j.

the class FaceletViewHandlingStrategy method retargetAttachedObjects.

/**
 * @see ViewHandlingStrategy#retargetAttachedObjects(jakarta.faces.context.FacesContext,
 * jakarta.faces.component.UIComponent, java.util.List)
 */
@SuppressWarnings({ "unchecked" })
@Override
public void retargetAttachedObjects(FacesContext context, UIComponent topLevelComponent, List<AttachedObjectHandler> handlers) {
    notNull("context", context);
    notNull("topLevelComponent", topLevelComponent);
    notNull("handlers", handlers);
    if (handlers == null || handlers.isEmpty()) {
        return;
    }
    BeanInfo componentBeanInfo = (BeanInfo) topLevelComponent.getAttributes().get(BEANINFO_KEY);
    if (componentBeanInfo == null) {
        return;
    }
    BeanDescriptor componentDescriptor = componentBeanInfo.getBeanDescriptor();
    // There is an entry in targetList for each attached object in the
    // <composite:interface> section of the composite component.
    List<AttachedObjectTarget> targetList = (List<AttachedObjectTarget>) componentDescriptor.getValue(ATTACHED_OBJECT_TARGETS_KEY);
    // Each entry in targetList will vend one or more UIComponent instances
    // that is to serve as the target of an attached object in the consuming
    // page.
    List<UIComponent> targetComponents;
    String forAttributeValue, curTargetName;
    // For each of the attached object handlers...
    for (AttachedObjectHandler curHandler : handlers) {
        // Get the name given to this attached object by the page author
        // in the consuming page.
        forAttributeValue = curHandler.getFor();
        // of this composite component...
        for (AttachedObjectTarget curTarget : targetList) {
            // Get the name given to this attached object target by the
            // composite component author
            curTargetName = curTarget.getName();
            targetComponents = curTarget.getTargets(topLevelComponent);
            if (curHandler instanceof ActionSource2AttachedObjectHandler && curTarget instanceof ActionSource2AttachedObjectTarget) {
                if (forAttributeValue.equals(curTargetName)) {
                    for (UIComponent curTargetComponent : targetComponents) {
                        retargetHandler(context, curHandler, curTargetComponent);
                    }
                    break;
                }
            } else if (curHandler instanceof EditableValueHolderAttachedObjectHandler && curTarget instanceof EditableValueHolderAttachedObjectTarget) {
                if (forAttributeValue.equals(curTargetName)) {
                    for (UIComponent curTargetComponent : targetComponents) {
                        retargetHandler(context, curHandler, curTargetComponent);
                    }
                    break;
                }
            } else if (curHandler instanceof ValueHolderAttachedObjectHandler && curTarget instanceof ValueHolderAttachedObjectTarget) {
                if (forAttributeValue.equals(curTargetName)) {
                    for (UIComponent curTargetComponent : targetComponents) {
                        retargetHandler(context, curHandler, curTargetComponent);
                    }
                    break;
                }
            } else if (curHandler instanceof BehaviorHolderAttachedObjectHandler && curTarget instanceof BehaviorHolderAttachedObjectTarget) {
                BehaviorHolderAttachedObjectHandler behaviorHandler = (BehaviorHolderAttachedObjectHandler) curHandler;
                BehaviorHolderAttachedObjectTarget behaviorTarget = (BehaviorHolderAttachedObjectTarget) curTarget;
                String eventName = behaviorHandler.getEventName();
                if (null != eventName && eventName.equals(curTargetName) || null == eventName && behaviorTarget.isDefaultEvent()) {
                    for (UIComponent curTargetComponent : targetComponents) {
                        retargetHandler(context, curHandler, curTargetComponent);
                    }
                }
            }
        }
    }
}
Also used : EditableValueHolderAttachedObjectHandler(jakarta.faces.view.EditableValueHolderAttachedObjectHandler) ValueHolderAttachedObjectHandler(jakarta.faces.view.ValueHolderAttachedObjectHandler) ValueHolderAttachedObjectTarget(jakarta.faces.view.ValueHolderAttachedObjectTarget) ActionSource2AttachedObjectTarget(jakarta.faces.view.ActionSource2AttachedObjectTarget) BehaviorHolderAttachedObjectTarget(jakarta.faces.view.BehaviorHolderAttachedObjectTarget) AttachedObjectTarget(jakarta.faces.view.AttachedObjectTarget) EditableValueHolderAttachedObjectTarget(jakarta.faces.view.EditableValueHolderAttachedObjectTarget) BeanInfo(java.beans.BeanInfo) CompositeComponentBeanInfo(com.sun.faces.facelets.tag.composite.CompositeComponentBeanInfo) UIComponent(jakarta.faces.component.UIComponent) BehaviorHolderAttachedObjectTarget(jakarta.faces.view.BehaviorHolderAttachedObjectTarget) ActionSource2AttachedObjectHandler(jakarta.faces.view.ActionSource2AttachedObjectHandler) AttachedObjectHandler(jakarta.faces.view.AttachedObjectHandler) EditableValueHolderAttachedObjectHandler(jakarta.faces.view.EditableValueHolderAttachedObjectHandler) ValueHolderAttachedObjectHandler(jakarta.faces.view.ValueHolderAttachedObjectHandler) BehaviorHolderAttachedObjectHandler(jakarta.faces.view.BehaviorHolderAttachedObjectHandler) ActionSource2AttachedObjectTarget(jakarta.faces.view.ActionSource2AttachedObjectTarget) EditableValueHolderAttachedObjectHandler(jakarta.faces.view.EditableValueHolderAttachedObjectHandler) ValueHolderAttachedObjectTarget(jakarta.faces.view.ValueHolderAttachedObjectTarget) EditableValueHolderAttachedObjectTarget(jakarta.faces.view.EditableValueHolderAttachedObjectTarget) BehaviorHolderAttachedObjectHandler(jakarta.faces.view.BehaviorHolderAttachedObjectHandler) BeanDescriptor(java.beans.BeanDescriptor) EditableValueHolderAttachedObjectTarget(jakarta.faces.view.EditableValueHolderAttachedObjectTarget) ActionSource2AttachedObjectHandler(jakarta.faces.view.ActionSource2AttachedObjectHandler) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List)

Example 4 with BehaviorHolderAttachedObjectTarget

use of jakarta.faces.view.BehaviorHolderAttachedObjectTarget in project mojarra by eclipse-ee4j.

the class AjaxBehaviorListenerImpl method applyNested.

// Applies a nested AjaxHandler by adding the AjaxBehavior to the
// parent component.
private void applyNested(FaceletContext ctx, UIComponent parent, String eventName) {
    if (!ComponentHandler.isNew(parent)) {
        return;
    }
    // Composite component case
    if (UIComponent.isCompositeComponent(parent)) {
        // Check composite component event name:
        boolean tagApplied = false;
        if (parent instanceof ClientBehaviorHolder) {
            // error here will propagate up
            applyAttachedObject(ctx, parent, eventName);
            tagApplied = true;
        }
        BeanInfo componentBeanInfo = (BeanInfo) parent.getAttributes().get(UIComponent.BEANINFO_KEY);
        if (null == componentBeanInfo) {
            throw new TagException(tag, "Error: enclosing composite component does not have BeanInfo attribute");
        }
        BeanDescriptor componentDescriptor = componentBeanInfo.getBeanDescriptor();
        if (null == componentDescriptor) {
            throw new TagException(tag, "Error: enclosing 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, "Error: enclosing composite component does not support behavior events");
        }
        boolean supportedEvent = false;
        for (AttachedObjectTarget target : targetList) {
            if (target instanceof BehaviorHolderAttachedObjectTarget) {
                BehaviorHolderAttachedObjectTarget behaviorTarget = (BehaviorHolderAttachedObjectTarget) target;
                if (null != eventName && eventName.equals(behaviorTarget.getName()) || null == eventName && behaviorTarget.isDefaultEvent()) {
                    supportedEvent = true;
                    break;
                }
            }
        }
        if (supportedEvent) {
            CompositeComponentTagHandler.getAttachedObjectHandlers(parent).add(this);
        } else {
            if (!tagApplied) {
                throw new TagException(tag, "Error: enclosing composite component does not support event " + eventName);
            }
        }
    } else if (parent instanceof ClientBehaviorHolder) {
        applyAttachedObject(ctx, parent, eventName);
    } else {
        throw new TagException(tag, "Unable to attach <f:ajax> to non-ClientBehaviorHolder parent");
    }
}
Also used : AttachedObjectTarget(jakarta.faces.view.AttachedObjectTarget) BehaviorHolderAttachedObjectTarget(jakarta.faces.view.BehaviorHolderAttachedObjectTarget) TagException(jakarta.faces.view.facelets.TagException) BeanDescriptor(java.beans.BeanDescriptor) BeanInfo(java.beans.BeanInfo) BehaviorHolderAttachedObjectTarget(jakarta.faces.view.BehaviorHolderAttachedObjectTarget) ArrayList(java.util.ArrayList) List(java.util.List) ClientBehaviorHolder(jakarta.faces.component.behavior.ClientBehaviorHolder)

Aggregations

AttachedObjectTarget (jakarta.faces.view.AttachedObjectTarget)4 BehaviorHolderAttachedObjectTarget (jakarta.faces.view.BehaviorHolderAttachedObjectTarget)4 BeanDescriptor (java.beans.BeanDescriptor)4 BeanInfo (java.beans.BeanInfo)4 List (java.util.List)4 ArrayList (java.util.ArrayList)3 UIComponent (jakarta.faces.component.UIComponent)2 ClientBehaviorHolder (jakarta.faces.component.behavior.ClientBehaviorHolder)2 ActionSource2AttachedObjectHandler (jakarta.faces.view.ActionSource2AttachedObjectHandler)2 ActionSource2AttachedObjectTarget (jakarta.faces.view.ActionSource2AttachedObjectTarget)2 AttachedObjectHandler (jakarta.faces.view.AttachedObjectHandler)2 BehaviorHolderAttachedObjectHandler (jakarta.faces.view.BehaviorHolderAttachedObjectHandler)2 EditableValueHolderAttachedObjectHandler (jakarta.faces.view.EditableValueHolderAttachedObjectHandler)2 EditableValueHolderAttachedObjectTarget (jakarta.faces.view.EditableValueHolderAttachedObjectTarget)2 ValueHolderAttachedObjectHandler (jakarta.faces.view.ValueHolderAttachedObjectHandler)2 ValueHolderAttachedObjectTarget (jakarta.faces.view.ValueHolderAttachedObjectTarget)2 TagException (jakarta.faces.view.facelets.TagException)2 CompositeComponentBeanInfo (com.sun.faces.facelets.tag.composite.CompositeComponentBeanInfo)1 Collections.emptyList (java.util.Collections.emptyList)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1