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

The following are Jave code examples for showing how to use getParent() 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: ResetActionListener.java   Source Code and License Vote up 7 votes
private UIComponent _getContainingForm(UIComponent component)
{
  UIComponent previous = component;
  UIComponent parent = component.getParent();

  while (parent != null)
  {
    if ((parent instanceof UIForm)
        || (parent instanceof UIXForm)
        || (parent instanceof UIXSubform))
      return parent;

    previous = parent;
    parent = parent.getParent();
  }
  return previous;
}
 
Example 2
Project: myfaces-trinidad   File: UIComponentTestCase.java   Source Code and License Vote up 6 votes
@SuppressWarnings("unchecked")
protected void doTestUpdateModelValues(
  FacesContext context,
  UIViewRoot   root,
  UIComponent  component)
{
  Mock mock = createMockUIComponent();
  UIComponent child = (UIComponent) mock.proxy();

  // JavaServer Faces 1.0 Specification, section 2.2.4
  // During the update-model-values phase,
  // only the processUpdates lifecycle method may be called.
  if (willChildrenBeProcessed(component))
    mock.expects(once()).method("processUpdates");

  // construct the UIComponent tree and
  // execute the apply-request-values lifecycle phase
  if (component.getParent() == null)
    root.getChildren().add(component);
  component.getChildren().add(child);
  root.processUpdates(context);

  mock.verify();
}
 
Example 3
Project: myfaces-trinidad   File: RenderUtils.java   Source Code and License Vote up 6 votes
/**
 *  Retrieves id of the form the component is contained in.
 *
 * @param context the faces context object
 * @param component UIComponent whose container form id is to be retuirned
 *
 * @return String id of the form if one exists in component's hierarchy,
 *                otherwise null
 */
public static String getFormId(
  FacesContext context,
  UIComponent  component)
{
  UIComponent form = null;
  while (component != null)
  {
    if ((component instanceof UIForm) ||
        (component instanceof UIXForm))
    {
      form = component;
      break;
    }

    component = component.getParent();
  }

  if (form == null)
    return null;

  return form.getClientId(context);
}
 
Example 4
Project: myfaces-trinidad   File: UIXComponent.java   Source Code and License Vote up 6 votes
/**
 * @see #addPartialTarget(FacesContext, PartialPageContext, UIComponent)
 * @see #setPartialTarget(FacesContext, PartialPageContext)
 */
private static void _addPartialTargetImpl(
  FacesContext facesContext, PartialPageContext partialContext, UIComponent component)
{
  if (component.getRendererType() == null)
  {
    if (component.getParent() != null)
    {
      // delegate to the parent component, assuming that no renderer type means that
      // there is no suitable replacable DOM element for this component
      addPartialTarget(facesContext, partialContext, component.getParent());
    }
  }
  else
  {
    partialContext.addPartialTarget(component.getClientId(facesContext));
  }
}
 
Example 5
Project: myfaces-trinidad   File: ChildArrayList.java   Source Code and License Vote up 6 votes
@Override
public void add(int index, UIComponent element)
{
  if (element == null)
    throw new NullPointerException();

  if ((index < 0) || (index > size()))
    throw new IndexOutOfBoundsException(_LOG.getMessage(
      "INDEX_SIZE", new Object[]{index, size()}));

  UIComponent oldParent = element.getParent();
  if (oldParent != null)
  {
    int adjustedIndex = __removeFromParent(element, index);
    // Only adjust the index when the child is re-added to the same parent
    if (oldParent == _parent)
    {
      index = adjustedIndex; 
    }
  }
  
  // do not change the order of these calls, see TRINIDAD-1674 for more info
  super.add(index, element);
  element.setParent(_parent);
}
 
Example 6
Project: Mastering-Java-EE-Development-with-WildFly   File: JSFUtil.java   Source Code and License Vote up 5 votes
public static void removeComponent(String componentId) {
	UIViewRoot root = getCurrentInstance().getViewRoot();
	UIComponent component = root.findComponent(componentId);

	if (component != null) {
		UIComponent parent = component.getParent();
		parent.getChildren().remove(component);
	}
}
 
Example 7
Project: myfaces-trinidad   File: BaseChangeManager.java   Source Code and License Vote up 5 votes
/**
 * {@inheritDoc}
 */
@Override
public void addComponentChange(
  FacesContext facesContext,
  UIComponent uiComponent,
  ComponentChange change)
{
  // if our component is a stamped component by UIXIterator, we 
  // don't want to persist the changes 
  UIComponent parent = uiComponent.getParent();
  UIComponent root = facesContext.getViewRoot();
  while (parent != null && parent != root)
  {
    if (parent.getClass() == UIXIterator.class) 
    {
      _LOG.info("DONT_PERSIST_STAMPED_COMPONENT_INSIDE_ITERATOR");      
      return;
    }
    parent = parent.getParent();      
  }
      
  if (facesContext == null || uiComponent == null || change == null)
    throw new IllegalArgumentException(_LOG.getMessage(
      "CANNOT_ADD_CHANGE_WITH_FACECONTEXT_OR_UICOMPONENT_OR_NULL"));

  // add the change to the component
  addComponentChangeImpl(facesContext, uiComponent, change);

  // add a corresponding DocumentChange if possible
  _addEquivalentDocumentChange(facesContext, uiComponent, change);
}
 
Example 8
Project: myfaces-trinidad   File: FrameRenderer.java   Source Code and License Vote up 5 votes
@Override
protected final void encodeAll(
  FacesContext     context,
  RenderingContext rc,
  UIComponent      component,
  FacesBean        bean
  ) throws IOException
{
  UIComponent parent = component.getParent();

  //
  // complain if our parent isn't a FrameBorderLayout
  //
  if ((parent == null) ||
      !HtmlFrameBorderLayout.COMPONENT_FAMILY.equals(parent.getFamily()))
  {
    _LOG.warning("FRAMES_MUST_INSIDE_FRAMEBORDERLAYOUTS");
  }
  else
  {
    ResponseWriter writer = context.getResponseWriter();

    writer.startElement("frame", component);
    renderId(context, component);
    renderAllAttributes(context, rc, component, bean);
    writer.endElement("frame");
  }
}
 
Example 9
Project: myfaces-trinidad   File: UIXComponent.java   Source Code and License Vote up 5 votes
/**
 * Provides a logical parent for the component (a parent in the context of the document where the component was
 * defined). The default implementation will simply call getParent() on the component. Components that get relocated during
 * tag execution should have their original parent returned (if available).
 * @param component - child component whose parent is being retrieved
 * @return logical parent component
 */
public static UIComponent getLogicalParent(UIComponent component)
{
  if (component instanceof UIXComponent)
  {
    return ((UIXComponent)component).getLogicalParent();
  }

  return component.getParent();
}
 
Example 10
Project: myfaces-trinidad   File: LabelAndMessageRenderer.java   Source Code and License Vote up 5 votes
private boolean _isParentPanelForm(
  FacesContext context,
  UIComponent  component)
{
  if (PanelFormLayoutRenderer.__isInPanelFormLayout(context, component))
  {
    return true;
  }

  // We must know if the immediate parent is a PanelForm because if there is
  // even just one other component inbetween this component and a PanelForm,
  // we must render different DOM--the same DOM as if there were no parent
  // PanelForm.
  UIComponent parentComponent = component.getParent();
  String family = parentComponent.getFamily();
  // FIXME: OK... This is another strong coupling
  //        We could add a an interface instead like ComponentHolder or something
  //        instead of checking against a specific component family.
  while (UIXGroup.COMPONENT_FAMILY.equals(family))
  {
    // Special case:
    // Since UIXGroup components are purely organizational, it is valid to
    // have them inbetween panelForm-friendly components and the panelForm,
    // so we need to look further up the chain:
    parentComponent = parentComponent.getParent();
    if (parentComponent == null)
    {
      return false;
    }
    family = parentComponent.getFamily();
  }
  if (UIXPanel.COMPONENT_FAMILY.equals(family))
  {
    String rendererType = parentComponent.getRendererType();
    if (_isFormRendererType(rendererType))
      return true;
  }
  return false;
}
 
Example 11
Project: myfaces-trinidad   File: MoveChildComponentChange.java   Source Code and License Vote up 5 votes
/**
 * Returns the nth ancestor of the passed in component.
 * @param component The UIComponent whose nth ancestor has to be found
 * @param level Indicates how many levels to go up from the component
 * @return The nth ancestor of the component
 */
private static UIComponent _getAncestor(UIComponent component, int level) 
{
  assert(level >= 0);
  
  while(level > 0)
  {
    component = component.getParent();
    level--;
  }
  return component;
}
 
Example 12
Project: ol3jsf   File: ZoomSlider.java   Source Code and License Vote up 5 votes
@Override
public void encodeAll(FacesContext context) throws IOException {
	ResponseWriter writer = context.getResponseWriter();
	UIComponent parent = this;

	while (!(parent instanceof Map)) {
		parent = parent.getParent();
	}

	Map mapComponent = (Map) parent;
	String mapVar = mapComponent.getJsVariable();

	writer.write(mapVar + ".addControl(new ol.control.ZoomSlider());\n");
}
 
Example 13
Project: ol3jsf   File: OverviewMap.java   Source Code and License Vote up 5 votes
@Override
public void encodeAll(FacesContext context) throws IOException {
	ResponseWriter writer = context.getResponseWriter();
	UIComponent parent = this;

	while (!(parent instanceof Map)) {
		parent = parent.getParent();
	}

	Map mapComponent = (Map) parent;
	String mapVar = mapComponent.getJsVariable();

	writer.write(mapVar + ".addControl(new ol.control.OverviewMap());\n");
}
 
Example 14
Project: myfaces-trinidad   File: ProcessChoiceBarRenderer.java   Source Code and License Vote up 5 votes
private boolean _renderAsTable(
  UIXRenderingContext context,
  UINode              node
  )
{
  UIComponent component = NodeUtils.getUIComponent(context, node);
  if (component.getParent() instanceof CorePanelButtonBar)
    return false;

  return true;
}
 
Example 15
Project: myfaces-trinidad   File: ComponentUtils.java   Source Code and License Vote up 5 votes
private static UIComponent _getParentNamingContainerOrViewRoot (
  UIComponent from)
{
  while (from.getParent() != null)
  {
    from = from.getParent();
    if (from instanceof NamingContainer)
      break;
  }
  return from;
}
 
Example 16
Project: myfaces-trinidad   File: MoveChildComponentChange.java   Source Code and License Vote up 5 votes
/**
 * Returns the destination container if passed non-null after doing needed validations. If null 
 * destinationContainer is passed, determines it from the supplied insertBeforeComponent. 
 */
private static UIComponent _getValidatedDestinationContainer(
  UIComponent destinationContainer, 
  UIComponent insertBeforeComponent)
{
  if (insertBeforeComponent != null)
  {
    UIComponent parent = insertBeforeComponent.getParent();
    
    if (destinationContainer == null)
    {
      destinationContainer = parent;
    }
    
    // if container was supplied, it better be parent of component to move next to
    else if (destinationContainer != parent)
    {
      throw new IllegalArgumentException(
        _LOG.getMessage("DESTINATION_CONTAINER_NOT_INSERTBEFORES_PARENT"));
    }
  }
  else if (destinationContainer == null)
  {
    throw new IllegalArgumentException(_LOG.getMessage("DESTINATION_CONTAINER_REQUIRED"));
  }
  
  return destinationContainer;
}
 
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: ComponentUtils.java   Source Code and License Vote up 4 votes
/**
  * Find a component relative to another.
  * This method is the same as the 'old' public findRelativeComponent.
 * This method is around so that the
 * new findRelativeComponent method is backward compatibility.
  * <p>
  * The relative ID must account for NamingContainers. If the component is already inside
  * of a naming container, you can use a single colon to start the search from the root, 
  * or multiple colons to move up through the NamingContainers - "::" will search from 
  * the parent naming container, ":::" will search from the grandparent 
  * naming container, etc.
  * </p>
  * 
  * @param from the component to search relative to
  * @param relativeId the relative path to the component to find
  * @return the component if found, null otherwise
  */
private static UIComponent _findRelativeComponentDeprecated(
  UIComponent from,
  String      relativeId)
{  
  UIComponent originalFrom = from;
  String originalRelativeId = relativeId;

  int idLength = relativeId.length();
  // Figure out how many colons
  int colonCount = 0;
  while (colonCount < idLength)
  {
    if (relativeId.charAt(colonCount) != NamingContainer.SEPARATOR_CHAR)
      break;
    colonCount++;
  }

  // colonCount == 0: fully relative
  // colonCount == 1: absolute (still normal findComponent syntax)
  // colonCount > 1: for each extra colon after 1, go up a naming container
  // (to the view root, if naming containers run out)
  if (colonCount > 1)
  {
    relativeId = relativeId.substring(colonCount);
    for (int j = 1; j < colonCount; j++)
    {
      while (from.getParent() != null)
      {
        from = from.getParent();
        if (from instanceof NamingContainer)
          break;
      }
    }
  }

  UIComponent found = from.findComponent(relativeId);
  if (found != null)
  {
    _LOG.warning("DEPRECATED_RELATIVE_ID_SYNTAX", 
      new Object[] {originalRelativeId, originalFrom});
  }
  return found;
}
 
Example 19
Project: myfaces-trinidad   File: UIXComponentBase.java   Source Code and License Vote up 4 votes
/**
 * Calculates the clientId for the component
 */
private String _calculateClientId(FacesContext context)
{
  // the clientId is always at least the id of the current component
  // our implementation of getId() guarantees that this always
  // returns a non-null value
  String clientId = getId();

  // Search for an ancestor that is a naming container
  UIComponent lastParent = null;
  UIComponent currParent = getParent();

  while (true)
  {
    // prepend the NamingContainer portion of the id
    if (currParent instanceof NamingContainer)
    {
      String contClientId;

      // Pass additional context information to naming containers which extend UIXComponent:
      if (currParent instanceof UIXComponent)
        contClientId = ((UIXComponent)currParent).getContainerClientId(context, this);
      else
        contClientId = currParent.getContainerClientId(context);

      StringBuilder bld = __getSharedStringBuilder();
      bld.append(contClientId).append(NamingContainer.SEPARATOR_CHAR).append(clientId);
      clientId = bld.toString();
      break;
    }
    else if (currParent == null)
    {
      if (lastParent instanceof UIViewRoot)
      {
        // we got to the top of the component tree, so done looping
        break;
      }
      else
      {
        // the component isn't in the component tree, which can cause the cached client id to be wrong.

        // =-= btsulliv see Trinidad-2374.  We can't do this right now because of a couple of bogus Trinidad Renderers
        break;

        /*
        throw new IllegalStateException("Calling getClientId() on component " + this +
                                        " when it is not in the component tree.  Ancestor path:" +_getAncestorPath());
        */
      }
    }

    lastParent = currParent;
    currParent = lastParent.getParent();
  }

  Renderer renderer = getRenderer(context);
  if (null != renderer)
    clientId = renderer.convertClientId(context, clientId);

  return clientId;
}
 
Example 20
Project: myfaces-trinidad   File: UIXComponentBase.java   Source Code and License Vote up 4 votes
@Override
public UIComponent findComponent(String id)
{
  if (id == null)
    throw new NullPointerException();

  if ("".equals(id))
    throw new IllegalArgumentException();

  UIComponent from = this;
  // If it starts with the separator character, it's
  // an absolute path: start at the top
  if (id.charAt(0) == NamingContainer.SEPARATOR_CHAR)
  {
    id = id.substring(1);
    while (from.getParent() != null)
      from = from.getParent();
  }
  // If it's a NamingContainer, start right here
  else if (this instanceof NamingContainer)
  {
    ;
  }
  // Otherwise, go up to look for NamingContainer (or the top)
  else
  {
    while (from.getParent() != null)
    {
      from = from.getParent();
      if (from instanceof NamingContainer)
        break;
    }
  }

  // Evaluate each part of the expression
  String searchId;
  int separatorIndex = id.indexOf(NamingContainer.SEPARATOR_CHAR);
  if (separatorIndex < 0)
    searchId = id;
  else
    searchId = id.substring(0, separatorIndex);

  if (searchId.equals(from.getId()))
  {
    // Don't need to look inside if we're already there
    ;
  }
  else
  {
    from = _findInsideOf(from, searchId);
  }

  // Final ID:  break, and return whatever we've found
  if (separatorIndex < 0)
  {
    return from;
  }
  // Just an intermediate step.  Make sure we're at a NamingContainer,
  // and then ask it to find the rest of the expression.
  else
  {
    if (from == null)
      return null;

    if (!(from instanceof NamingContainer))
      throw new IllegalArgumentException();
    return from.findComponent(id.substring(separatorIndex + 1));
  }
}