Search in sources :

Example 1 with ClientBehaviorBase

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

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

the class RendererBase method getCommandMap.

private static CommandMap getCommandMap(final FacesContext facesContext, final ClientBehaviorContext clientBehaviorContext, final ClientBehavior clientBehavior) {
    if (clientBehavior instanceof ClientBehaviorBase) {
        String type = ((ClientBehaviorBase) clientBehavior).getRendererType();
        // this is to use a different renderer for Tobago components and other components.
        if (type.equals(AjaxBehavior.BEHAVIOR_ID)) {
            type = "org.apache.myfaces.tobago.behavior.Ajax";
        }
        final ClientBehaviorRenderer renderer = facesContext.getRenderKit().getClientBehaviorRenderer(type);
        final String dummy = renderer.getScript(clientBehaviorContext, clientBehavior);
        if (dummy != null) {
            return CommandMap.restoreCommandMap(facesContext);
        }
    } else {
        LOG.warn("Ignoring: '{}'", clientBehavior);
    }
    return null;
}
Also used : TobagoClientBehaviorRenderer(org.apache.myfaces.tobago.internal.renderkit.renderer.TobagoClientBehaviorRenderer) ClientBehaviorRenderer(jakarta.faces.render.ClientBehaviorRenderer) ClientBehaviorBase(jakarta.faces.component.behavior.ClientBehaviorBase)

Example 3 with ClientBehaviorBase

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

the class ClientBehaviorRendererUtilsTest method testBuildBehaviorChain.

public void testBuildBehaviorChain() {
    Map<String, List<ClientBehavior>> behaviors = new HashMap<String, List<ClientBehavior>>();
    // Map<String, String> params = new HashMap<String, String>();
    Collection<ClientBehaviorContext.Parameter> params = new ArrayList<ClientBehaviorContext.Parameter>();
    UIComponent component = new HtmlInputText();
    Assert.assertEquals("", ClientBehaviorRendererUtils.buildBehaviorChain(facesContext, component, component.getClientId(facesContext), ClientBehaviorEvents.CLICK, params, ClientBehaviorEvents.ACTION, params, behaviors, null, null));
    Assert.assertEquals("return faces.util.chain(document.getElementById('j_id__v_0'), event,'huhn', 'suppe');", ClientBehaviorRendererUtils.buildBehaviorChain(facesContext, component, component.getClientId(facesContext), ClientBehaviorEvents.CLICK, params, ClientBehaviorEvents.ACTION, params, behaviors, "huhn", "suppe"));
    ClientBehavior submittingBehavior = new ClientBehaviorBase() {

        @Override
        public String getScript(ClientBehaviorContext behaviorContext) {
            return "script()";
        }

        @Override
        public Set<ClientBehaviorHint> getHints() {
            return EnumSet.allOf(ClientBehaviorHint.class);
        }
    };
    behaviors.put(ClientBehaviorEvents.CLICK, Arrays.asList(submittingBehavior));
    Assert.assertEquals("faces.util.chain(document.getElementById('j_id__v_0'), event,'huhn', 'script()', 'suppe'); return false;", ClientBehaviorRendererUtils.buildBehaviorChain(facesContext, component, component.getClientId(facesContext), ClientBehaviorEvents.CLICK, params, ClientBehaviorEvents.ACTION, params, behaviors, "huhn", "suppe"));
}
Also used : UIComponent(jakarta.faces.component.UIComponent) HtmlInputText(jakarta.faces.component.html.HtmlInputText) ClientBehaviorContext(jakarta.faces.component.behavior.ClientBehaviorContext) ClientBehaviorBase(jakarta.faces.component.behavior.ClientBehaviorBase) ClientBehaviorHint(jakarta.faces.component.behavior.ClientBehaviorHint) ClientBehavior(jakarta.faces.component.behavior.ClientBehavior)

Example 4 with ClientBehaviorBase

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

the class ClientBehaviorRendererUtilsTest method testBuildBehaviorChain2.

public void testBuildBehaviorChain2() {
    Map<String, List<ClientBehavior>> behaviors = new HashMap<String, List<ClientBehavior>>();
    // Map<String, String> params = new HashMap<String, String>();
    Collection<ClientBehaviorContext.Parameter> params = new ArrayList<ClientBehaviorContext.Parameter>();
    UIComponent component = new HtmlInputText();
    Assert.assertEquals("", ClientBehaviorRendererUtils.buildBehaviorChain(facesContext, component, ClientBehaviorEvents.CLICK, params, ClientBehaviorEvents.ACTION, params, behaviors, null, null));
    Assert.assertEquals("return faces.util.chain(this, event,'huhn', 'suppe');", ClientBehaviorRendererUtils.buildBehaviorChain(facesContext, component, ClientBehaviorEvents.CLICK, params, ClientBehaviorEvents.ACTION, params, behaviors, "huhn", "suppe"));
    ClientBehavior submittingBehavior = new ClientBehaviorBase() {

        @Override
        public String getScript(ClientBehaviorContext behaviorContext) {
            return "script()";
        }

        @Override
        public Set<ClientBehaviorHint> getHints() {
            return EnumSet.allOf(ClientBehaviorHint.class);
        }
    };
    behaviors.put(ClientBehaviorEvents.CLICK, Arrays.asList(submittingBehavior));
    Assert.assertEquals("faces.util.chain(this, event,'huhn', 'script()', 'suppe'); return false;", ClientBehaviorRendererUtils.buildBehaviorChain(facesContext, component, ClientBehaviorEvents.CLICK, params, ClientBehaviorEvents.ACTION, params, behaviors, "huhn", "suppe"));
}
Also used : UIComponent(jakarta.faces.component.UIComponent) HtmlInputText(jakarta.faces.component.html.HtmlInputText) ClientBehaviorContext(jakarta.faces.component.behavior.ClientBehaviorContext) ClientBehaviorBase(jakarta.faces.component.behavior.ClientBehaviorBase) ClientBehaviorHint(jakarta.faces.component.behavior.ClientBehaviorHint) ClientBehavior(jakarta.faces.component.behavior.ClientBehavior)

Example 5 with ClientBehaviorBase

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

the class RenderUtils method getCommandMap.

/**
 * @deprecated since 5.0.0
 */
@Deprecated
private static CommandMap getCommandMap(final FacesContext facesContext, final ClientBehaviorContext clientBehaviorContext, final ClientBehavior clientBehavior) {
    if (clientBehavior instanceof ClientBehaviorBase) {
        String type = ((ClientBehaviorBase) clientBehavior).getRendererType();
        // this is to use a different renderer for Tobago components and other components.
        if (type.equals(AjaxBehavior.BEHAVIOR_ID)) {
            type = "org.apache.myfaces.tobago.behavior.Ajax";
        }
        final ClientBehaviorRenderer renderer = facesContext.getRenderKit().getClientBehaviorRenderer(type);
        final String dummy = renderer.getScript(clientBehaviorContext, clientBehavior);
        if (dummy != null) {
            return CommandMap.restoreCommandMap(facesContext);
        }
    } else {
        LOG.warn("Ignoring: '{}'", clientBehavior);
    }
    return null;
}
Also used : TobagoClientBehaviorRenderer(org.apache.myfaces.tobago.internal.renderkit.renderer.TobagoClientBehaviorRenderer) ClientBehaviorRenderer(jakarta.faces.render.ClientBehaviorRenderer) ClientBehaviorBase(jakarta.faces.component.behavior.ClientBehaviorBase)

Aggregations

ClientBehaviorBase (jakarta.faces.component.behavior.ClientBehaviorBase)6 ClientBehaviorRenderer (jakarta.faces.render.ClientBehaviorRenderer)4 ClientBehavior (jakarta.faces.component.behavior.ClientBehavior)3 UIComponent (jakarta.faces.component.UIComponent)2 ClientBehaviorContext (jakarta.faces.component.behavior.ClientBehaviorContext)2 ClientBehaviorHint (jakarta.faces.component.behavior.ClientBehaviorHint)2 HtmlInputText (jakarta.faces.component.html.HtmlInputText)2 TobagoClientBehaviorRenderer (org.apache.myfaces.tobago.internal.renderkit.renderer.TobagoClientBehaviorRenderer)2 ELException (jakarta.el.ELException)1 FacesException (jakarta.faces.FacesException)1 FacesWrapper (jakarta.faces.FacesWrapper)1 Behavior (jakarta.faces.component.behavior.Behavior)1 FacesBehavior (jakarta.faces.component.behavior.FacesBehavior)1 FacesContext (jakarta.faces.context.FacesContext)1 RenderKit (jakarta.faces.render.RenderKit)1 MissingResourceException (java.util.MissingResourceException)1 NamingException (javax.naming.NamingException)1 FacesBehaviorCDIWrapper (org.apache.myfaces.cdi.wrapper.FacesBehaviorCDIWrapper)1 FacesClientBehaviorCDIWrapper (org.apache.myfaces.cdi.wrapper.FacesClientBehaviorCDIWrapper)1