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

The following are Jave code examples for showing how to use getChildCount() 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: myfaces-trinidad   File: PanelBorderLayoutRenderer.java   Source Code and License Vote up 6 votes
/**
 * Returns the row span to use for the left and right children
 */
private Integer _getRowSpan(
  UIComponent component
  )
{
  int rowSpan = 0;

  if (getFacet(component, CorePanelBorderLayout.INNER_TOP_FACET) != null)
    rowSpan++;

  // increment the rowspan if we have any content children.
  if (component.getChildCount() > 0)
    rowSpan++;

  if (getFacet(component, CorePanelBorderLayout.INNER_BOTTOM_FACET) != null)
    rowSpan++;

  return (rowSpan > 0) ? rowSpan : null;
}
 
Example 2
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 3
Project: myfaces-trinidad   File: UIComponentUINode.java   Source Code and License Vote up 6 votes
@SuppressWarnings("unchecked")
private void _renderComponent(FacesContext context, UIComponent component)
  throws IOException
{
  component.encodeBegin(context);
  if (component.getRendersChildren())
  {
    component.encodeChildren(context);
  }
  else
  {
    int count = component.getChildCount();
    if (count > 0)
    {
      List<UIComponent> children = component.getChildren();
      for (int i = 0; i < count; i++)
      {
        UIComponent child = children.get(i);
        if (child.isRendered())
          _renderComponent(context, child);
      }
    }
  }

  component.encodeEnd(context);
}
 
Example 4
Project: myfaces-trinidad   File: Structure.java   Source Code and License Vote up 6 votes
/**
 * Store the structure of all the children.
 */
@SuppressWarnings("unchecked")
private List<Structure> _getChildren(UIComponent component)
{
  if (component.getChildCount() == 0)
    return null;

  List<UIComponent> children = component.getChildren();
  ArrayList<Structure> list = new ArrayList<Structure>(children.size());

  for(UIComponent child : children)
  {
    if ((child != null) && !child.isTransient())
    {
      list.add(new Structure(child));
    }
  }

  if (list.isEmpty())
    return null;

  return list;
}
 
Example 5
Project: myfaces-trinidad   File: ChangeBean.java   Source Code and License Vote up 6 votes
/**
 * Removes a pair of children, based on some characteristic of the
 *  event source.
 */
public void removeChildren(ActionEvent event)
{
  UIComponent eventSource = event.getComponent();
  UIComponent uic = eventSource.findComponent("pg1");
  int numChildren = uic.getChildCount();
  if (numChildren == 0)
    return;
  String eventSourceId = eventSource.getId();    
  if (eventSourceId.equals("cb2"))
  {
    _removeChild(uic, "sic1");
    _removeChild(uic, "cc1");
  }
  else if (eventSourceId.equals("cb3"))
  {
    _removeChild(uic, "cd1");
    _removeChild(uic, "sid1");
  }
}
 
Example 6
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 7
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 8
Project: myfaces-trinidad   File: CoreRenderer.java   Source Code and License Vote up 6 votes
/**
 * Returns true if the component has children and at least
 * one has rendered=="true".
 */
@SuppressWarnings("unchecked")
static public boolean hasRenderedChildren(
  UIComponent component)
{
  int count = component.getChildCount();
  if (count == 0)
    return false;

  for(UIComponent child : (List<UIComponent>)component.getChildren())
  {
    if (child.isRendered())
    {
      return true;
    }
  }

  return false;
}
 
Example 9
Project: myfaces-trinidad   File: CoreRenderer.java   Source Code and License Vote up 5 votes
/**
 * Get a collection of all the parameters that are children of the current component as
 * client behavior parameters.
 * @param component The component
 * @return Collection of parameters (will be non-null)
 */
public static Collection<ClientBehaviorContext.Parameter> getBehaviorParameters(
  UIComponent component)
{
  int childCount = component.getChildCount();
  if (childCount > 0)
  {
    List<ClientBehaviorContext.Parameter> list = null;
    for (UIComponent child : component.getChildren())
    {
      if (!(child instanceof UIParameter)) { continue; }

      if (list == null)
      {
        // leave plenty of room to hold the parameters
        list = new ArrayList<ClientBehaviorContext.Parameter>(childCount);
      }
      UIParameter param = (UIParameter) child;
      list.add(new ClientBehaviorContext.Parameter(param.getName(), param.getValue()));
    }

    if (list != null)
    {
      return list;
    }
  }

  return Collections.<ClientBehaviorContext.Parameter>emptyList();
}
 
Example 10
Project: myfaces-trinidad   File: PanelLabelAndMessageRenderer.java   Source Code and License Vote up 5 votes
@Override
protected String getLabelFor(
  FacesContext     context,
  RenderingContext rc,
  UIComponent      component,
  FacesBean        bean)
{
  String forValue = getFor(component, bean);

  String val = null;
  if (forValue != null)
  {
    val = MessageUtils.getClientIdFor(context, component, forValue);
  }
  else
  {
    if (component.getChildCount() > 0)
    {
      UIComponent child = findForComponent(context, rc, component, bean);
      if (child != null)
      {
        val = child.getClientId(context);
      }
    }
  }

  return val;
}
 
Example 11
Project: myfaces-trinidad   File: StampState.java   Source Code and License Vote up 5 votes
/**
 * save the stamp state of just the children of the given component
 * in the given table.
 */
@SuppressWarnings("unchecked")
public static Object saveChildStampState(
  FacesContext context,
  UIComponent   stamp,
  UIXCollection table)
{
  int childCount = stamp.getChildCount();
  // If we have any children, iterate through the map,
  // saving state
  if (childCount == 0)
    return null;

  Map<String, Object> childStateMap = null;
  List<UIComponent> children = stamp.getChildren();
  for(int i=0; i < childCount; i++)
  {
    UIComponent child = children.get(i);
    Object childState = table.saveStampState(context, child);

    // Until we have one non-null entry, don't allocate the map.
    // So: allocate the map if we encounter our first
    // non-null childState

    if (childState == null)
      continue;

    if (childStateMap == null)
    {
      childStateMap = new HashMap<String, Object>(childCount);
    }

    // Store a value into the array
    childStateMap.put(child.getId(), childState);
  }

  return childStateMap;
}
 
Example 12
Project: myfaces-trinidad   File: UIXCollection.java   Source Code and License Vote up 5 votes
@Override
public VisitResult invokeVisitCallback(UIComponent component, VisitCallback callback)
{
  if (component instanceof UIXColumn)
  {
    if (component.getChildCount() > 0)
    {
      // visit only the indexed children of the columns
      for (UIComponent child : component.getChildren())
      {
        if (UIXComponent.visitTree(this, child, callback))
          return VisitResult.COMPLETE;
      }
    }

    return VisitResult.REJECT;
  }
  else
  {
    // Components do not expect to be visited twice, in fact with UIXComponent, it is illegal.
    // This is due to the fact that UIXComponent has setup and tearDown methods for visiting.
    // In order to avoid having the setup method called for the current visit context and
    // the wrapped context we invoke the visit on the component and then separately on the
    // children of the component
    VisitContext wrappedContext = getWrapped();
    VisitResult visitResult = wrappedContext.invokeVisitCallback(component, callback);

    if (visitResult == VisitResult.ACCEPT)
    {
      // Let the visitation continue with the wrapped context
      return (UIXComponent.visitChildren(wrappedContext, component, callback)) ?
        VisitResult.COMPLETE : VisitResult.REJECT;
    }
    else
    {
        return visitResult;
    }
  }
}
 
Example 13
Project: myfaces-trinidad   File: RenderUtils.java   Source Code and License Vote up 5 votes
/**
 * Encodes a component and all of its children.
 */
@SuppressWarnings("unchecked")
static public void encodeRecursive(FacesContext context,
                                   UIComponent component)
  throws IOException
{
  if (component.isRendered())
  {
    component.encodeBegin(context);
    if (component.getRendersChildren())
    {
      component.encodeChildren(context);
    }
    else
    {
      if (component.getChildCount() > 0)
      {
        for(UIComponent child : (List<UIComponent>)component.getChildren())
        {
          encodeRecursive(context, child);
        }
      }
    }

    component.encodeEnd(context);
  }
}
 
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: BreadCrumbsRenderer.java   Source Code and License Vote up 4 votes
protected boolean hasChildren(
  UIComponent component)
{
  int childCount = component.getChildCount();
  return childCount > 0;
}
 
Example 17
Project: myfaces-trinidad   File: NavigationPaneRenderer.java   Source Code and License Vote up 4 votes
protected boolean hasChildren(
  UIComponent component)
{
  int childCount = component.getChildCount();
  return childCount > 0;
}
 
Example 18
Project: myfaces-trinidad   File: PanelBoxRenderer.java   Source Code and License Vote up 4 votes
protected boolean hasChildren(
  UIComponent component)
{
  return component.getChildCount() > 0;
}
 
Example 19
Project: myfaces-trinidad   File: PdaTableRenderer.java   Source Code and License Vote up 4 votes
private void _renderColumnHeader(
  FacesContext          context,
  RenderingContext      rc,
  TableRenderingContext tContext,
  UIComponent           component
  ) throws IOException
{
  tContext.getRenderStage().setStage(RenderStage.COLUMN_HEADER_STAGE);

  ResponseWriter writer = context.getResponseWriter();
  ColumnData colData = tContext.getColumnData();
  colData.setRowIndex(0);
  writer.startElement("tr", null);

  int physicalCol = renderSpecialColumns(context, rc, tContext, component, 0);
  int[] hidden = tContext.getHiddenColumns();
  int colCount = component.getChildCount();

  for (int j = 0; j < colCount; j++)
  {
    if (hidden[j] != TableRenderingContext.NORMAL_COLUMN)
      continue;
    UIComponent child =
      (UIComponent) component.getChildren().get(j);
    if (!(child instanceof UIXColumn))
      continue;

    UIXColumn column = (UIXColumn) child;
    boolean isRowHeader = Boolean.TRUE.equals(
      column.getAttributes().get(CoreColumn.ROW_HEADER_KEY.getName()));
    if (!isRowHeader)
    {
      colData.setColumnIndex(physicalCol, j);
      encodeChild(context, column);
      // ColumnBeans automatically increment the physical and logical
      // column indices (these may be increase by more than one, if
      // there are columnGroups). So we must not increment the column
      // indices here
      physicalCol = colData.getPhysicalColumnIndex();
    }
  }

  colData.setRowIndex(-1);

  writer.endElement("tr");
}
 
Example 20
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;
}