use of jakarta.faces.view.AttachedObjectHandler 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.AttachedObjectHandler in project faces by jakartaee.
the class TestServlet method vdlRetargetAttachedObjectsNPETest.
// End vdlRetargetMethodExpressionsNPETest
public void vdlRetargetAttachedObjectsNPETest(HttpServletRequest request, HttpServletResponse response) throws IOException {
PrintWriter pw = response.getWriter();
FacesContext context = getFacesContext();
ViewDeclarationLanguage vdl = this.getVDL(context, FACELETS_VIEWID);
UIComponent component = new UIInput();
List<AttachedObjectHandler> handlers = new ArrayList<AttachedObjectHandler>();
handlers.add(new TCKAttachedObjectHandler());
// FacesContext as 'null'
JSFTestUtil.checkForNPE(vdl, "retargetAttachedObjects", new Class<?>[] { FacesContext.class, UIComponent.class, List.class }, new Object[] { null, component, handlers }, pw);
// Component as 'null'
JSFTestUtil.checkForNPE(vdl, "retargetAttachedObjects", new Class<?>[] { FacesContext.class, UIComponent.class, List.class }, new Object[] { context, null, handlers }, pw);
// AttachedObjectHandler as 'null'
JSFTestUtil.checkForNPE(vdl, "retargetAttachedObjects", new Class<?>[] { FacesContext.class, UIComponent.class, List.class }, new Object[] { context, component, null }, pw);
}
use of jakarta.faces.view.AttachedObjectHandler in project myfaces by apache.
the class ResetValuesActionListenerHandler method applyAttachedObject.
public void applyAttachedObject(FacesContext context, UIComponent parent, boolean checkForParentCC) {
UIComponent topParentComponent = null;
// Retrieve the current FaceletContext from FacesContext object
FaceletContext faceletContext = (FaceletContext) context.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
if (checkForParentCC) {
FaceletCompositionContext mctx = FaceletCompositionContext.getCurrentInstance(faceletContext);
UIComponent parentComponent = parent;
while (parentComponent != null) {
if (UIComponent.isCompositeComponent(parentComponent)) {
List<AttachedObjectHandler> handlerList = mctx.getAttachedObjectHandlers(parentComponent);
if (handlerList != null && handlerList.contains(this)) {
// Found, but we need to go right to the top for it.
topParentComponent = parentComponent;
}
}
parentComponent = parentComponent.getParent();
}
}
ActionSource as = (ActionSource) parent;
ActionListener listener = null;
if (_render.isLiteral()) {
listener = new LiteralResetValuesActionListener(_clientIds, topParentComponent != null ? (Location) topParentComponent.getAttributes().get(CompositeComponentELUtils.LOCATION_KEY) : null);
} else {
listener = new ResetValuesActionListener(_render.getValueExpression(faceletContext, Object.class), topParentComponent != null ? (Location) topParentComponent.getAttributes().get(CompositeComponentELUtils.LOCATION_KEY) : null);
}
as.addActionListener(listener);
}
use of jakarta.faces.view.AttachedObjectHandler in project myfaces by apache.
the class CompositeComponentResourceTagHandler method applyFinalInitializationSteps.
protected void applyFinalInitializationSteps(FaceletContext ctx, FaceletCompositionContext mctx, UIComponent c) {
FacesContext facesContext = ctx.getFacesContext();
ViewDeclarationLanguage vdl = facesContext.getApplication().getViewHandler().getViewDeclarationLanguage(facesContext, facesContext.getViewRoot().getViewId());
List<AttachedObjectHandler> handlers = mctx.getAttachedObjectHandlers(c);
if (handlers != null) {
vdl.retargetAttachedObjects(facesContext, c, handlers);
// remove the list of handlers, as it is no longer necessary
mctx.removeAttachedObjectHandlers(c);
}
vdl.retargetMethodExpressions(facesContext, c);
if (FaceletCompositionContext.getCurrentInstance(ctx).isMarkInitialState()) {
// Call it only if we are using partial state saving
c.markInitialState();
// Call it to other components created not bound by a tag handler
c.getFacet(UIComponent.COMPOSITE_FACET_NAME).markInitialState();
}
}
use of jakarta.faces.view.AttachedObjectHandler in project faces by jakartaee.
the class TestServlet method vdlWrapperRetargetAttachedObjectsNPETest.
// End vdlWrapperRetargetMethodExpressionsNPETest
public void vdlWrapperRetargetAttachedObjectsNPETest(HttpServletRequest request, HttpServletResponse response) throws IOException {
PrintWriter out = response.getWriter();
StringBuffer buff = new StringBuffer(50);
FacesContext context = getFacesContext();
ViewDeclarationLanguage vdlWrapper = this.getVDL();
UIComponent component = new UIInput();
List<AttachedObjectHandler> handlers = new ArrayList<AttachedObjectHandler>();
handlers.add(new TCKAttachedObjectHandler());
String methName = "retargetAttachedObjects";
String expected = "NullPointerException";
// FacesContext as 'null'
try {
vdlWrapper.retargetAttachedObjects(null, component, handlers);
buff.append("No Exception thrown when calling '" + vdlWrapper.getClass().getName() + "." + methName + "(null, " + component.getClass().getSimpleName() + ", " + handlers.getClass().getSimpleName() + ")'" + JSFTestUtil.NL + "Expected: " + expected + JSFTestUtil.NL);
} catch (NullPointerException npe) {
// do nothing test passed
} catch (Exception e) {
buff.append("Unexpected Exception thrown for " + vdlWrapper.getClass().getName() + "." + methName + "(null, " + component.getClass().getSimpleName() + ", " + handlers.getClass().getSimpleName() + ")" + JSFTestUtil.NL + "Expected: " + expected + JSFTestUtil.NL + "Received: " + e.getClass().getName() + JSFTestUtil.NL);
}
// Component as 'null'
try {
vdlWrapper.retargetAttachedObjects(context, null, handlers);
buff.append("No Exception thrown when calling '" + vdlWrapper.getClass().getName() + "." + methName + "(" + context.getClass().getSimpleName() + ", null, " + handlers.getClass().getSimpleName() + ")'" + JSFTestUtil.NL + "Expected: " + expected + JSFTestUtil.NL);
} catch (NullPointerException npe) {
// do nothing test passed
} catch (Exception e) {
buff.append("Unexpected Exception thrown for " + vdlWrapper.getClass().getName() + "." + methName + "(" + context.getClass().getSimpleName() + ",null, " + handlers.getClass().getSimpleName() + ")" + JSFTestUtil.NL + "Expected: " + expected + JSFTestUtil.NL + "Received: " + e.getClass().getName());
}
// AttachedObjectHandler as 'null'
try {
vdlWrapper.retargetAttachedObjects(context, component, null);
buff.append("No Exception thrown when calling " + vdlWrapper.getClass().getName() + "." + methName + "(" + context.getClass().getSimpleName() + ", null, " + handlers.getClass().getSimpleName() + ")" + JSFTestUtil.NL + "Expected: " + expected + JSFTestUtil.NL);
} catch (NullPointerException npe) {
// do nothing test passed
} catch (Exception e) {
buff.append("Unexpected Exception thrown for " + vdlWrapper.getClass().getName() + "." + methName + "(" + context.getClass().getSimpleName() + ", " + component.getClass().getSimpleName() + ")" + JSFTestUtil.NL + "Expected: " + expected + JSFTestUtil.NL + "Received: " + e.getClass().getName());
}
if (buff.length() > 0) {
out.println("Test FAILED. For the following reason(s):" + JSFTestUtil.NL + buff.toString());
} else {
out.println(JSFTestUtil.PASS);
}
}
Aggregations