public static String getFormClientId(UIComponent component, FacesContext context) { UIComponent parent = component.getParent(); while (parent != null) { if (parent instanceof UIForm) { break; } parent = parent.getParent(); } UIForm form = (UIForm) parent; if (form != null) { return form.getClientId(context); } return null; }
public static UIComponent findParentForm(FacesContext context, UIComponent component) { UIComponent parent = component; while (parent != null) if (parent instanceof UIForm) break; else parent = parent.getParent(); return parent; }
/** * Override the {@link UIComponent#getContainerClientId} to allow users to disable this form from * prepending its <code>clientId</code> to its descendent's <code>clientIds</code> depending on * the value of this form's {@link #isPrependId} property. */ public String getContainerClientId(FacesContext context) { if (this.isPrependId()) { return super.getContainerClientId(context); } else { UIComponent parent = this.getParent(); while (parent != null) { if (parent instanceof NamingContainer) { return parent.getContainerClientId(context); } parent = parent.getParent(); } } return null; }
/** * Method handles UIComponent tree creation in accordance with the JSF 1.2 spec. * * <ol> * <li>First determines this UIComponent's id by calling {@link * javax.faces.view.facelets.ComponentHandler#getTagId()}. * <li>Search the parent for an existing UIComponent of the id we just grabbed * <li>If found, {@link * com.sun.faces.facelets.tag.jsf.ComponentSupport#markForDeletion(javax.faces.component.UIComponent) * mark} its children for deletion. * <li>If <i>not</i> found, call {@link #createComponent(FaceletContext) createComponent}. * <ol> * <li>Only here do we apply {@link * com.sun.faces.facelets.tag.MetaTagHandlerImpl#setAttributes(FaceletContext, * Object)} * <li>Set the UIComponent's id * <li>Set the RendererType of this instance * </ol> * <li>Now apply the nextHandler, passing the UIComponent we've created/found. * <li>Now add the UIComponent to the passed parent * <li>Lastly, if the UIComponent already existed (found), then {@link * ComponentSupport#finalizeForDeletion(UIComponent) finalize} for deletion. * </ol> * * @throws TagException if the UIComponent parent is null */ @Override public void apply(FaceletContext ctx, UIComponent parent) throws IOException { FacesContext context = ctx.getFacesContext(); // make sure our parent is not null if (parent == null) { throw new TagException(owner.getTag(), "Parent UIComponent was null"); } // our id String id = ctx.generateUniqueId(owner.getTagId()); // grab our component UIComponent c = findChild(ctx, parent, id); if (null == c && context.isPostback() && UIComponent.isCompositeComponent(parent) && parent.getAttributes().get(id) != null) { c = findReparentedComponent(ctx, parent, id); } else { /** * If we found a child that is dynamic, the actual parent might have changed, so we need to * remove it from the actual parent. The reapplyDynamicActions will then replay the actions * and will make sure it ends up in the correct order. */ if (c != null && c.getParent() != parent && c.getAttributes().containsKey(DYNAMIC_COMPONENT)) { c.getParent().getChildren().remove(c); } } boolean componentFound = false; if (c != null) { componentFound = true; doExistingComponentActions(ctx, id, c); } else { c = this.createComponent(ctx); doNewComponentActions(ctx, id, c); assignUniqueId(ctx, parent, id, c); // hook method owner.onComponentCreated(ctx, c, parent); } CompositeComponentStackManager ccStackManager = CompositeComponentStackManager.getManager(context); boolean compcompPushed = pushComponentToEL(ctx, c, ccStackManager); if (ProjectStage.Development == context.getApplication().getProjectStage()) { ComponentSupport.setTagForComponent(context, c, this.owner.getTag()); } // If this this a naming container, stop generating unique Ids // for the repeated tags boolean setUniqueIds = false; boolean oldUnique = false; if (c instanceof NamingContainer) { oldUnique = ComponentSupport.setNeedUniqueIds(ctx, false); setUniqueIds = true; } try { // first allow c to get populated owner.applyNextHandler(ctx, c); } finally { if (setUniqueIds) ComponentSupport.setNeedUniqueIds(ctx, oldUnique); } // finish cleaning up orphaned children if (componentFound) { doOrphanedChildCleanup(ctx, parent, c); } this.privateOnComponentPopulated(ctx, c); owner.onComponentPopulated(ctx, c, parent); // add to the tree afterwards // this allows children to determine if it's // been part of the tree or not yet addComponentToView(ctx, parent, c, componentFound); adjustIndexOfDynamicChildren(context, c); popComponentFromEL(ctx, c, ccStackManager, compcompPushed); }
public static String findClientIds(FacesContext context, UIComponent component, String list) { if (list == null) return "@none"; // System.out.println("ComponentUtils.findClientIds() component.clientId: " + // component.getClientId(context) + " list: " + list); String[] ids = list.split("[,\\s]+"); StringBuilder buffer = new StringBuilder(); for (int i = 0; i < ids.length; i++) { if (i != 0) buffer.append(" "); String id = ids[i].trim(); // System.out.println("ComponentUtils.findClientIds() ["+i+"] id: " + id); if (id.equals("@all") || id.equals("@none")) { // System.out.println("ComponentUtils.findClientIds() ["+i+"] " + id); buffer.append(id); } else if (id.equals("@this")) { // System.out.println("ComponentUtils.findClientIds() ["+i+"] @this : " + // component.getClientId(context)); buffer.append(component.getClientId(context)); } else if (id.equals("@parent")) { // System.out.println("ComponentUtils.findClientIds() ["+i+"] @parent: " + // component.getParent().getClientId(context)); buffer.append(component.getParent().getClientId(context)); } else if (id.equals("@form")) { UIComponent form = ComponentUtils.findParentForm(context, component); if (form == null) throw new FacesException( "Component " + component.getClientId(context) + " needs to be enclosed in a form"); buffer.append(form.getClientId(context)); } else { UIComponent comp = component.findComponent(id); // For portlets, if the standard search doesn't work, it may be necessary to do an absolute // search // which requires including the portlet's namespace. So the resulting encoded id looks // something // like portletNamespace:container:componentId. We make the search absolute by pre-pending // a leading colon (:). if (comp == null) { String encodedId = encodeNameSpace(context, id); if (!encodedId.startsWith(":")) { encodedId = ":" + encodedId; } comp = component.findComponent(encodedId); // System.out.println("ComponentUtils.findClientIds() ["+i+"] comp // : " + (comp == null ? "null" : comp.getClientId(context)) + " id: " + encodedId); } if (comp != null) { buffer.append(comp.getClientId(context)); } else { if (context.getApplication().getProjectStage().equals(ProjectStage.Development)) { logger.log(Level.INFO, "Cannot find component with identifier \"{0}\" in view.", id); } buffer.append(id); } } } return buffer.toString(); }