// 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 = getEventName();
if (eventName == null)
{
eventName = cvh.getDefaultEventName();
if (eventName == null)
{
if (_wrapMode)
{
// No eventName defined, we can't apply this tag to this component, because
// there is no event defined to attach it, but since we are in wrap mode
// we have here the case that the component could not be the target
// 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)
{
// The current component does not implement the event selected,
// this ajax behavior cannot be applied, but we can't throw any exception
// since we are in wrap mode and we have here the case that the
// 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 most inner one has been applied, so according to
// jsf 2.0 spec section 10.4.1.1 it is not necessary to apply
// this one, because the inner one has precendece over
// 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));
}
cvh.addClientBehavior(eventName, ajaxBehavior);
}