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