Java Code Examples for javax.faces.component.UIComponent.getChildren()

The following are Jave code examples for showing how to use getChildren() of the javax.faces.component.UIComponent class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: oscm   File: JSFUtils.java   Source Code and License Vote up 6 votes
/**
 * Reset the values of all UIInput children. This might be necessary after a
 * validation error to successfully process an AJAX request. See [Bug 5449]
 * and http://wiki.apache.org/myfaces/ClearInputComponents
 * 
 * @param uiComponent
 *            the root component to be processed.
 */
public static void resetUIInputChildren(UIComponent uiComponent) {
    if (uiComponent != null) {
        List<UIComponent> children = uiComponent.getChildren();
        for (UIComponent child : children) {
            if (child instanceof UIInput) {
                UIInput uiInput = (UIInput) child;
                uiInput.setSubmittedValue(null);
                uiInput.setValue(null);
                uiInput.setLocalValueSet(false);
            } else {
                resetUIInputChildren(child);
            }
        }
    }
}
 
Example 2
Project: myfaces-trinidad   File: ChangeBean.java   Source Code and License Vote up 6 votes
@SuppressWarnings("unchecked")
private static UIComponent _findChildById(UIComponent uic, String id)
{
  int numChildren = uic.getChildCount();
  if (numChildren == 0)
    return null;
  List<UIComponent> children = uic.getChildren();
  UIComponent child = null;
  for (int i=0; i<numChildren; i++)
  {
    child = children.get(i);
    if (id.equals(child.getId()))
      return child;
  }
  return null;
}
 
Example 3
Project: myfaces-trinidad   File: ChangeUtils.java   Source Code and License Vote up 6 votes
/**
 * Given a parent component and the identifier value for the child, looks up among
 * the children for a child with the specified identifier and returns.
 * Returns null if there were to be no such child
 * @param parent the parent UIComponent
 * @param childId the identifier value of child to be searched in the parent's 
 *        children.
 * @param identifier the identifier type 
 */
@SuppressWarnings("unchecked")
public static UIComponent getChildForId(
  UIComponent parent, 
  String childId,
  String identifier)
{
  if (parent == null)
    return null;

  int numChildren = parent.getChildCount();
  if (numChildren == 0)
    return null;

  List<UIComponent> children = parent.getChildren();
  
  for (int i=0; i<numChildren; i++)
  {
    UIComponent child = children.get(i);
    Object attrVal = child.getAttributes().get(identifier);
    
    if ( childId.equals(attrVal) )
      return child;
  }
  return null;
}
 
Example 4
Project: myfaces-trinidad   File: ColumnGroupRenderer.java   Source Code and License Vote up 6 votes
@SuppressWarnings("unchecked")
private void _renderChildren(
  FacesContext context,
  UIComponent  component,
  NodeData     parentNode
  ) throws IOException
{
  int i = 0;
  for(UIComponent child : (List<UIComponent>)component.getChildren())
  {
    if (child.isRendered())
    {
      // Tell the parent node - if there is one - which child we're rendering
      if (parentNode != null)
      {
        parentNode.currentChild = i;
      }

      encodeChild(context, child);
    }

    i++;
  }
}
 
Example 5
Project: myfaces-trinidad   File: StampState.java   Source Code and License Vote up 6 votes
/**
 * Restore the stamp state of just the children of the given component
 * in the given table.
 */
@SuppressWarnings("unchecked")
public static void restoreChildStampState(
  FacesContext context,
  UIComponent stamp,
  UIXCollection table,
  Object stampState)
{
  if (stampState == null || !(stampState instanceof Map))
    return;

  List<UIComponent> kids = stamp.getChildren();
  Map<String, Object> state = (Map<String, Object>)stampState;

  for (UIComponent kid : kids)
  {
    Object childState = state.get(kid.getId());
    if (childState == null)
      continue;

    table.restoreStampState(context, kid, childState);
  }
}
 
Example 6
Project: myfaces-trinidad   File: CoreRenderer.java   Source Code and License Vote up 6 votes
/**
 * Returns the total number of children with rendered=="true".
 */
@SuppressWarnings("unchecked")
static public int getRenderedChildCount(
  UIComponent component)
{
  int count = component.getChildCount();
  if (count == 0)
    return 0;

  int total = 0;
  for(UIComponent child : (List<UIComponent>)component.getChildren())
  {
    if (child.isRendered())
    {
      total++;
    }
  }

  return total;
}
 
Example 7
Project: myfaces-trinidad   File: RemoveChildComponentChange.java   Source Code and License Vote up 5 votes
/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
@Override
public void changeComponent(UIComponent uiComponent)
{
  if (uiComponent.getChildCount() == 0)
    return;
    
  List<UIComponent> children = uiComponent.getChildren();
  children.remove(ChangeUtils.getChildForId(uiComponent, _childId, _identifier));
}
 
Example 8
Project: myfaces-trinidad   File: ShowOneListRendererBase.java   Source Code and License Vote up 5 votes
/**
 *  Find and encodes recursivly the child specified.
 *
 *  If the specified child can't be found, doesn't do anything.
 *
 *  @param context the facesContext object
 *  @param parent which contains the child to be encoded
 *  @param disclosedChildId id of the child which occurs in parent
 *         and has to be encoded recursively.
 *
 *  @throws IOException when any IO error while writing markup
 */
@SuppressWarnings("unchecked")
private static void _findAndEncodeChild(FacesContext context,
                                        UIComponent parent,
                                        String disclosedChildId)
  throws IOException
{
  UIComponent disclosedChild = _findChild(context,
                                          parent,
                                          disclosedChildId);

  if (disclosedChild == null)
  {
    return;
  }

  // Paint container span for showDetail child
  ResponseWriter out = context.getResponseWriter();
  out.startElement("span", disclosedChild);

  // Render the children of disclosedChild since the child is disclosed.
  if (disclosedChild.isRendered())
  {
    if (disclosedChild.getChildCount() > 0)
    {
      for(UIComponent child : (List<UIComponent>)disclosedChild.getChildren())
      {
        RenderUtils.encodeRecursive(context, child);
      }
    }
  }

  out.endElement("span");
  // end container span for showDetail child
}
 
Example 9
Project: myfaces-trinidad   File: CoreRenderer.java   Source Code and License Vote up 5 votes
@SuppressWarnings("unchecked")
protected void encodeAllChildren(
  FacesContext context,
  UIComponent  component
  ) throws IOException
{
  int childCount = component.getChildCount();
  if (childCount == 0)
    return;

  for (UIComponent child : (List<UIComponent>)component.getChildren())
  {
    encodeChild(context, child);
  }
}
 
Example 10
Project: myfaces-trinidad   File: Structure.java   Source Code and License Vote up 5 votes
/**
 * Re-create a component from a structure object
 */
@SuppressWarnings("unchecked")
public UIComponent createComponent()
  throws ClassNotFoundException, InstantiationException,
         IllegalAccessException
{
  Class<?> clazz = ClassLoaderUtils.loadClass(_class);
  UIComponent component = (UIComponent) clazz.newInstance();
  if (_id != null)
    component.setId(_id);
  // Create any facets
  if (_facets != null)
  {
    Map<String, UIComponent> facets = component.getFacets();
    for (int i = 0 ; i < _facets.size(); i += 2)
    {
      UIComponent facet = ((Structure) _facets.get(i + 1)).
                               createComponent();
      facets.put((String)_facets.get(i), facet);
    }
  }

  // Create any children
  if (_children != null)
  {
    List<UIComponent> children = component.getChildren();
    for (int i = 0 ; i < _children.size(); i++)
    {
      UIComponent child = _children.get(i).createComponent();
      children.add(child);
    }
  }

  return component;
}
 
Example 11
Project: myfaces-trinidad   File: PanelGroupLayoutRenderer.java   Source Code and License Vote up 5 votes
/**
 * Render all the children of the PanelGroup
 */
@SuppressWarnings("unchecked")
private void _encodeChildren(
  FacesContext context,
  UIComponent  component,
  boolean      isVertical,
  boolean      isHorizontal
  ) throws IOException
{
  ResponseWriter rw = context.getResponseWriter();

  UIComponent separator = getFacet(component,
                                   CorePanelGroupLayout.SEPARATOR_FACET);

  boolean needSeparator = false;
  for(UIComponent child : (List<UIComponent>)component.getChildren())
  {
    if (!child.isRendered())
      continue;

    if (needSeparator)
    {
      if (isVertical)
        rw.startElement("div", null);
      encodeSeparator(context, separator, isHorizontal);
      if (isVertical)
        rw.endElement("div");
    }

    encodeChild(context, child, isHorizontal);
    needSeparator = true;
  }
}
 
Example 12
Project: myfaces-trinidad   File: TableUtils.java   Source Code and License Vote up 5 votes
static void cacheColumnHeaderFooterFacets(UIComponent parent, Map<UIComponent, Boolean> cache)
{
  List<UIComponent> children = parent.getChildren();
  for (UIComponent child : children)
  {
    if (child instanceof UIXColumn)
    {
      cacheHeaderFooterFacets(child, cache);
      cacheColumnHeaderFooterFacets(child, cache);
    }
  }
}
 
Example 13
Project: myfaces-trinidad   File: ColumnRenderer.java   Source Code and License Vote up 5 votes
@SuppressWarnings("unchecked")
protected void renderKids(
  FacesContext          context,
  RenderingContext      rc,
  TableRenderingContext trc,
  UIComponent           column
  ) throws IOException
{
  boolean renderedOne = false;
  for(UIComponent child : (List<UIComponent>)column.getChildren())
  {
    if (child.isRendered())
    {
      // Put each component on a separate line, separated by a div
      if (renderedOne)
      {
        ResponseWriter rw = context.getResponseWriter();
        rw.startElement("div", null);
        rw.endElement("div");
      }
      else
      {
        renderedOne = true;
      }

      encodeChild(context, child);
    }
  }
}
 
Example 14
Project: myfaces-trinidad   File: SelectItemSupport.java   Source Code and License Vote up 4 votes
/**
 *
 * @param component  UIComponent
 * @param converter For UISelectItem and UIXSelectItem children of the
 *                  component, use the converter to convert itemValue Strings
 *                  when creating the javax.faces.model.SelectItem Object if
 *                  the child's value is not an instanceof SelectItem.
 * @param filteredItems to exclude SelectItemGroup components
 * @return a List of javax.faces.model.SelectItem Objects that we get or
 *         create from the component's children.
 *         OR
 *         java.util.Collections.emptyList if component has no children or
 *         the component isn't a javax.faces.component.ValueHolder. else
 */
@SuppressWarnings("unchecked")
static public List<SelectItem> getSelectItems(
  UIComponent  component,
  Converter    converter,
  boolean filteredItems)
{

  int childCount = component.getChildCount();
  if (childCount == 0)
    return Collections.emptyList();

  // Make sure we haven't accidentally stumbled outside of
  // the UIXSelectXXX world.
  if (!(component instanceof ValueHolder))
    return Collections.emptyList();

  FacesContext context = FacesContext.getCurrentInstance();
  List<SelectItem> items = null;
  for(UIComponent child : (List<UIComponent>)component.getChildren())
  {
    // f:selectItem
    if (child instanceof UISelectItem)
    {
      if (items == null)
        items = new ArrayList<SelectItem>(childCount);
      _addSelectItem(context, 
                     component, 
                     (UISelectItem) child, 
                     items, 
                     converter);
    }
    // f:selectItems
    else if (child instanceof UISelectItems)
    {
      if (items == null)
        items = new ArrayList<SelectItem>(childCount);
      addSelectItems((UISelectItems) child, items, filteredItems );
    }
    // tr:selectItem
    else if (child instanceof UIXSelectItem)
    {
      if (items == null)
        items = new ArrayList<SelectItem>(childCount);
      _addUIXSelectItem(context,
                        component,
                        (UIXSelectItem) child, 
                        items, 
                        converter);
      

      
    }
  }

  if (items == null)
    return Collections.emptyList();

  return items;
}
 
Example 15
Project: myfaces-trinidad   File: SelectItemSupport.java   Source Code and License Vote up 4 votes
/**
 * 
 * @param component  UIComponent
 * @return item count
 */
@SuppressWarnings("unchecked")
static public int getSelectItemCount(
  UIComponent  component)
{ 
  int itemCount = 0;
  int childCount = component.getChildCount();
  if (childCount == 0)
    return itemCount;

  // Make sure we haven't accidentally stumbled outside of
  // the UIXSelectXXX world.
  if (!(component instanceof ValueHolder))
    return itemCount;

  for(UIComponent child : (List<UIComponent>)component.getChildren())
  {
    if (child instanceof UISelectItem ||
        child instanceof UIXSelectItem)
    {
      itemCount++;
    }
    // f:selectItems
    else if (child instanceof UISelectItems)
    {        
      Object value = ((UISelectItems)child).getValue();
      if (value instanceof SelectItem)
      {
        itemCount++;
      }
      else if (value instanceof Object[])
      {
        Object[] array = (Object[]) value;
        itemCount = itemCount + array.length;
      }
      else if (value instanceof Collection)
      {
        itemCount = itemCount + ((Collection) value).size();  
      }
      else if (value instanceof Map)
      {        
        itemCount = itemCount + ((Map) value).size();  
      }                        
    }
  }
  
  return itemCount;
}
 
Example 16
Project: myfaces-trinidad   File: PanelAccordionRenderer.java   Source Code and License Vote up 4 votes
@SuppressWarnings("unchecked")
@Override
protected void decode(
  FacesContext facesContext,
  UIComponent  component,
  @SuppressWarnings("unused")
  FacesBean    facesBean,
  String       clientId)
{
  Map<String, String> parameters =
    facesContext.getExternalContext().getRequestParameterMap();

  Object event = parameters.get(XhtmlConstants.EVENT_PARAM);
  if (XhtmlConstants.HIDE_EVENT.equals(event) ||
      XhtmlConstants.SHOW_EVENT.equals(event))
  {
    Object source = parameters.get(XhtmlConstants.SOURCE_PARAM);
    String id = clientId == null ? component.getClientId(facesContext) : clientId;

    if (id.equals(source))
    {
      boolean isDisclosed = XhtmlConstants.SHOW_EVENT.equals(event);
      String itemId = parameters.get("targetItem");
      if (itemId != null)
      {
        List<UIComponent> children = component.getChildren();
        for (UIComponent child : children)
        {
          if (!(child instanceof UIXShowDetail))
            continue;

          // Don't even bother with disabled showDetailItems -
          // a malicious user should not be able to force
          // a disabled item open by dummying up an event
          if (!child.isRendered() || _isItemDisabled(child))
            continue;

          if (itemId.equals(child.getClientId(facesContext)))
          {
            (new DisclosureEvent(child, isDisclosed)).queue();
            RequestContext rc = RequestContext.getCurrentInstance();

            // Don't force PPR on for the browsers that do not support Ajax
            Object cap = rc.getAgent().getCapabilities().get(TrinidadAgent.CAP_PARTIAL_RENDERING);
            if ((cap != null) && (Boolean.TRUE.equals(cap)))
            {
              RequestContext.getCurrentInstance().addPartialTarget(component);
              PartialPageUtils.forcePartialRendering(facesContext);
            }

            break;
          }
        }
      }
    }
  }
}
 
Example 17
Project: myfaces-trinidad   File: ChildArrayList.java   Source Code and License Vote up 4 votes
@SuppressWarnings("unchecked")
static int __removeFromParent(
  UIComponent component,
  int index)
{
  UIComponent parent = component.getParent();
  assert(parent != null);

  if (parent.getChildCount() > 0)
  {
    List<UIComponent> children = parent.getChildren();
    int size = children.size();
    for  (int i = 0; i < size; i++)
    {
      if  (children.get(i) == component)
      {
        children.remove(i);
        if (index > i)
          index--;
        return index;
      }
    }
  }
  
  // TRINIDAD-2369: Until TRINIDAD-2368 is fixed, we have to call remove() on the map
  // returned by getFacets() rather than calling remove() on getFacets().values()
  // Note that the old code used to call getFacets().values().contains(), which would iterate on the entry set.
  // The new code performs the same iteration, so it should not be any slower.
  
  Map<String, UIComponent> facets = parent.getFacets();
  for (Map.Entry<String, UIComponent> entry: facets.entrySet())
  {
    if (entry.getValue() == component)
    {
      facets.remove(entry.getKey());
      return index;
    }
  }

  // Not good - the child thought it was in a parent,
  // but it wasn't.
  assert(false);
  return index;
}
 
Example 18
Project: myfaces-trinidad   File: ReorderChildrenComponentChange.java   Source Code and License Vote up 4 votes
/**
 * {@inheritDoc}
 * In case children were to be removed between the time when this Change was
 *  added, and the time when it was applied, maybe due to application of a
 *  RemoveChildrenChange, such children are not re-instated.
 * In case children were to be added between the time when this Change was
 *  added, and the time when it was applied, maybe due to application of an 
 *  AddChildChange, such children are appended to the end of the list in
 *  preserving the order in which they were added (that is they appear at 
 *  the end).
 */
@SuppressWarnings("unchecked")
@Override
public void changeComponent(UIComponent uiComponent)
{
  int childCount = uiComponent.getChildCount();
  if (childCount == 0)
    return;
 
  // build order map of of current Nodes, keyed by id
  Map<String, UIComponent> childrenMap = new LinkedHashMap<String, UIComponent>();
  
  List<UIComponent> children = uiComponent.getChildren();
  
  int fakeIndex = 0;
  for(UIComponent child : children)
  {
    String attrValue = (String)child.getAttributes().get(_identifier);
    
    // create a dummy key to maintain order of children whose identifier 
    // does not exist
    if (attrValue == null) 
    {
      attrValue = Integer.valueOf(fakeIndex++).toString();
    }
    childrenMap.put(attrValue, child);
  }

  // remove the children so that we can add them back in
  children.clear();

  //
  // put children back in, in order
  //
  for(String currReorderID : _childIds)
  {
    UIComponent currChild = childrenMap.remove(currReorderID);
    
    if (currChild != null)
    {
      children.add(currChild);
    }
  }
  
  // add in all of the rest of the children in
  // relative order they originally appeared
  children.addAll(childrenMap.values());
}
 
Example 19
Project: myfaces-trinidad   File: CSVBean.java   Source Code and License Vote up 4 votes
private boolean _exportCell(UIComponent cell, Writer out) throws IOException
{
  boolean foundValue = false;
  if (cell instanceof ValueHolder)
  {
    foundValue = true;
    Object value = ((ValueHolder)cell).getValue();
    if (value != null)
    {
      String valueStr;
      Converter converter = ((ValueHolder) cell).getConverter();

      // TODO: add default by-type converter support
      if (converter != null)
      {
        try
        {
          valueStr = converter.getAsString(FacesContext.getCurrentInstance(),
                                           cell,
                                           value);
        }
        catch (ConverterException ce)
        {
          valueStr = value.toString();
        }
      }
      else
      {
        valueStr = value.toString();
      }

      if (valueStr != null)
      {
        out.write('"');
        out.write(_csvEscape(valueStr));
        out.write('"');
      }
    }
  }


  if (cell.getChildCount() > 0)
  {
    List children = cell.getChildren();
    for (Object o : children)
    {
      UIComponent child = (UIComponent) o;
      if (!child.isRendered())
        continue;
      
      if (_exportCell(child, out))
        foundValue = true;
    }
  }

  return foundValue;
}
 
Example 20
Project: myfaces-trinidad   File: PdaTableRenderer.java   Source Code and License Vote up 4 votes
@SuppressWarnings("unchecked")
private void _renderColumnFooter(
  FacesContext          context,
  RenderingContext      rc,
  TableRenderingContext tContext,
  UIComponent           component
  ) throws IOException
{
  tContext.getRenderStage().setStage(RenderStage.COLUMN_FOOTER_STAGE);
  final ColumnData colData = tContext.getColumnData();
  UIComponent footer = getFacet(component, CoreTable.FOOTER_FACET);
  if (footer != null)
  {
    ResponseWriter writer = context.getResponseWriter();
    writer.startElement(XhtmlConstants.TABLE_ROW_ELEMENT, null);
    /*  boolean useScroll = (getHeight(getFacesBean(component)) != null) && isIE(arc);
   if (useScroll)
   {
     writer.writeAttribute("style", "position:relative;"+
                                    "bottom:expression("+
                                     "this.offsetParent.scrollHeight-this.offsetParent.scrollTop-"+
                                     "this.offsetParent.clientHeight+1);" +
                                    "left:-1px", null);
   }
*/
    writer.startElement(XhtmlConstants.TABLE_HEADER_ELEMENT, null);
    // total rows may need an ID. see bug 3211593:
    /* Need new scheme for generateUniqueId()?
   String rowID = XhtmlLafUtils.generateUniqueID(tContext);
   writer.writeAttribute(XhtmlLafConstants.ID_ATTRIBUTE, rowID, null);
   tContext.getRowData().setCurrentRowHeaderID(rowID);
   */
    final int firstFooterPhysicalIndex =
      colData.getPhysicalIndexOfFirstFooter();
    final int colSpan =
      (firstFooterPhysicalIndex > 0)? firstFooterPhysicalIndex:
      tContext.getActualColumnCount();
    writer.writeAttribute(XhtmlConstants.COLSPAN_ATTRIBUTE,
        IntegerUtils.getString(colSpan), null);
    renderStyleClass(context, rc,
        SkinSelectors.AF_TABLE_COLUMN_FOOTER_STYLE);
    encodeChild(context, footer);
    writer.endElement(XhtmlConstants.TABLE_HEADER_ELEMENT);
    if (firstFooterPhysicalIndex > 0)
    {
      colData.setColumnIndex(tContext.getSpecialColumnCount(),
          0) /*logicalColumnIndex*/;

      for (UIComponent child:
        (List<UIComponent>) component.getChildren())
      {
        if (child.isRendered())
        {
          encodeChild(context, child);
        }
      }
    }
    writer.endElement(XhtmlConstants.TABLE_ROW_ELEMENT);
  }
}