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);
}
}
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;
}
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"));
}
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"));
}
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;
}
Aggregations