Search in sources :

Example 1 with Behavior

use of jakarta.faces.component.behavior.Behavior 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 Behavior

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

the class UIComponentBase method broadcast.

/**
 * Invoke any listeners attached to this object which are listening for an event whose type matches the specified
 * event's runtime type.
 * <p>
 * This method does not propagate the event up to parent components, ie listeners attached to parent components
 * don't automatically get called.
 * <p>
 * If any of the listeners throws AbortProcessingException then that exception will prevent any further listener
 * callbacks from occurring, and the exception propagates out of this method without alteration.
 * <p>
 * ActionEvent events are typically queued by the renderer associated with this component in its decode method;
 * ValueChangeEvent events by the component's validate method. In either case the event's source property references
 * a component. At some later time the UIViewRoot component iterates over its queued events and invokes the
 * broadcast method on each event's source object.
 *
 * @param event
 *            must not be null.
 */
@Override
public void broadcast(FacesEvent event) throws AbortProcessingException {
    Assert.notNull(event, "event");
    if (event instanceof BehaviorEvent && event.getComponent() == this) {
        Behavior behavior = ((BehaviorEvent) event).getBehavior();
        behavior.broadcast((BehaviorEvent) event);
    }
    if (_facesListeners == null) {
        return;
    }
    // perf: _facesListeners is RandomAccess instance (jakarta.faces.component._DeltaList)
    for (int i = 0, size = _facesListeners.size(); i < size; i++) {
        FacesListener facesListener = _facesListeners.get(i);
        if (event.isAppropriateListener(facesListener)) {
            event.processListener(facesListener);
        }
    }
}
Also used : BehaviorEvent(jakarta.faces.event.BehaviorEvent) FacesListener(jakarta.faces.event.FacesListener) ClientBehavior(jakarta.faces.component.behavior.ClientBehavior) Behavior(jakarta.faces.component.behavior.Behavior)

Example 3 with Behavior

use of jakarta.faces.component.behavior.Behavior in project mojarra by eclipse-ee4j.

the class UIComponentBase method broadcast.

// -------------------------------------------- Lifecycle Processing Methods
/**
 * @throws AbortProcessingException {@inheritDoc}
 * @throws IllegalStateException {@inheritDoc}
 * @throws NullPointerException {@inheritDoc}
 */
@Override
public void broadcast(FacesEvent event) throws AbortProcessingException {
    if (event == null) {
        throw new NullPointerException();
    }
    if (event instanceof BehaviorEvent) {
        BehaviorEvent behaviorEvent = (BehaviorEvent) event;
        Behavior behavior = behaviorEvent.getBehavior();
        behavior.broadcast(behaviorEvent);
    }
    if (listeners == null) {
        return;
    }
    for (FacesListener listener : listeners.asArray(FacesListener.class)) {
        if (event.isAppropriateListener(listener)) {
            event.processListener(listener);
        }
    }
}
Also used : BehaviorEvent(jakarta.faces.event.BehaviorEvent) FacesListener(jakarta.faces.event.FacesListener) ClientBehavior(jakarta.faces.component.behavior.ClientBehavior) Behavior(jakarta.faces.component.behavior.Behavior)

Example 4 with Behavior

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

the class BehaviorTagHandlerDelegate method applyAttachedObject.

/**
 * Create a ClientBehavior and attach it to the component
 */
@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);
    ValueExpression ve = null;
    Behavior behavior = null;
    if (_delegate.getBinding() != null) {
        ve = _delegate.getBinding().getValueExpression(faceletContext, Behavior.class);
        behavior = (Behavior) ve.getValue(faceletContext);
    }
    if (behavior == null) {
        behavior = this.createBehavior(faceletContext);
        if (ve != null) {
            ve.setValue(faceletContext, behavior);
        }
    }
    if (behavior == null) {
        throw new TagException(_delegate.getTag(), "No Validator was created");
    }
    _delegate.setAttributes(faceletContext, behavior);
    if (behavior instanceof ClientBehavior) {
        // cast to a ClientBehaviorHolder
        ClientBehaviorHolder cvh = (ClientBehaviorHolder) parent;
        // TODO: check if the behavior could be applied to the current parent
        // For run tests it is not necessary, so we let this one pending.
        // It is necessary to obtain a event name for add it, so we have to
        // look first to the defined event name, otherwise take the default from
        // the holder
        String eventName = getEventName();
        if (eventName == null) {
            eventName = cvh.getDefaultEventName();
        }
        if (eventName == null) {
            throw new TagAttributeException(_delegate.getEvent(), "eventName could not be defined for client behavior " + behavior.toString());
        } else if (!cvh.getEventNames().contains(eventName)) {
            throw new TagAttributeException(_delegate.getEvent(), "eventName " + eventName + " not found on component instance");
        } else {
            cvh.addClientBehavior(eventName, (ClientBehavior) behavior);
        }
        AjaxHandler.registerFacesJsResource(faceletContext, parent);
    }
}
Also used : TagAttributeException(jakarta.faces.view.facelets.TagAttributeException) TagException(jakarta.faces.view.facelets.TagException) ValueExpression(jakarta.el.ValueExpression) FaceletContext(jakarta.faces.view.facelets.FaceletContext) ClientBehavior(jakarta.faces.component.behavior.ClientBehavior) Behavior(jakarta.faces.component.behavior.Behavior) ClientBehaviorHolder(jakarta.faces.component.behavior.ClientBehaviorHolder) ClientBehavior(jakarta.faces.component.behavior.ClientBehavior)

Example 5 with Behavior

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

the class MockApplication20 method createBehavior.

public Behavior createBehavior(String behaviorId) throws FacesException {
    checkNull(behaviorId, "behaviorId");
    checkEmpty(behaviorId, "behaviorId");
    final Class<?> behaviorClass = this._behaviorClassMap.get(behaviorId);
    if (behaviorClass == null) {
        throw new FacesException("Could not find any registered behavior-class for behaviorId : " + behaviorId);
    }
    try {
        final Behavior behavior = (Behavior) behaviorClass.newInstance();
        _handleAttachedResourceDependencyAnnotations(FacesContext.getCurrentInstance(), behaviorClass);
        return behavior;
    } catch (Exception e) {
        throw new FacesException("Could not instantiate behavior: " + behaviorClass, e);
    }
}
Also used : Behavior(jakarta.faces.component.behavior.Behavior) FacesException(jakarta.faces.FacesException) AbortProcessingException(jakarta.faces.event.AbortProcessingException) FacesException(jakarta.faces.FacesException)

Aggregations

Behavior (jakarta.faces.component.behavior.Behavior)9 ClientBehavior (jakarta.faces.component.behavior.ClientBehavior)5 ValueExpression (jakarta.el.ValueExpression)2 FacesException (jakarta.faces.FacesException)2 ClientBehaviorHolder (jakarta.faces.component.behavior.ClientBehaviorHolder)2 FacesBehavior (jakarta.faces.component.behavior.FacesBehavior)2 BehaviorEvent (jakarta.faces.event.BehaviorEvent)2 FacesListener (jakarta.faces.event.FacesListener)2 FaceletContext (jakarta.faces.view.facelets.FaceletContext)2 TagException (jakarta.faces.view.facelets.TagException)2 ELException (jakarta.el.ELException)1 FacesWrapper (jakarta.faces.FacesWrapper)1 ClientBehaviorBase (jakarta.faces.component.behavior.ClientBehaviorBase)1 FacesContext (jakarta.faces.context.FacesContext)1 AbortProcessingException (jakarta.faces.event.AbortProcessingException)1 ClientBehaviorRenderer (jakarta.faces.render.ClientBehaviorRenderer)1 TagAttributeException (jakarta.faces.view.facelets.TagAttributeException)1 PrintWriter (java.io.PrintWriter)1 MissingResourceException (java.util.MissingResourceException)1 NamingException (javax.naming.NamingException)1