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

The following are Jave code examples for showing how to use getId() 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: DebugResponseWriter.java   Source Code and License Vote up 6 votes
@Override
public void startElement(String name, UIComponent component) throws IOException
 {
   if ((component != null) && (_lastComponentStarted != component))
   {
     String componentAsString = component.getFamily();
     String id = component.getId();
     if (id != null)
     {
       componentAsString = componentAsString + "[\"" + id + "\"]";
     }

     writeComment("Start: " + componentAsString);

     _lastComponentStarted = component;
   }

   super.startElement(name, component);
   _inElement = true;
   _elementStack.push(name);
   _attributes.clear();
 }
 
Example 2
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 3
Project: myfaces-trinidad   File: CoreRenderer.java   Source Code and License Vote up 6 votes
/**
 * Returns true if the component should render an ID.  Components
 * that deliver events should always return "true".
 */
// TODO Is this a bottleneck?  If so, optimize!
protected boolean shouldRenderId(
  FacesContext context,
  UIComponent component)
{
  String id = component.getId();

  // Otherwise, if ID isn't set, don't bother
  if (id == null)
    return false;

  // ... or if the ID was generated, don't bother
  if (id.startsWith(UIViewRoot.UNIQUE_ID_PREFIX))
    return false;

  return true;
}
 
Example 4
Project: oscm   File: MessageListener.java   Source Code and License Vote up 5 votes
/**
 * Tries to get the id of a parent section if existing.
 * 
 * @param uiComponent
 *            the component to check the parents for
 * @return the id of the section if found or <code>null</code>
 */
private String getSectionId(UIComponent uiComponent) {
    UIComponent parent = uiComponent.getParent();
    if (parent instanceof UIViewRoot) {
        return null;
    }
    if (parent instanceof UITogglePanel) {
        return parent.getId();
    }
    return getSectionId(parent);
}
 
Example 5
Project: myfaces-trinidad   File: TrinidadComponentHandler.java   Source Code and License Vote up 5 votes
@Override
public void onComponentPopulated(FaceletContext context,
                                   UIComponent component,
                                   UIComponent parent)
{
  assert (_markInitialState != null);

  if ((component instanceof UIXComponent) &&
      (_markInitialState == Boolean.TRUE))
  {
    if (component.getId() == null)
      component.setId(context.generateUniqueId(UIViewRoot.UNIQUE_ID_PREFIX));
    
    PhaseId phase = context.getFacesContext().getCurrentPhaseId();
    
    // In jsf2 markInitialState will be called by the framework during restore view, 
    // and in fact the framework should always be the one
    // calling markInitialState, but it doesn't always do that in render response, see
    // http://java.net/jira/browse/JAVASERVERFACES-2285
    // Also don't call markInitialState unless initialStateMarked returns false, otherwise
    // any deltas previously saved may get blown away.
    if (PhaseId.RENDER_RESPONSE.equals(phase) && !component.initialStateMarked())
    {
      component.markInitialState();
    }
  }
}
 
Example 6
Project: myfaces-trinidad   File: Structure.java   Source Code and License Vote up 5 votes
/**
 * Create the structure of an existing component.
 */
public Structure(UIComponent component)
{
  _class = component.getClass().getName();
  _id = component.getId();
  _facets = _getFacets(component);
  _children = _getChildren(component);
}
 
Example 7
Project: myfaces-trinidad   File: ComponentRefTag.java   Source Code and License Vote up 5 votes
/**
 * Indicate that a facet was moved. The region component must keep track
 * of all facets that were moved so that they may be restored before the
 * jsf jsp tag framework notices. If the jsf jsp tag framework notices
 * that the facets are missing, it will recreate them, and that is undesireable
 * as that will break the "binding" attribute support
 * (on the facet) and will have performance consequences.
 * @param region the component that the facet was moved from.
 * @param facet the facet that was moved.
 * @param component the component that the facet was moved to.
 */
@SuppressWarnings("unchecked")
public static void addRelocatedFacet(
  UIComponent region,
  String facet,
  UIComponent component)
{
  Map<String, RelocatedFacet> map = 
    (Map<String, RelocatedFacet>) region.getAttributes().get(_RELOCATED_FACETS_ATTRIBUTE);
  if (map == null)
  {
    map = new HashMap<String, RelocatedFacet>(3);
    region.getAttributes().put(_RELOCATED_FACETS_ATTRIBUTE, map);
  }

  // compute the ID to retrieve this relocated facet later.
  // this is because we don't always have the relocated facet instance
  // For example, after Serialization, the relocated facet instance is lost:
  StringBuffer findId = new StringBuffer(component.getId());
  for(UIComponent c = component.getParent(); c!=region; c = c.getParent())
  {
    if (c instanceof NamingContainer)
    {
      findId.insert(0, NamingContainer.SEPARATOR_CHAR);
      findId.insert(0, c.getId());
    }
  }

  map.put(facet, new RelocatedFacet(component, findId.toString()));
}
 
Example 8
Project: myfaces-trinidad   File: DateFieldAsRenderer.java   Source Code and License Vote up 5 votes
@SuppressWarnings("unchecked")
private void _addChildren(FacesContext context, UIComponent component)
{
  // If the components are already there, bail.
  if (component.getFacet("month") != null)
    return;

  String id = component.getId();
  if (id == null)
  {
    id = context.getViewRoot().createUniqueId();
    component.setId(id);
  }

  Map<String, UIComponent> facets = component.getFacets();
  facets.clear();

  Date value = (Date) ((EditableValueHolder) component).getValue();
  Calendar calendar = null;
  if(value != null)
  {
    calendar = Calendar.getInstance();
    calendar.setLenient(true);
    calendar.setTime(value);
  }

  CoreInputText month = _createTwoDigitInput(context);
  month.setShortDesc("Month");
  month.setId(id + "_month");

  LongRangeValidator monthRange = _createLongRangeValidator(context);
  monthRange.setMinimum(1);
  monthRange.setMaximum(12);
  month.addValidator(monthRange);
  if (value != null)
    month.setValue(new Integer(calendar.get(Calendar.MONTH) + 1));
  facets.put("month", month);

  CoreInputText day = _createTwoDigitInput(context);
  day.setShortDesc("Day");
  day.setId(id + "_day");
  LongRangeValidator dayRange = _createLongRangeValidator(context);
  dayRange.setMinimum(1);
  dayRange.setMaximum(31);
  day.addValidator(dayRange);
  if (value != null)
    day.setValue(new Integer(calendar.get(Calendar.DAY_OF_MONTH)));
  facets.put("day", day);

  CoreInputText year = _createTwoDigitInput(context);
  year.setShortDesc("Year");
  year.setId(id + "_year");
  if (value != null)
  {
    int yearValue = calendar.get(Calendar.YEAR) - 1900;
    if (yearValue >= 100)
      yearValue -= 100;
    year.setValue(new Integer(yearValue));
  }

  facets.put("year", year);
}
 
Example 9
Project: myfaces-trinidad   File: ComponentReference.java   Source Code and License Vote up 5 votes
protected static String calculateScopedId(
  UIComponent component,
  String      componentId)
{
  if (componentId == null)
    throw new IllegalStateException("Can't create a ComponentReference for component " +
                                    component +
                                    " no id");    
  int scopedIdLength = componentId.length();

  List<String> scopedIdList = new ArrayList<String>();
 
  // determine how many characters we need to store the scopedId.  We skip the component itself,
  // because we have already accounted for its id
  UIComponent currAncestor = component.getParent();
  
  while (currAncestor != null)
  {
    // add the sizes of all of the NamingContainer ancestors, plus 1 for each NamingContainer separator
    if (currAncestor instanceof NamingContainer)
    {
      String currId = currAncestor.getId();
      scopedIdLength += currId.length() + 1;
    
      // add the NamingContainer to the list of NamingContainers
      scopedIdList.add(currId);
    }
    
    currAncestor = currAncestor.getParent();
  }
  
  // now append all of the NamingContaintes
  return _createScopedId(scopedIdLength, scopedIdList, componentId);
}
 
Example 10
Project: myfaces-trinidad   File: ComponentReference.java   Source Code and License Vote up 5 votes
protected String getScopedId()
{
  String scopedId = _scopedId;
  
  // we have no scopedId, so calculate the scopedId and finish initializing
  // the DeferredComponentReference
  if (scopedId == null)
  {
    UIComponent component = _component;
    
    // need to check that component isn't null because of possible race condition if this
    // method is called from different threads.  In that case, scopedId will have been filled
    // in, so we can return it
    if (component != null)
    {
      String componentId = component.getId();
                
      scopedId = calculateScopedId(component, componentId);
      _scopedId = scopedId;
      _componentId = componentId;
      
      // store away our component path while we can efficiently calculate it
      setComponentPath(calculateComponentPath(component));
      
      _component = null;
    }
    else
    {
      scopedId = _scopedId;
    }
  }
  
  return scopedId;
}
 
Example 11
Project: myfaces-trinidad   File: ComponentUtils.java   Source Code and License Vote up 5 votes
/**
 * Returns scoped id for a component
 * @param targetComponent The component for which the scoped id needs to be
 * determined.
 * @param baseComponent The component relative to which the scoped id for the
 * targetComponent needs to be determined.
 * @param isLogical true if a logical scoped id (the id in the context of the document where targetComponent was defined)
 * should be returned, false otherwise
 * @return The scoped id for target component. Returns null if the supplied
 * targetComponent was null or did not have an id.
 */
private static String _getScopedIdForComponentImpl(
  UIComponent targetComponent,
  UIComponent baseComponent,
  boolean isLogical)
{
  String targetComponentId = targetComponent.getId();
  
  if (targetComponent == null || 
      targetComponentId == null ||
      targetComponentId.length() == 0)
    return null;
  
  // Optimize when both arguments are the same
  if (targetComponent.equals(baseComponent))
    return targetComponentId;

  StringBuilder builder = new StringBuilder(100);
  
  // Add a leading ':' if the baseComponent is the view root
  if (baseComponent instanceof UIViewRoot)
    builder.append(NamingContainer.SEPARATOR_CHAR);

  _buildScopedId(targetComponent, baseComponent, builder, isLogical);
  
  return builder.toString();
}
 
Example 12
Project: myfaces-trinidad   File: UIXComponent.java   Source Code and License Vote up 5 votes
/**
 * Default implementation of clearing the cached client ids
 */
private static void _clearCachedClientIds(UIComponent component)
{
  // clear this component
  String id = component.getId();
  component.setId(id);

  // clear the children
  Iterator<UIComponent> allChildren = component.getFacetsAndChildren();

  while (allChildren.hasNext())
  {
    clearCachedClientIds(allChildren.next());
  }
}
 
Example 13
Project: myfaces-trinidad   File: UIXCollection.java   Source Code and License Vote up 5 votes
/**
   * Restores the state of all the stamps of this component.
   * This method should be called after the currency of this component
   * changes. This method gets all the stamps using {@link #getStamps} and
   * restores their states by calling
   * {@link #restoreStampState}.
   */
  private void _restoreStampState()
  {
    StampState stampState = _getStampState();
    Map<String, String> idToIndexMap = _getIdToIndexMap();
    FacesContext context = getFacesContext();
    Object currencyObj = getRowKey();
    for(UIComponent stamp : getStamps())
    {
//      String stampId = stamp.getId();
      // TODO
      // temporarily use position. later we need to use ID's to access
      // stamp state everywhere, and special case NamingContainers:
      String compId = stamp.getId();
      String stampId = idToIndexMap.get(compId);
      Object state = stampState.get(currencyObj, stampId);
      if (state == null)
      {
        Object iniStateObj = _getCurrencyKeyForInitialStampState();
        state = stampState.get(iniStateObj, stampId);
        /*
        if (state==null)
        {
          _LOG.severe("NO_INITIAL_STAMP_STATE", new Object[]{currencyObj,iniStateObj,stampId});
          continue;
        }*/
      }
      restoreStampState(context, stamp, state);
    }
  }
 
Example 14
Project: tap17-muggl-javaee   File: AreaRenderer.java   Source Code and License Vote up 5 votes
/**
 * <p>
 * Return the calculated name for the hidden input field.</p>
 *
 * @param context Context for the current request
 * @param component Component we are rendering
 */
private String getName(FacesContext context, UIComponent component) {
    while (component != null) {
        if (component instanceof MapComponent) {
            return (component.getId() + "_current");
        }

        component = component.getParent();
    }

    throw new IllegalArgumentException();
}
 
Example 15
Project: myfaces-trinidad   File: UIXCollection.java   Source Code and License Vote up 4 votes
/**
   * Saves the state of all the stamps of this component.
   * This method should be called before the rowData of this component
   * changes. This method gets all the stamps using {@link #getStamps} and
   * saves their states by calling {@link #saveStampState}.
   */
  private void _saveStampState()
  {
    // Never read and created by _getStampState
    //InternalState iState = _getInternalState(true);

    StampState stampState = _getStampState();
    Map<String, String> idToIndexMap = _getIdToIndexMap();
    FacesContext context = getFacesContext();
    Object currencyObj = getRowKey();

    // Note: even though the currencyObj may be null, we still need to save the state. The reason
    // is that the code does not clear out the state when it is saved, instead, the un-stamped
    // state is saved. Once the row key is set back to null, this un-stamped state is restored
    // onto the children components. This restoration allows editable value holders, show detail
    // items and nested UIXCollections to clear their state.
    // For nested UIXCollections, this un-stamped state is required to set the nested collection's
    // _state (internal state containing the stamp state) to null when not on a row key. Without
    // that call, the nested UIXCollection components would end up sharing the same stamp state
    // across parent rows.

    for (UIComponent stamp : getStamps())
    {
      Object state = saveStampState(context, stamp);
//      String stampId = stamp.getId();
      // TODO
      // temporarily use position. later we need to use ID's to access
      // stamp state everywhere, and special case NamingContainers:
      String compId = stamp.getId();
      String stampId = idToIndexMap.get(compId);
      if (stampId == null)
      {
        stampId = String.valueOf(idToIndexMap.size());
        idToIndexMap.put(compId, stampId);
      }
      stampState.put(currencyObj, stampId, state);
      if (_LOG.isFinest())
        _LOG.finest("saving stamp state for currencyObject:"+currencyObj+
          " and stampId:"+stampId);
    }
  }
 
Example 16
Project: myfaces-trinidad   File: AddChildComponentChange.java   Source Code and License Vote up 4 votes
/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
@Override
public void changeComponent(UIComponent uiComponent)
{
  UIComponent child = getComponent();
  
  if (child == null)
    return;
    
  String newChildId = child.getId();
  List<UIComponent> children = uiComponent.getChildren();
  
  // If there were to be a child already with the ID same as the to-be-added child, it might have
  //  been added from previous change application, and further customizations might have happened
  //  on them. We just want to warn, abort the child addition, and not alter the component tree.
  UIComponent duplicateChild = ChangeUtils.getChildForId(uiComponent, newChildId);

  if (duplicateChild != null)
  {
    _LOG.info("ATTEMPT_ADD_CHILD_WITH_DUPLICATE_ID", newChildId);
    return;
  }
  
  if (_insertBeforeId == null)
  {
    // append the child
    children.add(child); 
  }
  else
  {
    int index = ChangeUtils.getChildIndexForId(uiComponent, _insertBeforeId);
    if(index == -1)
    {
      children.add(child);
    }
    else
    {
      children.add(index, child);
    }
  }
}
 
Example 17
Project: tap17-muggl-javaee   File: MapRenderer.java   Source Code and License Vote up 2 votes
/**
 * <p>Return the calculated name for the hidden input field.</p>
 *
 * @param context <code>FacesContext</code> for the current request
 * @param component <code>UIComponent</code> we are rendering
 */
private String getName(FacesContext context, UIComponent component) {
    return (component.getId() + "_current");
}