// our id
String id = ctx.generateUniqueId(_delegate.getTagId());
// Cast to use UniqueIdVendor stuff
AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
// grab our component
UIComponent c = ComponentSupport.findChildByTagId(parent, id);
// Check if the component was relocated using
// composite:insertChildren or composite:insertFacet
boolean componentFoundInserted = false;
if (c == null && actx.isRefreshingTransientBuild() && UIComponent.isCompositeComponent(parent))
{
if (facetName == null)
{
String targetClientId = (String) parent.getAttributes().get(InsertChildrenHandler.INSERT_CHILDREN_TARGET_ID);
if (targetClientId != null)
{
UIComponent targetComponent = parent.findComponent(targetClientId.substring(parent.getClientId().length()+1));
if (targetComponent != null)
{
c = ComponentSupport.findChildByTagId(targetComponent, id);
}
}
if (c != null)
{
c.getAttributes().put(InsertChildrenHandler.USES_INSERT_CHILDREN, Boolean.TRUE);
componentFoundInserted = true;
}
}
else
{
String targetClientId = (String) parent.getAttributes().get(InsertFacetHandler.INSERT_FACET_TARGET_ID+facetName);
if (targetClientId != null)
{
UIComponent targetComponent = parent.findComponent(targetClientId.substring(parent.getClientId().length()+1));
if (targetComponent != null)
{
c = ComponentSupport.findChildByTagId(targetComponent, id);
if (c != null)
{
c.getAttributes().put(InsertFacetHandler.USES_INSERT_FACET, Boolean.TRUE);
componentFoundInserted = true;
}
}
}
}
}
boolean componentFound = false;
if (c != null)
{
componentFound = true;
// mark all children for cleaning
if (log.isLoggable(Level.FINE))
{
log.fine(_delegate.getTag() + " Component[" + id + "] Found, marking children for cleanup");
}
ComponentSupport.markForDeletion(c);
}
else
{
c = this.createComponent(ctx);
if (log.isLoggable(Level.FINE))
{
log.fine(_delegate.getTag() + " Component[" + id + "] Created: " + c.getClass().getName());
}
_delegate.setAttributes(ctx, c);
// mark it owned by a facelet instance
c.getAttributes().put(ComponentSupport.MARK_CREATED, id);
if (facesContext.isProjectStage(ProjectStage.Development))
{
c.getAttributes().put(UIComponent.VIEW_LOCATION_KEY,
_delegate.getTag().getLocation());
}
// assign our unique id
if (this._id != null)
{
c.setId(this._id.getValue(ctx));
}
else
{
UniqueIdVendor uniqueIdVendor = actx.getUniqueIdVendorFromStack();
if (uniqueIdVendor == null)
{
uniqueIdVendor = facesContext.getViewRoot();
if (uniqueIdVendor == null)
{
// facesContext.getViewRoot() returns null here if we are in
// phase restore view, so we have to try to get the view root
// via the method in ComponentSupport and our parent
uniqueIdVendor = ComponentSupport.getViewRoot(ctx, parent);
}
}
if (uniqueIdVendor != null)
{
// UIViewRoot implements UniqueIdVendor, so there is no need to cast to UIViewRoot
// and call createUniqueId()
String uid = uniqueIdVendor.createUniqueId(facesContext, id);
c.setId(uid);
}
}
if (this._rendererType != null)
{
c.setRendererType(this._rendererType);
}
// hook method
_delegate.onComponentCreated(ctx, c, parent);
}
c.pushComponentToEL(facesContext, c);
if (c instanceof UniqueIdVendor)
{
actx.pushUniqueIdVendorToStack((UniqueIdVendor)c);
}
// first allow c to get populated
_delegate.applyNextHandler(ctx, c);
// finish cleaning up orphaned children
if (componentFound)
{
ComponentSupport.finalizeForDeletion(c);
if (!componentFoundInserted)
{
if (facetName == null)
{
parent.getChildren().remove(c);
}
else
{
ComponentSupport.removeFacet(ctx, parent, c, facetName);
}
}
}
if (actx.isRefreshingTransientBuild() &&
UIComponent.isCompositeComponent(parent))
{
// Save the child structure behind this component, so it can be
// used later by InsertChildrenHandler and InsertFacetHandler
// to update components correctly.
if (facetName != null)
{
if (parent.getAttributes().containsKey(InsertFacetHandler.INSERT_FACET_TARGET_ID+facetName))
{
List<String> ordering = (List<String>) parent.getAttributes().get(
InsertFacetHandler.INSERT_FACET_ORDERING+facetName);
if (ordering == null)
{
ordering = new ArrayList<String>();
parent.getAttributes().put(InsertFacetHandler.INSERT_FACET_ORDERING+facetName, ordering);
}
ordering.remove(id);
ordering.add(id);
}
}
else
{
if (parent.getAttributes().containsKey(InsertChildrenHandler.INSERT_CHILDREN_TARGET_ID))
{
List<String> ordering = (List<String>) parent.getAttributes().get(
InsertChildrenHandler.INSERT_CHILDREN_ORDERING);
if (ordering == null)
{
ordering = new ArrayList<String>();
parent.getAttributes().put(InsertChildrenHandler.INSERT_CHILDREN_ORDERING, ordering);
}
ordering.remove(id);
ordering.add(id);
}
}
}
if (c instanceof ClientBehaviorHolder && !UIComponent.isCompositeComponent(c))
{
Iterator<AjaxHandler> it = actx.getAjaxHandlers();
if (it != null)
{
while(it.hasNext())
{
it.next().applyAttachedObject(facesContext, c);
}
}
}
if (c instanceof EditableValueHolder)
{
// add default validators here, because this feature
// is only available in facelets (see MYFACES-2362 for details)
addDefaultValidators(facesContext, actx, (EditableValueHolder) c);
}
_delegate.onComponentPopulated(ctx, c, parent);
if (!componentFoundInserted)
{
// add to the tree afterwards
// this allows children to determine if it's
// been part of the tree or not yet
if (facetName == null)
{
parent.getChildren().add(c);
}
else
{
ComponentSupport.addFacet(ctx, parent, c, facetName);
}
}
/*
else
{
if (facetName != null)
{
if (UIComponent.isCompositeComponent(parent))
{
UIComponent facet = parent.getFacet(facetName);
if (Boolean.TRUE.equals(facet.getAttributes().get(ComponentSupport.FACET_CREATED_UIPANEL_MARKER)))
{
facet.getAttributes().put(InsertFacetHandler.USES_INSERT_FACET, Boolean.TRUE);
}
}
}
}*/
if (c instanceof UniqueIdVendor)
{
actx.popUniqueIdVendorToStack();
}
c.popComponentFromEL(facesContext);
if (actx.isMarkInitialState())
{
//Call it only if we are using partial state saving
c.markInitialState();
}
}