protected void resetComponent(UIComponent comp, boolean recursive) {
   if (comp == null) {
     return;
   }
   if (comp instanceof ResettableComponent) {
     ((ResettableComponent) comp).resetCachedModel();
   } else {
     if (comp instanceof EditableValueHolder) {
       // reset submitted value
       ((EditableValueHolder) comp).setSubmittedValue(null);
     }
     if (comp instanceof ValueHolder) {
       // reset local value, only if there's a value expression
       // binding
       ValueExpression ve = comp.getValueExpression("value");
       if (ve != null) {
         ValueHolder vo = (ValueHolder) comp;
         vo.setValue(null);
         if (comp instanceof EditableValueHolder) {
           ((EditableValueHolder) comp).setLocalValueSet(false);
         }
       }
     }
   }
   if (recursive) {
     List<UIComponent> children = comp.getChildren();
     if (children != null && !children.isEmpty()) {
       for (UIComponent child : children) {
         resetComponent(child, recursive);
       }
     }
   }
 }
Пример #2
0
 public void applyAttachedObject(FacesContext context, UIComponent parent) {
   FaceletContext ctx =
       (FaceletContext) context.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
   // cast to a ValueHolder
   ValueHolder vh = (ValueHolder) parent;
   ValueExpression ve = null;
   Converter c = null;
   if (this.binding != null) {
     ve = this.binding.getValueExpression(ctx, Converter.class);
     c = (Converter) ve.getValue(ctx);
   }
   if (c == null) {
     c = this.createConverter(ctx);
     if (ve != null) {
       ve.setValue(ctx, c);
     }
   }
   if (c == null) {
     throw new TagException(this.tag, "No Converter was created");
   }
   this.setAttributes(ctx, c);
   vh.setConverter(c);
   Object lv = vh.getLocalValue();
   FacesContext faces = ctx.getFacesContext();
   if (lv instanceof String) {
     vh.setValue(c.getAsObject(faces, parent, (String) lv));
   }
 }
Пример #3
0
  /**
   * Algorithm works as follows; - If it's an input component, submitted value is checked first
   * since it'd be the value to be used in case validation errors terminates jsf lifecycle - Finally
   * the value of the component is retrieved from backing bean and if there's a converter, converted
   * value is returned
   *
   * <p>- If the component is not a value holder, toString of component is used to support Facelets
   * UIInstructions.
   *
   * @param facesContext FacesContext instance
   * @param component UIComponent instance whose value will be returned
   * @return End text
   */
  public static String getStringValueToRender(FacesContext facesContext, UIComponent component) {
    if (component instanceof ValueHolder) {

      if (component instanceof EditableValueHolder) {
        Object submittedValue = ((EditableValueHolder) component).getSubmittedValue();
        if (submittedValue != null) {
          return submittedValue.toString();
        }
      }

      ValueHolder valueHolder = (ValueHolder) component;
      Object value = valueHolder.getValue();
      if (value == null) return "";

      // first ask the converter
      if (valueHolder.getConverter() != null) {
        return valueHolder.getConverter().getAsString(facesContext, component, value);
      }
      // Try to guess
      else {
        ValueExpression expr = component.getValueExpression("value");
        if (expr != null) {
          Class<?> valueType = expr.getType(facesContext.getELContext());
          if (valueType != null) {
            Converter converterForType = facesContext.getApplication().createConverter(valueType);

            if (converterForType != null)
              return converterForType.getAsString(facesContext, component, value);
          }
        }
      }

      // No converter found just return the value as string
      return value.toString();
    } else {
      // This would get the plain texts on UIInstructions when using Facelets
      String value = component.toString();

      if (value != null) return value.trim();
      else return "";
    }
  }
Пример #4
0
  /**
   * Resolves the end text to render by using a specified value
   *
   * @param facesContext FacesContext instance
   * @param component UIComponent instance whose value will be returned
   * @return End text
   */
  public static String getStringValueToRender(
      FacesContext facesContext, UIComponent component, Object value) {
    if (value == null) return null;

    ValueHolder valueHolder = (ValueHolder) component;

    Converter converter = valueHolder.getConverter();
    if (converter != null) {
      return converter.getAsString(facesContext, component, value);
    } else {
      ValueExpression expr = component.getValueExpression("value");
      if (expr != null) {
        Class<?> valueType = expr.getType(facesContext.getELContext());
        Converter converterForType = facesContext.getApplication().createConverter(valueType);

        if (converterForType != null)
          return converterForType.getAsString(facesContext, component, value);
      }
    }

    return value.toString();
  }
  Object getValue() {
    Map<UIData, Integer> savedRowIndexes = new HashMap<UIData, Integer>();

    // save current row and set row needed to get value
    for (Iterator<UIData> i = reverse(uiDataRowMap.keySet().iterator()); i.hasNext(); ) {
      UIData uiData = i.next();
      savedRowIndexes.put(uiData, uiData.getRowIndex());
      uiData.setRowIndex(uiDataRowMap.get(uiData));
    }

    Object value = component.getValue();

    // restore rows
    for (Iterator<UIData> i = reverse(savedRowIndexes.keySet().iterator()); i.hasNext(); ) {
      UIData uiData = i.next();
      uiData.setRowIndex(savedRowIndexes.get(uiData));
    }

    return value;
  }
Пример #6
0
  /**
   * populate the argument component with values, being sensitive to the possible multi-nature of
   * the values, and to the type of the values.
   *
   * @param context the <code>FacesContext</code> for the current request
   * @param component the <code>UIComponent</code> to populate
   * @param componentType the component type
   * @param value the value
   * @param valueType the value type
   */
  private void populateComponentWithValue(
      FacesContext context,
      UIComponent component,
      String componentType,
      String value,
      String valueType) {
    Application application = context.getApplication();
    Converter converter = null;

    // if we need a converter, and can have a converter
    if (!"java.lang.String".equals(valueType) && component instanceof ValueHolder) {
      // if so create it,
      try {
        converter = application.createConverter(CarStore.loadClass(valueType, this));
        // add it to our component,
        ((ValueHolder) component).setConverter(converter);
      } catch (ClassNotFoundException cne) {
        FacesMessage errMsg = MessageFactory.getMessage(CONVERTER_ERROR_MESSAGE_ID, valueType);
        throw new IllegalStateException(errMsg.getSummary());
      }
    }

    // if this component is a SelectOne or SelectMany, take special action
    if (isMultiValue(componentType)) {
      // create a UISelectItems instance
      UISelectItems items = new UISelectItems();
      items.setValue(parseStringIntoArrayList(value, converter));
      // add it to the component
      component.getChildren().add(items);
    } else {
      // we have a single value
      if (null != converter) {
        component.getAttributes().put("value", converter.getAsObject(context, component, value));
      } else {
        component.getAttributes().put("value", value);
      }
    }
  }
Пример #7
0
 public void applyMetadata(FaceletContext ctx, Object instance) {
   ((ValueHolder) instance)
       .setConverter(ctx.getFacesContext().getApplication().createConverter(this.converterId));
 }
  @SuppressWarnings("deprecation")
  public UIComponent processWidget(
      UIComponent component,
      String elementName,
      Map<String, String> attributes,
      UIMetawidget metawidget) {

    // Actions don't get converters

    if (ACTION.equals(elementName)) {
      return component;
    }

    // Recurse into stubs...

    if (component instanceof UIStub) {
      // ...whose children have the same value binding as us...
      //
      // (this is important because choice of Converter is based off the attributes Map, and
      // if the value binding is different then all bets are off as to the accuracy of the
      // attributes)

      javax.faces.el.ValueBinding valueBinding = component.getValueBinding("value");

      if (valueBinding != null) {
        String expressionString = valueBinding.getExpressionString();

        List<UIComponent> children = component.getChildren();

        for (UIComponent componentChild : children) {
          javax.faces.el.ValueBinding childValueBinding = componentChild.getValueBinding("value");

          if (childValueBinding == null) {
            continue;
          }

          if (!expressionString.equals(childValueBinding.getExpressionString())) {
            continue;
          }

          // ...and apply the Converter to them

          processWidget(componentChild, elementName, attributes, metawidget);
        }
      }

      return component;
    }

    // Ignore components that cannot have Converters

    if (!(component instanceof ValueHolder)) {
      return component;
    }

    // Defer evaluation of EL-based converters, else we will fail trying to store/restore them
    // from the ViewState

    ValueHolder valueHolder = (ValueHolder) component;
    String converterId = attributes.get(FACES_CONVERTER);

    if (converterId != null && FacesUtils.isExpression(converterId)) {

      FacesContext context = FacesContext.getCurrentInstance();
      component.setValueBinding(
          "converter", context.getApplication().createValueBinding(converterId));
      return component;
    }

    // Standard Converter

    valueHolder.setConverter(getConverter(valueHolder, attributes));

    return component;
  }
  public Converter getConverter(ValueHolder valueHolder, Map<String, String> attributes) {

    // Use existing Converter (if any)

    Converter converter = valueHolder.getConverter();

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

    // Create from id

    FacesContext context = FacesContext.getCurrentInstance();
    String converterId = attributes.get(FACES_CONVERTER);

    if (converterId != null) {

      converter = context.getApplication().createConverter(converterId);

    } else if (valueHolder instanceof UISelectOne || valueHolder instanceof UISelectMany) {

      // Create from parameterized type (eg. a Date converter for List<Date>)

      String parameterizedType = WidgetBuilderUtils.getComponentType(attributes);

      if (parameterizedType != null) {
        Class<?> parameterizedClass = ClassUtils.niceForName(parameterizedType);

        // The parameterized type might be null, or might not be concrete
        // enough to be instantiatable (eg. List<? extends Foo>)

        if (parameterizedClass != null) {
          converter = context.getApplication().createConverter(parameterizedClass);
        }
      }

    } else {

      // Create implicit converters
      //
      // JSF does not appear to implicitly hook up DateTimeConverters without either an
      // explicit f:convertDateTime tag or a registered java.util.Date converter. Adding one
      // fixes both POSTback and display of read-only dates (otherwise JSF uses Date.toString)
      //
      // JSF *does* appear to implicitly hook up NumberConverters.

      String type = attributes.get(TYPE);

      if (type != null) {

        Class<?> clazz = ClassUtils.niceForName(type);

        if (clazz != null) {

          if (clazz.isAssignableFrom(Date.class)) {
            converter = getDateTimeConverter(converter);
          }
        }
      }
    }

    // Support for DateTimeConverter

    if (attributes.containsKey(DATE_STYLE)) {
      converter = getDateTimeConverter(converter);
      ((DateTimeConverter) converter).setDateStyle(attributes.get(DATE_STYLE));
    }

    if (attributes.containsKey(DATETIME_PATTERN)) {
      converter = getDateTimeConverter(converter);
      ((DateTimeConverter) converter).setPattern(attributes.get(DATETIME_PATTERN));
    }

    if (attributes.containsKey(TIME_STYLE)) {
      converter = getDateTimeConverter(converter);
      ((DateTimeConverter) converter).setTimeStyle(attributes.get(TIME_STYLE));
    }

    if (attributes.containsKey(TIME_ZONE)) {
      converter = getDateTimeConverter(converter);
      ((DateTimeConverter) converter).setTimeZone(TimeZone.getTimeZone(attributes.get(TIME_ZONE)));
    }

    if (attributes.containsKey(DATETIME_TYPE)) {
      converter = getDateTimeConverter(converter);
      ((DateTimeConverter) converter).setType(attributes.get(DATETIME_TYPE));
    }

    // Support for NumberConverter

    if (attributes.containsKey(CURRENCY_CODE)) {
      converter = getNumberConverter(converter);
      ((NumberConverter) converter).setCurrencyCode(attributes.get(CURRENCY_CODE));
    }

    if (attributes.containsKey(CURRENCY_SYMBOL)) {
      converter = getNumberConverter(converter);
      ((NumberConverter) converter).setCurrencySymbol(attributes.get(CURRENCY_SYMBOL));
    }

    if (attributes.containsKey(NUMBER_USES_GROUPING_SEPARATORS)) {
      converter = getNumberConverter(converter);
      ((NumberConverter) converter)
          .setGroupingUsed(Boolean.parseBoolean(attributes.get(NUMBER_USES_GROUPING_SEPARATORS)));
    }

    if (attributes.containsKey(MINIMUM_INTEGER_DIGITS)) {
      converter = getNumberConverter(converter);
      ((NumberConverter) converter)
          .setMinIntegerDigits(Integer.parseInt(attributes.get(MINIMUM_INTEGER_DIGITS)));
    }

    if (attributes.containsKey(MAXIMUM_INTEGER_DIGITS)) {
      converter = getNumberConverter(converter);
      ((NumberConverter) converter)
          .setMaxIntegerDigits(Integer.parseInt(attributes.get(MAXIMUM_INTEGER_DIGITS)));
    }

    if (attributes.containsKey(MINIMUM_FRACTIONAL_DIGITS)) {
      converter = getNumberConverter(converter);
      ((NumberConverter) converter)
          .setMinFractionDigits(Integer.parseInt(attributes.get(MINIMUM_FRACTIONAL_DIGITS)));
    }

    if (attributes.containsKey(MAXIMUM_FRACTIONAL_DIGITS)) {
      converter = getNumberConverter(converter);
      ((NumberConverter) converter)
          .setMaxFractionDigits(Integer.parseInt(attributes.get(MAXIMUM_FRACTIONAL_DIGITS)));
    }

    if (attributes.containsKey(NUMBER_PATTERN)) {
      converter = getNumberConverter(converter);
      ((NumberConverter) converter).setPattern(attributes.get(NUMBER_PATTERN));
    }

    if (attributes.containsKey(NUMBER_TYPE)) {
      converter = getNumberConverter(converter);
      ((NumberConverter) converter).setType(attributes.get(NUMBER_TYPE));
    }

    // Locale (applies to both DateTimeConverter and NumberConverter)

    if (attributes.containsKey(LOCALE)) {
      if (converter instanceof NumberConverter) {
        ((NumberConverter) converter).setLocale(new Locale(attributes.get(LOCALE)));
      } else {
        converter = getDateTimeConverter(converter);
        ((DateTimeConverter) converter).setLocale(new Locale(attributes.get(LOCALE)));
      }
    }

    // Return it

    return converter;
  }