Search in sources :

Example 1 with FaceletContext

use of jakarta.faces.view.facelets.FaceletContext in project myfaces by apache.

the class AjaxHandler method applyAttachedObject.

/**
 * This method should create an AjaxBehavior object and attach it to the
 * parent component.
 *
 * Also, it should check if the parent can apply the selected AjaxBehavior
 * to the selected component through ClientBehaviorHolder.getEventNames() or
 * ClientBehaviorHolder.getDefaultEventName()
 */
@Override
public void applyAttachedObject(FacesContext context, UIComponent parent) {
    // Retrieve the current FaceletContext from FacesContext object
    FaceletContext faceletContext = (FaceletContext) context.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
    // cast to a ClientBehaviorHolder
    ClientBehaviorHolder cvh = (ClientBehaviorHolder) parent;
    String eventName = null;
    if (_event != null) {
        if (_event.isLiteral()) {
            eventName = _event.getValue();
        } else {
            eventName = (String) _event.getValueExpression(faceletContext, String.class).getValue(faceletContext);
        }
    }
    if (eventName == null) {
        eventName = cvh.getDefaultEventName();
        if (eventName == null) {
            if (_wrapMode) {
                // for this attached object.
                return;
            } else {
                throw new TagAttributeException(_event, "eventName could not be defined for f:ajax tag with no wrap mode.");
            }
        }
    } else if (!cvh.getEventNames().contains(eventName)) {
        if (_wrapMode) {
            // component could not be the target for this attached object.
            return;
        } else {
            throw new TagAttributeException(_event, "event it is not a valid eventName defined for this component");
        }
    }
    Map<String, List<ClientBehavior>> clientBehaviors = cvh.getClientBehaviors();
    List<ClientBehavior> clientBehaviorList = clientBehaviors.get(eventName);
    if (clientBehaviorList != null && !clientBehaviorList.isEmpty()) {
        for (ClientBehavior cb : clientBehaviorList) {
            if (cb instanceof AjaxBehavior) {
                // the outer one.
                return;
            }
        }
    }
    AjaxBehavior ajaxBehavior = createBehavior(context);
    if (_disabled != null) {
        if (_disabled.isLiteral()) {
            ajaxBehavior.setDisabled(_disabled.getBoolean(faceletContext));
        } else {
            ajaxBehavior.setValueExpression("disabled", _disabled.getValueExpression(faceletContext, Boolean.class));
        }
    }
    if (_execute != null) {
        ajaxBehavior.setValueExpression("execute", _execute.getValueExpression(faceletContext, Object.class));
    }
    if (_immediate != null) {
        if (_immediate.isLiteral()) {
            ajaxBehavior.setImmediate(_immediate.getBoolean(faceletContext));
        } else {
            ajaxBehavior.setValueExpression("immediate", _immediate.getValueExpression(faceletContext, Boolean.class));
        }
    }
    if (_listener != null) {
        MethodExpression expr = _listener.getMethodExpression(faceletContext, Void.TYPE, AJAX_BEHAVIOR_LISTENER_SIG);
        AjaxBehaviorListener abl = new AjaxBehaviorListenerImpl(expr);
        ajaxBehavior.addAjaxBehaviorListener(abl);
    }
    if (_onerror != null) {
        if (_onerror.isLiteral()) {
            ajaxBehavior.setOnerror(_onerror.getValue(faceletContext));
        } else {
            ajaxBehavior.setValueExpression("onerror", _onerror.getValueExpression(faceletContext, String.class));
        }
    }
    if (_onevent != null) {
        if (_onevent.isLiteral()) {
            ajaxBehavior.setOnevent(_onevent.getValue(faceletContext));
        } else {
            ajaxBehavior.setValueExpression("onevent", _onevent.getValueExpression(faceletContext, String.class));
        }
    }
    if (_render != null) {
        ajaxBehavior.setValueExpression("render", _render.getValueExpression(faceletContext, Object.class));
    }
    if (_delay != null) {
        if (_delay.isLiteral()) {
            ajaxBehavior.setDelay(_delay.getValue(faceletContext));
        } else {
            ajaxBehavior.setValueExpression("delay", _delay.getValueExpression(faceletContext, String.class));
        }
    }
    if (_resetValues != null) {
        if (_resetValues.isLiteral()) {
            ajaxBehavior.setResetValues(_resetValues.getBoolean(faceletContext));
        } else {
            ajaxBehavior.setValueExpression("resetValues", _resetValues.getValueExpression(faceletContext, Boolean.class));
        }
    }
    // map @this in a composite to @composite
    if (parent instanceof ClientBehaviorRedirectEventComponentWrapper) {
        UIComponent composite = ((ClientBehaviorRedirectEventComponentWrapper) parent).getComposite();
        if (composite != null) {
            Collection<String> execute = ajaxBehavior.getExecute();
            if (execute != null && execute.contains("@this")) {
                Collection<String> newExecute = new ArrayList<>(execute);
                newExecute.remove("@this");
                newExecute.add("@composite");
                ajaxBehavior.setExecute(newExecute);
            }
            Collection<String> render = ajaxBehavior.getRender();
            if (render != null && render.contains("@this")) {
                Collection<String> newRender = new ArrayList<>(render);
                newRender.remove("@this");
                newRender.add("@composite");
                ajaxBehavior.setRender(newRender);
            }
        }
    }
    cvh.addClientBehavior(eventName, ajaxBehavior);
}
Also used : TagAttributeException(jakarta.faces.view.facelets.TagAttributeException) AjaxBehaviorListener(jakarta.faces.event.AjaxBehaviorListener) UIComponent(jakarta.faces.component.UIComponent) ArrayList(java.util.ArrayList) ClientBehaviorHolder(jakarta.faces.component.behavior.ClientBehaviorHolder) MethodExpression(jakarta.el.MethodExpression) AjaxBehavior(jakarta.faces.component.behavior.AjaxBehavior) ClientBehaviorRedirectEventComponentWrapper(org.apache.myfaces.view.facelets.tag.composite.ClientBehaviorRedirectEventComponentWrapper) FaceletContext(jakarta.faces.view.facelets.FaceletContext) AbstractFaceletContext(org.apache.myfaces.view.facelets.AbstractFaceletContext) ArrayList(java.util.ArrayList) List(java.util.List) ClientBehavior(jakarta.faces.component.behavior.ClientBehavior)

Example 2 with FaceletContext

use of jakarta.faces.view.facelets.FaceletContext in project myfaces by apache.

the class ValidatorTagHandlerDelegate method applyAttachedObject.

@SuppressWarnings("unchecked")
@Override
public void applyAttachedObject(FacesContext context, UIComponent parent) {
    // Retrieve the current FaceletContext from FacesContext object
    FaceletContext faceletContext = (FaceletContext) context.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
    // id from beeing registered on the component.
    if (_delegate.isDisabled(faceletContext)) {
        // tag is disabled --> add its validatorId to the parent's exclusion list
        String validatorId = _delegate.getValidatorConfig().getValidatorId();
        if (validatorId != null && !validatorId.isEmpty()) {
            List<String> exclusionList = (List<String>) parent.getAttributes().get(VALIDATOR_ID_EXCLUSION_LIST_KEY);
            if (exclusionList == null) {
                exclusionList = new ArrayList<String>();
                parent.getAttributes().put(VALIDATOR_ID_EXCLUSION_LIST_KEY, exclusionList);
            }
            exclusionList.add(validatorId);
        }
    } else {
        // tag is enabled --> create the validator and attach it
        // cast to a ValueHolder
        EditableValueHolder evh = (EditableValueHolder) parent;
        ValueExpression ve = null;
        Validator v = null;
        if (_delegate.getBinding() != null) {
            ve = _delegate.getBinding().getValueExpression(faceletContext, Validator.class);
            v = (Validator) ve.getValue(faceletContext);
        }
        if (v == null) {
            v = this.createValidator(faceletContext);
            if (ve != null) {
                ve.setValue(faceletContext, v);
            }
        }
        if (v == null) {
            throw new TagException(_delegate.getTag(), "No Validator was created");
        }
        _delegate.setAttributes(faceletContext, v);
        if (shouldBeanBeforeJsfValidationEnabled(context)) {
            parent.getAttributes().put(BEAN_BEFORE_JSF_PROPERTY, Boolean.TRUE);
        }
        evh.addValidator(v);
    }
}
Also used : TagException(jakarta.faces.view.facelets.TagException) ValueExpression(jakarta.el.ValueExpression) FaceletContext(jakarta.faces.view.facelets.FaceletContext) ArrayList(java.util.ArrayList) List(java.util.List) Validator(jakarta.faces.validator.Validator) EditableValueHolder(jakarta.faces.component.EditableValueHolder)

Example 3 with FaceletContext

use of jakarta.faces.view.facelets.FaceletContext in project myfaces by apache.

the class ActionListenerHandler method applyAttachedObject.

@Override
public void applyAttachedObject(FacesContext context, UIComponent parent) {
    // Retrieve the current FaceletContext from FacesContext object
    FaceletContext faceletContext = (FaceletContext) context.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
    ActionSource as = (ActionSource) parent;
    ValueExpression b = null;
    if (this.binding != null) {
        b = this.binding.getValueExpression(faceletContext, ActionListener.class);
    }
    ActionListener listener = new LazyActionListener(this.listenerType, b);
    as.addActionListener(listener);
}
Also used : ActionListener(jakarta.faces.event.ActionListener) ValueExpression(jakarta.el.ValueExpression) FaceletContext(jakarta.faces.view.facelets.FaceletContext) ActionSource(jakarta.faces.component.ActionSource)

Example 4 with FaceletContext

use of jakarta.faces.view.facelets.FaceletContext in project myfaces by apache.

the class ValueChangeListenerHandler method applyAttachedObject.

@Override
public void applyAttachedObject(FacesContext context, UIComponent parent) {
    // Retrieve the current FaceletContext from FacesContext object
    FaceletContext faceletContext = (FaceletContext) context.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
    EditableValueHolder evh = (EditableValueHolder) parent;
    ValueExpression b = null;
    if (this.binding != null) {
        b = this.binding.getValueExpression(faceletContext, ValueChangeListener.class);
    }
    ValueChangeListener listener = new LazyValueChangeListener(this.listenerType, b);
    evh.addValueChangeListener(listener);
}
Also used : ValueChangeListener(jakarta.faces.event.ValueChangeListener) ValueExpression(jakarta.el.ValueExpression) FaceletContext(jakarta.faces.view.facelets.FaceletContext) EditableValueHolder(jakarta.faces.component.EditableValueHolder)

Example 5 with FaceletContext

use of jakarta.faces.view.facelets.FaceletContext in project myfaces by apache.

the class FaceletViewDeclarationLanguage method getComponentMetadata.

/**
 * retargetMethodExpressions(FacesContext, UIComponent) has some clues about the behavior of this method
 *
 * {@inheritDoc}
 */
@Override
public BeanInfo getComponentMetadata(FacesContext context, Resource componentResource) {
    BeanInfo beanInfo = null;
    Assert.notNull(context, "context");
    try {
        Facelet compositeComponentFacelet;
        FaceletFactory.setInstance(faceletFactory);
        try {
            compositeComponentFacelet = faceletFactory.getCompositeComponentMetadataFacelet(componentResource.getURL());
        } finally {
            FaceletFactory.setInstance(null);
        }
        // context.getAttributes().put(BUILDING_COMPOSITE_COMPONENT_METADATA, Boolean.TRUE);
        // Create a temporal tree where all components will be put, but we are only
        // interested in metadata.
        UINamingContainer compositeComponentBase = (UINamingContainer) context.getApplication().createComponent(context, UINamingContainer.COMPONENT_TYPE, null);
        // Fill the component resource key, because this information should be available
        // on metadata to recognize which is the component used as composite component base.
        // Since this method is called from Application.createComponent(FacesContext,Resource),
        // and in that specific method this key is updated, this is the best option we
        // have for recognize it (also this key is used by UIComponent.isCompositeComponent)
        compositeComponentBase.getAttributes().put(Resource.COMPONENT_RESOURCE_KEY, componentResource);
        // According to UserTagHandler, in this point we need to wrap the facelet
        // VariableMapper, so local changes are applied on "page context", but
        // data is retrieved from full context
        FaceletContext faceletContext = (FaceletContext) context.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
        VariableMapper orig = faceletContext.getVariableMapper();
        try {
            faceletContext.setVariableMapper(new VariableMapperWrapper(orig));
            compositeComponentBase.pushComponentToEL(context, compositeComponentBase);
            compositeComponentFacelet.apply(context, compositeComponentBase);
            compositeComponentBase.popComponentFromEL(context);
        } finally {
            faceletContext.setVariableMapper(orig);
        }
        beanInfo = (BeanInfo) compositeComponentBase.getAttributes().get(UIComponent.BEANINFO_KEY);
    } catch (IOException e) {
        throw new FacesException(e);
    }
    return beanInfo;
}
Also used : Facelet(jakarta.faces.view.facelets.Facelet) VariableMapper(jakarta.el.VariableMapper) BeanInfo(java.beans.BeanInfo) VariableMapperWrapper(org.apache.myfaces.view.facelets.el.VariableMapperWrapper) FaceletContext(jakarta.faces.view.facelets.FaceletContext) UINamingContainer(jakarta.faces.component.UINamingContainer) IOException(java.io.IOException) FacesException(jakarta.faces.FacesException)

Aggregations

FaceletContext (jakarta.faces.view.facelets.FaceletContext)31 ValueExpression (jakarta.el.ValueExpression)13 FacesContext (jakarta.faces.context.FacesContext)10 UIComponent (jakarta.faces.component.UIComponent)7 TagAttribute (jakarta.faces.view.facelets.TagAttribute)7 TagException (jakarta.faces.view.facelets.TagException)7 ActionSource (jakarta.faces.component.ActionSource)6 ClientBehaviorHolder (jakarta.faces.component.behavior.ClientBehaviorHolder)5 EditableValueHolder (jakarta.faces.component.EditableValueHolder)4 ActionListener (jakarta.faces.event.ActionListener)4 ClientBehavior (jakarta.faces.component.behavior.ClientBehavior)3 TagAttributeException (jakarta.faces.view.facelets.TagAttributeException)3 AbstractFaceletContext (org.apache.myfaces.view.facelets.AbstractFaceletContext)3 FaceletCompositionContext (org.apache.myfaces.view.facelets.FaceletCompositionContext)3 MethodExpression (jakarta.el.MethodExpression)2 VariableMapper (jakarta.el.VariableMapper)2 FacesException (jakarta.faces.FacesException)2 UniqueIdVendor (jakarta.faces.component.UniqueIdVendor)2 ValueHolder (jakarta.faces.component.ValueHolder)2 AjaxBehavior (jakarta.faces.component.behavior.AjaxBehavior)2