Exemple #1
0
 static Renderer getRenderer(final FacesContext context, final UIComponent component) {
   final String rendererType = component.getRendererType();
   if (rendererType == null) {
     return null;
   }
   final RenderKit renderKit = RenderKitUtil.getRenderKit(context);
   return renderKit.getRenderer(component.getFamily(), rendererType);
 }
  public static String getWidgetVar(String id) {
    UIComponent component = findComponent(FacesContext.getCurrentInstance().getViewRoot(), id);
    Renderer renderer =
        FacesContext.getCurrentInstance()
            .getRenderKit()
            .getRenderer(component.getFamily(), component.getRendererType());

    if (component == null) throw new FacesException("Cannot find component " + id + " in view.");
    else if (!(renderer instanceof CoreRenderer))
      throw new FacesException("Component with id " + id + " is not derived from CoreRenderer.");

    return ((CoreRenderer) renderer).resolveWidgetVar(component);
  }
 /**
  * After nornal setting <code>parent</code> property in case of created component set Ajax
  * properties for parent.
  *
  * @see javax.faces.component.UIComponentBase#setParent(javax.faces.component.UIComponent)
  */
 public void setParent(UIComponent parent) {
   super.setParent(parent);
   if (null != parent && parent.getFamily() != null) {
     if (log.isDebugEnabled()) {
       log.debug(Messages.getMessage(Messages.CALLED_SET_PARENT, parent.getClass().getName()));
     }
     // TODO If this comopnent configured, set properties for parent
     // component.
     // NEW created component have parent, restored view - null in My
     // faces.
     // and SUN RI not call at restore saved view.
     // In other case - set in restoreState method.
     // if (parent.getParent() != null)
     {
       if (log.isDebugEnabled()) {
         log.debug(Messages.getMessage(Messages.DETECT_NEW_COMPONENT));
       }
       setParentProperties(parent);
     }
   }
 }
  /**
   * Return a List of {@link javax.faces.model.SelectItem} instances representing the available
   * options for this component, assembled from the set of {@link
   * javax.faces.component.UISelectItem} and/or {@link javax.faces.component.UISelectItems}
   * components that are direct children of this component. If there are no such children, an empty
   * <code>Iterator</code> is returned.
   *
   * @param context The {@link javax.faces.context.FacesContext} for the current request. If null,
   *     the UISelectItems behavior will not work.
   * @param component the component
   * @throws IllegalArgumentException if <code>context</code> is <code>null</code>
   * @return a List of the select items for the specified component
   */
  public static List<SelectItem> getSelectItems(FacesContext context, UIComponent component) {

    if (context == null) {
      throw new IllegalArgumentException(
          MessageUtils.getExceptionMessageString(
              MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "context"));
    }

    ArrayList<SelectItem> list = new ArrayList<SelectItem>();
    for (UIComponent kid : component.getChildren()) {
      if (kid instanceof UISelectItem) {
        UISelectItem item = (UISelectItem) kid;
        Object value = item.getValue();

        if (value == null) {
          list.add(
              new SelectItem(
                  item.getItemValue(),
                  item.getItemLabel(),
                  item.getItemDescription(),
                  item.isItemDisabled(),
                  item.isItemEscaped()));
        } else if (value instanceof SelectItem) {
          list.add((SelectItem) value);
        } else {
          throw new IllegalArgumentException(
              MessageUtils.getExceptionMessageString(
                  MessageUtils.VALUE_NOT_SELECT_ITEM_ID,
                  component.getId(),
                  value.getClass().getName()));
        }
      } else if (kid instanceof UISelectItems) {
        Object value = ((UISelectItems) kid).getValue();
        if (value instanceof SelectItem) {
          list.add((SelectItem) value);
        } else if (value instanceof SelectItem[]) {
          SelectItem[] items = (SelectItem[]) value;
          // we manually copy the elements so that the list is
          // modifiable.  Arrays.asList() returns a non-mutable
          // list.
          //noinspection ManualArrayToCollectionCopy
          for (SelectItem item : items) {
            list.add(item);
          }
        } else if (value instanceof Collection) {
          for (Object element : ((Collection) value)) {
            if (SelectItem.class.isInstance(element)) {
              list.add((SelectItem) element);
            } else {
              throw new IllegalArgumentException(
                  MessageUtils.getExceptionMessageString(
                      MessageUtils.VALUE_NOT_SELECT_ITEM_ID,
                      component.getId(),
                      value.getClass().getName()));
            }
          }
        } else if (value instanceof Map) {
          Map optionMap = (Map) value;
          for (Object o : optionMap.entrySet()) {
            Entry entry = (Entry) o;
            Object key = entry.getKey();
            Object val = entry.getValue();
            if (key == null || val == null) {
              continue;
            }
            list.add(new SelectItem(val, key.toString()));
          }
        } else {
          throw new IllegalArgumentException(
              MessageUtils.getExceptionMessageString(
                  MessageUtils.CHILD_NOT_OF_EXPECTED_TYPE_ID,
                  "UISelectItem/UISelectItems",
                  component.getFamily(),
                  component.getId(),
                  value != null ? value.getClass().getName() : "null"));
        }
      }
    }
    return (list);
  }
 public static boolean isLiteralText(UIComponent component) {
   return component.getFamily().equalsIgnoreCase("facelets.LiteralText");
 }