use of jakarta.faces.view.facelets.FaceletContext 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.view.facelets.FaceletContext in project myfaces by apache.
the class ValidatorTagHandlerDelegate method applyAttachedObject.
@SuppressWarnings("unchecked")
@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);
// id from beeing registered on the component.
if (_delegate.isDisabled(faceletContext)) {
// tag is disabled --> add its validatorId to the parent's exclusion list
String validatorId = _delegate.getValidatorConfig().getValidatorId();
if (validatorId != null && !validatorId.isEmpty()) {
List<String> exclusionList = (List<String>) parent.getAttributes().get(VALIDATOR_ID_EXCLUSION_LIST_KEY);
if (exclusionList == null) {
exclusionList = new ArrayList<String>();
parent.getAttributes().put(VALIDATOR_ID_EXCLUSION_LIST_KEY, exclusionList);
}
exclusionList.add(validatorId);
}
} else {
// tag is enabled --> create the validator and attach it
// cast to a ValueHolder
EditableValueHolder evh = (EditableValueHolder) parent;
ValueExpression ve = null;
Validator v = null;
if (_delegate.getBinding() != null) {
ve = _delegate.getBinding().getValueExpression(faceletContext, Validator.class);
v = (Validator) ve.getValue(faceletContext);
}
if (v == null) {
v = this.createValidator(faceletContext);
if (ve != null) {
ve.setValue(faceletContext, v);
}
}
if (v == null) {
throw new TagException(_delegate.getTag(), "No Validator was created");
}
_delegate.setAttributes(faceletContext, v);
if (shouldBeanBeforeJsfValidationEnabled(context)) {
parent.getAttributes().put(BEAN_BEFORE_JSF_PROPERTY, Boolean.TRUE);
}
evh.addValidator(v);
}
}
use of jakarta.faces.view.facelets.FaceletContext in project myfaces by apache.
the class ActionListenerHandler method applyAttachedObject.
@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);
ActionSource as = (ActionSource) parent;
ValueExpression b = null;
if (this.binding != null) {
b = this.binding.getValueExpression(faceletContext, ActionListener.class);
}
ActionListener listener = new LazyActionListener(this.listenerType, b);
as.addActionListener(listener);
}
use of jakarta.faces.view.facelets.FaceletContext in project myfaces by apache.
the class ValueChangeListenerHandler method applyAttachedObject.
@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);
EditableValueHolder evh = (EditableValueHolder) parent;
ValueExpression b = null;
if (this.binding != null) {
b = this.binding.getValueExpression(faceletContext, ValueChangeListener.class);
}
ValueChangeListener listener = new LazyValueChangeListener(this.listenerType, b);
evh.addValueChangeListener(listener);
}
use of jakarta.faces.view.facelets.FaceletContext in project myfaces by apache.
the class FaceletViewDeclarationLanguage method getComponentMetadata.
/**
* retargetMethodExpressions(FacesContext, UIComponent) has some clues about the behavior of this method
*
* {@inheritDoc}
*/
@Override
public BeanInfo getComponentMetadata(FacesContext context, Resource componentResource) {
BeanInfo beanInfo = null;
Assert.notNull(context, "context");
try {
Facelet compositeComponentFacelet;
FaceletFactory.setInstance(faceletFactory);
try {
compositeComponentFacelet = faceletFactory.getCompositeComponentMetadataFacelet(componentResource.getURL());
} finally {
FaceletFactory.setInstance(null);
}
// context.getAttributes().put(BUILDING_COMPOSITE_COMPONENT_METADATA, Boolean.TRUE);
// Create a temporal tree where all components will be put, but we are only
// interested in metadata.
UINamingContainer compositeComponentBase = (UINamingContainer) context.getApplication().createComponent(context, UINamingContainer.COMPONENT_TYPE, null);
// Fill the component resource key, because this information should be available
// on metadata to recognize which is the component used as composite component base.
// Since this method is called from Application.createComponent(FacesContext,Resource),
// and in that specific method this key is updated, this is the best option we
// have for recognize it (also this key is used by UIComponent.isCompositeComponent)
compositeComponentBase.getAttributes().put(Resource.COMPONENT_RESOURCE_KEY, componentResource);
// According to UserTagHandler, in this point we need to wrap the facelet
// VariableMapper, so local changes are applied on "page context", but
// data is retrieved from full context
FaceletContext faceletContext = (FaceletContext) context.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
VariableMapper orig = faceletContext.getVariableMapper();
try {
faceletContext.setVariableMapper(new VariableMapperWrapper(orig));
compositeComponentBase.pushComponentToEL(context, compositeComponentBase);
compositeComponentFacelet.apply(context, compositeComponentBase);
compositeComponentBase.popComponentFromEL(context);
} finally {
faceletContext.setVariableMapper(orig);
}
beanInfo = (BeanInfo) compositeComponentBase.getAttributes().get(UIComponent.BEANINFO_KEY);
} catch (IOException e) {
throw new FacesException(e);
}
return beanInfo;
}
Aggregations