Search in sources :

Example 1 with ClientBehavior

use of jakarta.faces.component.behavior.ClientBehavior in project myfaces by apache.

the class ApplicationImpl method createBehavior.

@Override
public Behavior createBehavior(String behaviorId) throws FacesException {
    Assert.notEmpty(behaviorId, "behaviorId");
    final Class<? extends Behavior> behaviorClass = getObjectFromClassMap(behaviorId, _behaviorClassMap);
    if (behaviorClass == null) {
        throw new FacesException("Could not find any registered behavior-class for behaviorId : " + behaviorId);
    }
    try {
        if (!_cdiManagedBehaviorMap.containsKey(behaviorClass)) {
            FacesBehavior annotation = behaviorClass.getAnnotation(FacesBehavior.class);
            if (annotation != null && annotation.managed()) {
                _cdiManagedBehaviorMap.put(behaviorClass, true);
            } else {
                _cdiManagedBehaviorMap.put(behaviorClass, false);
            }
        }
        boolean managed = _cdiManagedBehaviorMap.get(behaviorClass);
        Behavior behavior = null;
        if (managed) {
            if (ClientBehavior.class.isAssignableFrom(behaviorClass)) {
                behavior = new FacesClientBehaviorCDIWrapper((Class<ClientBehavior>) behaviorClass, behaviorId);
            } else {
                behavior = new FacesBehaviorCDIWrapper(behaviorClass, behaviorId);
            }
            Behavior innerBehavior = ((FacesWrapper<Behavior>) behavior).getWrapped();
            FacesContext facesContext = getFacesContext();
            _handleAttachedResourceDependencyAnnotations(facesContext, innerBehavior);
            if (innerBehavior instanceof ClientBehaviorBase) {
                ClientBehaviorBase clientBehavior = (ClientBehaviorBase) innerBehavior;
                String renderType = clientBehavior.getRendererType();
                if (renderType != null) {
                    ClientBehaviorRenderer cbr = facesContext.getRenderKit().getClientBehaviorRenderer(renderType);
                    _handleAttachedResourceDependencyAnnotations(facesContext, cbr);
                }
            }
        } else {
            behavior = behaviorClass.newInstance();
            FacesContext facesContext = getFacesContext();
            _handleAttachedResourceDependencyAnnotations(facesContext, behavior);
            if (behavior instanceof ClientBehaviorBase) {
                ClientBehaviorBase clientBehavior = (ClientBehaviorBase) behavior;
                String renderType = clientBehavior.getRendererType();
                if (renderType != null) {
                    ClientBehaviorRenderer cbr = facesContext.getRenderKit().getClientBehaviorRenderer(renderType);
                    _handleAttachedResourceDependencyAnnotations(facesContext, cbr);
                }
            }
        }
        return behavior;
    } catch (Exception e) {
        log.log(Level.SEVERE, "Could not instantiate behavior " + behaviorClass, e);
        throw new FacesException("Could not instantiate behavior: " + behaviorClass, e);
    }
}
Also used : FacesContext(jakarta.faces.context.FacesContext) FacesBehaviorCDIWrapper(org.apache.myfaces.cdi.wrapper.FacesBehaviorCDIWrapper) FacesWrapper(jakarta.faces.FacesWrapper) FacesException(jakarta.faces.FacesException) MissingResourceException(java.util.MissingResourceException) NamingException(javax.naming.NamingException) ELException(jakarta.el.ELException) FacesException(jakarta.faces.FacesException) ClientBehaviorRenderer(jakarta.faces.render.ClientBehaviorRenderer) ClientBehaviorBase(jakarta.faces.component.behavior.ClientBehaviorBase) FacesClientBehaviorCDIWrapper(org.apache.myfaces.cdi.wrapper.FacesClientBehaviorCDIWrapper) ClientBehavior(jakarta.faces.component.behavior.ClientBehavior) FacesBehavior(jakarta.faces.component.behavior.FacesBehavior) Behavior(jakarta.faces.component.behavior.Behavior) FacesBehavior(jakarta.faces.component.behavior.FacesBehavior)

Example 2 with ClientBehavior

use of jakarta.faces.component.behavior.ClientBehavior 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 3 with ClientBehavior

use of jakarta.faces.component.behavior.ClientBehavior in project myfaces by apache.

the class ClientBehaviorRendererUtils method decodeClientBehaviors.

public static void decodeClientBehaviors(FacesContext facesContext, UIComponent component) {
    if (!(component instanceof ClientBehaviorHolder)) {
        return;
    }
    ClientBehaviorHolder clientBehaviorHolder = (ClientBehaviorHolder) component;
    Map<String, List<ClientBehavior>> clientBehaviors = clientBehaviorHolder.getClientBehaviors();
    if (clientBehaviors != null && !clientBehaviors.isEmpty()) {
        Map<String, String> paramMap = facesContext.getExternalContext().getRequestParameterMap();
        String behaviorEventName = paramMap.get(ClientBehaviorContext.BEHAVIOR_EVENT_PARAM_NAME);
        if (behaviorEventName != null) {
            List<ClientBehavior> clientBehaviorList = clientBehaviors.get(behaviorEventName);
            if (clientBehaviorList != null && !clientBehaviorList.isEmpty()) {
                String sourceId = paramMap.get(ClientBehaviorContext.BEHAVIOR_SOURCE_PARAM_NAME);
                String componentClientId = component.getClientId(facesContext);
                String clientId = sourceId;
                if (sourceId.startsWith(componentClientId) && sourceId.length() > componentClientId.length()) {
                    String item = sourceId.substring(componentClientId.length() + 1);
                    // component, because that could conflict with the clientId naming convention.
                    if (StringUtils.isInteger(item)) {
                        clientId = componentClientId;
                    }
                }
                if (component.getClientId(facesContext).equals(clientId)) {
                    if (clientBehaviorList instanceof RandomAccess) {
                        for (int i = 0, size = clientBehaviorList.size(); i < size; i++) {
                            ClientBehavior clientBehavior = clientBehaviorList.get(i);
                            clientBehavior.decode(facesContext, component);
                        }
                    } else {
                        for (ClientBehavior clientBehavior : clientBehaviorList) {
                            clientBehavior.decode(facesContext, component);
                        }
                    }
                }
            }
        }
    }
}
Also used : RandomAccess(java.util.RandomAccess) ArrayList(java.util.ArrayList) List(java.util.List) ClientBehaviorHolder(jakarta.faces.component.behavior.ClientBehaviorHolder) ClientBehavior(jakarta.faces.component.behavior.ClientBehavior) ClientBehaviorHint(jakarta.faces.component.behavior.ClientBehaviorHint)

Example 4 with ClientBehavior

use of jakarta.faces.component.behavior.ClientBehavior in project myfaces-tobago by apache.

the class TobagoClientBehaviorRenderer method decode.

@Override
public void decode(final FacesContext facesContext, final UIComponent component, final ClientBehavior clientBehavior) {
    if (clientBehavior instanceof AjaxBehavior) {
        AjaxBehavior ajaxBehavior = (AjaxBehavior) clientBehavior;
        if (!component.isRendered() || ajaxBehavior.isDisabled()) {
            return;
        }
        dispatchBehaviorEvent(component, ajaxBehavior);
    } else if (clientBehavior instanceof EventBehavior) {
        final EventBehavior eventBehavior = (EventBehavior) clientBehavior;
        final AbstractUIEvent abstractUIEvent = RenderUtils.getAbstractUIEvent(component, eventBehavior);
        if (!component.isRendered() || abstractUIEvent == null || !abstractUIEvent.isRendered() || abstractUIEvent.isDisabled()) {
            return;
        }
        for (List<ClientBehavior> children : abstractUIEvent.getClientBehaviors().values()) {
            for (ClientBehavior child : children) {
                decode(facesContext, component, child);
            }
        }
        dispatchBehaviorEvent(component, eventBehavior);
    }
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) EventBehavior(org.apache.myfaces.tobago.internal.behavior.EventBehavior) AbstractUIEvent(org.apache.myfaces.tobago.internal.component.AbstractUIEvent) ClientBehavior(jakarta.faces.component.behavior.ClientBehavior) AjaxBehavior(jakarta.faces.component.behavior.AjaxBehavior)

Example 5 with ClientBehavior

use of jakarta.faces.component.behavior.ClientBehavior in project myfaces-tobago by apache.

the class RenderUtils method decodeClientBehaviors.

public static void decodeClientBehaviors(final FacesContext facesContext, final UIComponent component) {
    if (component instanceof ClientBehaviorHolder) {
        final ClientBehaviorHolder clientBehaviorHolder = (ClientBehaviorHolder) component;
        final Map<String, List<ClientBehavior>> clientBehaviors = clientBehaviorHolder.getClientBehaviors();
        if (clientBehaviors != null && !clientBehaviors.isEmpty()) {
            final Map<String, String> paramMap = facesContext.getExternalContext().getRequestParameterMap();
            final String behaviorEventName = paramMap.get("javax.faces.behavior.event");
            if (behaviorEventName != null) {
                final List<ClientBehavior> clientBehaviorList = clientBehaviors.get(behaviorEventName);
                if (clientBehaviorList != null && !clientBehaviorList.isEmpty()) {
                    final String clientId = paramMap.get("javax.faces.source");
                    if (component.getClientId(facesContext).equals(clientId)) {
                        for (final ClientBehavior clientBehavior : clientBehaviorList) {
                            clientBehavior.decode(facesContext, component);
                        }
                    }
                }
            }
        }
    }
}
Also used : List(java.util.List) ClientBehaviorHolder(jakarta.faces.component.behavior.ClientBehaviorHolder) ClientBehavior(jakarta.faces.component.behavior.ClientBehavior)

Aggregations

ClientBehavior (jakarta.faces.component.behavior.ClientBehavior)19 List (java.util.List)9 ClientBehaviorHolder (jakarta.faces.component.behavior.ClientBehaviorHolder)8 ClientBehaviorContext (jakarta.faces.component.behavior.ClientBehaviorContext)6 UIComponent (jakarta.faces.component.UIComponent)5 ArrayList (java.util.ArrayList)5 ClientBehaviorHint (jakarta.faces.component.behavior.ClientBehaviorHint)4 AjaxBehavior (jakarta.faces.component.behavior.AjaxBehavior)3 Behavior (jakarta.faces.component.behavior.Behavior)3 ClientBehaviorBase (jakarta.faces.component.behavior.ClientBehaviorBase)3 FaceletContext (jakarta.faces.view.facelets.FaceletContext)3 EventBehavior (org.apache.myfaces.tobago.internal.behavior.EventBehavior)3 AbstractUIEvent (org.apache.myfaces.tobago.internal.component.AbstractUIEvent)3 ValueExpression (jakarta.el.ValueExpression)2 HtmlInputText (jakarta.faces.component.html.HtmlInputText)2 FacesContext (jakarta.faces.context.FacesContext)2 ClientBehaviorRenderer (jakarta.faces.render.ClientBehaviorRenderer)2 TagAttributeException (jakarta.faces.view.facelets.TagAttributeException)2 TagException (jakarta.faces.view.facelets.TagException)2 Map (java.util.Map)2