use of jakarta.faces.view.AttachedObjectTarget in project myfaces by apache.
the class FaceletViewDeclarationLanguage method retargetAttachedObjects.
/**
* In short words, this method take care of "target" an "attached object".
* <ul>
* <li>The "attached object" is instantiated by a tag handler.</li>
* <li>The "target" is an object used as "marker", that exposes a List<UIComponent></li>
* </ul>
* This method should be called from some composite component tag handler, after
* all children of composite component has been applied.
*/
@Override
@SuppressWarnings("unchecked")
public void retargetAttachedObjects(FacesContext context, UIComponent topLevelComponent, List<AttachedObjectHandler> handlerList) {
Assert.notNull(context, "context");
Assert.notNull(topLevelComponent, "topLevelComponent");
Assert.notNull(handlerList, "handlerList");
BeanInfo compositeComponentMetadata = (BeanInfo) topLevelComponent.getAttributes().get(UIComponent.BEANINFO_KEY);
if (compositeComponentMetadata == null) {
log.severe("Composite component metadata not found for: " + topLevelComponent.getClientId(context));
return;
}
BeanDescriptor compositeComponentDescriptor = compositeComponentMetadata.getBeanDescriptor();
List<AttachedObjectTarget> targetList = (List<AttachedObjectTarget>) compositeComponentDescriptor.getValue(AttachedObjectTarget.ATTACHED_OBJECT_TARGETS_KEY);
if (targetList == null || targetList.isEmpty()) {
return;
}
for (int i = 0, size = handlerList.size(); i < size; i++) {
AttachedObjectHandler currentHandler = handlerList.get(i);
// In the spec javadoc this variable is referred as forAttributeValue, but
// note it is also called curTargetName
String forValue = currentHandler.getFor();
// and ClientBehaviorHandler.apply
for (int k = 0, targetsSize = targetList.size(); k < targetsSize; k++) {
AttachedObjectTarget currentTarget = targetList.get(k);
FaceletCompositionContext mctx = FaceletCompositionContext.getCurrentInstance();
if ((forValue != null && forValue.equals(currentTarget.getName())) && ((currentTarget instanceof ActionSource2AttachedObjectTarget && currentHandler instanceof ActionSource2AttachedObjectHandler) || (currentTarget instanceof EditableValueHolderAttachedObjectTarget && currentHandler instanceof EditableValueHolderAttachedObjectHandler) || (currentTarget instanceof ValueHolderAttachedObjectTarget && currentHandler instanceof ValueHolderAttachedObjectHandler))) {
// perf: getTargets return ArrayList - see getTargets implementations
List<UIComponent> targets = currentTarget.getTargets(topLevelComponent);
for (int l = 0, targetsCount = targets.size(); l < targetsCount; l++) {
UIComponent component = targets.get(l);
// its tag handler is applied.
if (UIComponent.isCompositeComponent(component)) {
// How we obtain the list of AttachedObjectHandler for
// the current composite component? It should be a component
// attribute or retrieved by a key inside component.getAttributes
// map. Since api does not specify any attribute, we suppose
// this is an implementation detail and it should be retrieved
// from component attribute map.
// But this is only the point of the iceberg, because we should
// define how we register attached object handlers in this list.
// ANS: see CompositeComponentResourceTagHandler.
// The current handler should be added to the list, to be chained.
// Note that the inner component should have a target with the same name
// as "for" attribute
mctx.addAttachedObjectHandler(component, currentHandler);
List<AttachedObjectHandler> handlers = mctx.getAttachedObjectHandlers(component);
retargetAttachedObjects(context, component, handlers);
handlers.remove(currentHandler);
} else {
currentHandler.applyAttachedObject(context, component);
}
if (mctx.isUsingPSSOnThisView() && mctx.isMarkInitialState()) {
component.markInitialState();
}
}
} else if ((currentTarget instanceof BehaviorHolderAttachedObjectTarget && currentHandler instanceof BehaviorHolderAttachedObjectHandler)) {
String eventName = ((BehaviorHolderAttachedObjectHandler) currentHandler).getEventName();
boolean isDefaultEvent = ((BehaviorHolderAttachedObjectTarget) currentTarget).isDefaultEvent();
if ((eventName != null && eventName.equals(currentTarget.getName())) || (eventName == null && isDefaultEvent)) {
List<UIComponent> targets = currentTarget.getTargets(topLevelComponent);
for (int j = 0, targetssize = targets.size(); j < targetssize; j++) {
UIComponent component = targets.get(j);
// its tag handler is applied.
if (UIComponent.isCompositeComponent(component)) {
if (currentTarget instanceof ClientBehaviorAttachedObjectTarget) {
mctx.addAttachedObjectHandler(component, new ClientBehaviorRedirectBehaviorAttachedObjectHandlerWrapper((BehaviorHolderAttachedObjectHandler) currentHandler, ((ClientBehaviorAttachedObjectTarget) currentTarget).getEvent()));
} else {
mctx.addAttachedObjectHandler(component, currentHandler);
}
List<AttachedObjectHandler> handlers = mctx.getAttachedObjectHandlers(component);
retargetAttachedObjects(context, component, handlers);
handlers.remove(currentHandler);
} else {
if (currentHandler instanceof ClientBehaviorRedirectBehaviorAttachedObjectHandlerWrapper) {
ClientBehaviorRedirectBehaviorAttachedObjectHandlerWrapper wrapper = (ClientBehaviorRedirectBehaviorAttachedObjectHandlerWrapper) currentHandler;
currentHandler.applyAttachedObject(context, new ClientBehaviorRedirectEventComponentWrapper(topLevelComponent, component, wrapper.getWrappedEventName(), eventName));
} else {
currentHandler.applyAttachedObject(context, component);
}
}
if (mctx.isUsingPSSOnThisView() && mctx.isMarkInitialState()) {
component.markInitialState();
}
}
}
}
}
}
}
use of jakarta.faces.view.AttachedObjectTarget in project mojarra by eclipse-ee4j.
the class BehaviorTagHandlerDelegateImpl method apply.
@Override
public void apply(FaceletContext ctx, UIComponent parent) throws IOException {
// only process if it's been created
if (parent == null || !(parent.getParent() == null)) {
return;
}
ComponentSupport.copyPassthroughAttributes(ctx, parent, owner.getTag());
if (UIComponent.isCompositeComponent(parent)) {
// Check composite component event name:
BeanInfo componentBeanInfo = (BeanInfo) parent.getAttributes().get(UIComponent.BEANINFO_KEY);
if (null == componentBeanInfo) {
throw new TagException(owner.getTag(), "Error: enclosing composite component does not have BeanInfo attribute");
}
BeanDescriptor componentDescriptor = componentBeanInfo.getBeanDescriptor();
if (null == componentDescriptor) {
throw new TagException(owner.getTag(), "Error: enclosing composite component BeanInfo does not have BeanDescriptor");
}
List<AttachedObjectTarget> targetList = (List<AttachedObjectTarget>) componentDescriptor.getValue(AttachedObjectTarget.ATTACHED_OBJECT_TARGETS_KEY);
if (null == targetList) {
throw new TagException(owner.getTag(), "Error: enclosing composite component does not support behavior events");
}
String eventName = owner.getEventName();
boolean supportedEvent = false;
for (AttachedObjectTarget target : targetList) {
if (target instanceof BehaviorHolderAttachedObjectTarget) {
BehaviorHolderAttachedObjectTarget behaviorTarget = (BehaviorHolderAttachedObjectTarget) target;
if (null != eventName && eventName.equals(behaviorTarget.getName()) || null == eventName && behaviorTarget.isDefaultEvent()) {
supportedEvent = true;
break;
}
}
}
if (supportedEvent) {
CompositeComponentTagHandler.getAttachedObjectHandlers(parent).add(owner);
} else {
throw new TagException(owner.getTag(), "Error: enclosing composite component does not support event " + eventName);
}
} else if (parent instanceof ClientBehaviorHolder) {
owner.applyAttachedObject(ctx.getFacesContext(), parent);
} else {
throw new TagException(owner.getTag(), "Parent not an instance of ClientBehaviorHolder: " + parent);
}
}
use of jakarta.faces.view.AttachedObjectTarget in project mojarra by eclipse-ee4j.
the class InterfaceHandler method imbueComponentWithMetadata.
@SuppressWarnings({ "unchecked" })
private void imbueComponentWithMetadata(FaceletContext ctx, UIComponent parent) {
// only process if it's been created
if (null == parent || null == (parent = parent.getParent()) || !ComponentHandler.isNew(parent)) {
return;
}
Map<String, Object> attrs = parent.getAttributes();
CompositeComponentBeanInfo componentBeanInfo = (CompositeComponentBeanInfo) attrs.get(UIComponent.BEANINFO_KEY);
if (componentBeanInfo == null) {
componentBeanInfo = new CompositeComponentBeanInfo();
attrs.put(UIComponent.BEANINFO_KEY, componentBeanInfo);
BeanDescriptor componentDescriptor = new BeanDescriptor(parent.getClass());
// PENDING(edburns): Make sure attributeNames() returns the right content
// per the javadocs for ViewDeclarationLanguage.getComponentMetadata()
componentBeanInfo.setBeanDescriptor(componentDescriptor);
for (TagAttribute tagAttribute : tag.getAttributes().getAll()) {
String attributeName = tagAttribute.getLocalName();
PropertyHandler handler = INTERFACE_HANDLERS.getHandler(ctx, attributeName);
if (handler != null) {
handler.apply(ctx, attributeName, componentDescriptor, tagAttribute);
}
}
List<AttachedObjectTarget> targetList = (List<AttachedObjectTarget>) componentDescriptor.getValue(AttachedObjectTarget.ATTACHED_OBJECT_TARGETS_KEY);
if (null == targetList) {
targetList = new ArrayList<>();
componentDescriptor.setValue(AttachedObjectTarget.ATTACHED_OBJECT_TARGETS_KEY, targetList);
}
Resource componentResource = (Resource) attrs.get(Resource.COMPONENT_RESOURCE_KEY);
if (null == componentResource) {
throw new NullPointerException("Unable to find Resource for composite component");
}
}
}
use of jakarta.faces.view.AttachedObjectTarget in project myfaces by apache.
the class AttachedObjectTargetHandler method apply.
@SuppressWarnings("unchecked")
@Override
public void apply(FaceletContext ctx, UIComponent parent) throws IOException {
UIComponent compositeBaseParent = FaceletCompositionContext.getCurrentInstance(ctx).getCompositeComponentFromStack();
CompositeComponentBeanInfo beanInfo = (CompositeComponentBeanInfo) compositeBaseParent.getAttributes().get(UIComponent.BEANINFO_KEY);
if (beanInfo == null) {
if (log.isLoggable(Level.SEVERE)) {
log.severe("Cannot find composite bean descriptor UIComponent.BEANINFO_KEY ");
}
return;
}
BeanDescriptor beanDescriptor = beanInfo.getBeanDescriptor();
// 1. Obtain the list mentioned as "targetList" on ViewDeclarationLanguage.retargetAttachedObjects
List<AttachedObjectTarget> targetList = (List<AttachedObjectTarget>) beanDescriptor.getValue(AttachedObjectTarget.ATTACHED_OBJECT_TARGETS_KEY);
if (targetList == null) {
// 2. If not found create it and set
targetList = new ArrayList<>(5);
beanDescriptor.setValue(AttachedObjectTarget.ATTACHED_OBJECT_TARGETS_KEY, targetList);
}
// 3. Create the instance of AttachedObjectTarget
if (isCacheable()) {
if (_target == null) {
_target = createAttachedObjectTarget(ctx);
}
targetList.add(_target);
} else {
AttachedObjectTarget target = createAttachedObjectTarget(ctx);
targetList.add(target);
}
this.nextHandler.apply(ctx, parent);
}
use of jakarta.faces.view.AttachedObjectTarget in project myfaces by apache.
the class ClientBehaviorHandler method apply.
@SuppressWarnings("unchecked")
@Override
public void apply(FaceletContext ctx, UIComponent parent) throws IOException {
UIComponent compositeBaseParent = FaceletCompositionContext.getCurrentInstance(ctx).getCompositeComponentFromStack();
CompositeComponentBeanInfo beanInfo = (CompositeComponentBeanInfo) compositeBaseParent.getAttributes().get(UIComponent.BEANINFO_KEY);
if (beanInfo == null) {
if (log.isLoggable(Level.SEVERE)) {
log.severe("Cannot find composite bean descriptor UIComponent.BEANINFO_KEY ");
}
return;
}
BeanDescriptor beanDescriptor = beanInfo.getBeanDescriptor();
// 1. Obtain the list mentioned as "targetList" on ViewDeclarationLanguage.retargetAttachedObjects
List<AttachedObjectTarget> targetList = (List<AttachedObjectTarget>) beanDescriptor.getValue(AttachedObjectTarget.ATTACHED_OBJECT_TARGETS_KEY);
if (targetList == null) {
// 2. If not found create it and set
targetList = new ArrayList<>(5);
beanDescriptor.setValue(AttachedObjectTarget.ATTACHED_OBJECT_TARGETS_KEY, targetList);
}
// 3. Create the instance of AttachedObjectTarget
if (isCacheable()) {
if (_target == null) {
_target = createAttachedObjectTarget(ctx);
}
targetList.add(_target);
} else {
ClientBehaviorAttachedObjectTargetImpl target = createAttachedObjectTarget(ctx);
targetList.add(target);
}
this.nextHandler.apply(ctx, parent);
}
Aggregations