Search in sources :

Example 1 with AjaxBehaviorListener

use of jakarta.faces.event.AjaxBehaviorListener 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)

Aggregations

MethodExpression (jakarta.el.MethodExpression)1 UIComponent (jakarta.faces.component.UIComponent)1 AjaxBehavior (jakarta.faces.component.behavior.AjaxBehavior)1 ClientBehavior (jakarta.faces.component.behavior.ClientBehavior)1 ClientBehaviorHolder (jakarta.faces.component.behavior.ClientBehaviorHolder)1 AjaxBehaviorListener (jakarta.faces.event.AjaxBehaviorListener)1 FaceletContext (jakarta.faces.view.facelets.FaceletContext)1 TagAttributeException (jakarta.faces.view.facelets.TagAttributeException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AbstractFaceletContext (org.apache.myfaces.view.facelets.AbstractFaceletContext)1 ClientBehaviorRedirectEventComponentWrapper (org.apache.myfaces.view.facelets.tag.composite.ClientBehaviorRedirectEventComponentWrapper)1