Java Code Examples for javax.faces.convert.Converter#getAsString()

The following examples show how to use javax.faces.convert.Converter#getAsString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SelectMultiMenuRenderer.java    From BootsFaces-OSP with Apache License 2.0 6 votes vote down vote up
/**
 * 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
 *
 * @param context
 *            FacesContext instance
 * @return End text
 */
public Object getValue2Render(FacesContext context, SelectMultiMenu menu) {
	Object sv = menu.getSubmittedValue();
	if (sv != null) {
		return sv;
	}

	Object val = menu.getValue();
	if (val != null) {
		Converter converter = menu.getConverter();

		if (converter != null)
			return converter.getAsString(context, menu, val);
		else
			return val;

	} else {
		// component is a value holder but has no value
		return null;
	}
}
 
Example 2
Source File: SelectOneMenuRenderer.java    From BootsFaces-OSP with Apache License 2.0 6 votes vote down vote up
private String getOptionAsString(FacesContext context, SelectOneMenu menu, Object value, Converter converter)
		throws ConverterException {

	if (converter == null) {
		if (value == null) {
			return "";
		} else if (value instanceof String) {
			return (String) value;
		} else {
			Converter implicitConverter = findImplicitConverter(context, menu);

			return implicitConverter == null ? value.toString()
					: implicitConverter.getAsString(context, menu, value);
		}
	} else {
		return converter.getAsString(context, menu, value);
	}
}
 
Example 3
Source File: SketchPadRenderer.java    From ctsms with GNU Lesser General Public License v2.1 5 votes vote down vote up
private Object getValue(FacesContext context, SketchPad sketchPad) {
	Object submittedValue = sketchPad.getSubmittedValue();
	if (submittedValue != null) {
		return submittedValue;
	}
	Object value = sketchPad.getValue();
	Converter converter = getConverter(context, sketchPad);
	if (converter != null) {
		return converter.getAsString(context, sketchPad, value);
	} else if (value != null) {
		return value.toString();
	} else {
		return "";
	}
}
 
Example 4
Source File: DateTimePickerRenderer.java    From BootsFaces-OSP with Apache License 2.0 5 votes vote down vote up
/**
 * Get date in string format
 * @param fc The FacesContext
 * @param dtp the DateTimePicker component
 * @param value The date to display
 * @param javaFormatString The format string as defined by the SimpleDateFormat syntax
 * @param locale The locale
 * @return null if the value is null.
 */
public static String getDateAsString(FacesContext fc, DateTimePicker dtp, Object value, String javaFormatString, Locale locale) {
	if (value == null) {
		return null;
	}

	Converter converter = dtp.getConverter();
	return  converter == null ?
			getInternalDateAsString(value, javaFormatString, locale)
			:
			converter.getAsString(fc, dtp, value);

}
 
Example 5
Source File: DojoExtImageSelectRenderer.java    From XPagesExtensionLibrary with Apache License 2.0 5 votes vote down vote up
public String convertValue(FacesContext context, UIComponent component, Converter converter, Object value) throws ConverterException {
    if(value!=null) {
        if(converter==null) {
            Application application = context.getApplication();
            converter = application.createConverter(value.getClass());
        }
        // Format it using the converter if necessary, or just converter it to a simple string
        String strValue = converter!=null ? converter.getAsString(context, component, value) 
                                          : value.toString(); 
        return strValue;
    }

    return "";
}
 
Example 6
Source File: DojoExtLinkSelectRenderer.java    From XPagesExtensionLibrary with Apache License 2.0 5 votes vote down vote up
public String convertValue(FacesContext context, UIComponent component, Converter converter, Object value) throws ConverterException {
    if(value!=null) {
        if(converter==null) {
            Application application = context.getApplication();
            converter = application.createConverter(value.getClass());
        }
        // Format it using the converter if necessary, or just converter it to a simple string
        String strValue = converter!=null ? converter.getAsString(context, component, value) 
                                          : value.toString(); 
        return strValue;
    }

    return "";
}
 
Example 7
Source File: DataSourceIteratorRenderer.java    From XPagesExtensionLibrary with Apache License 2.0 5 votes vote down vote up
protected String formatColumnValue(FacesContext context, UIDataSourceIterator c, ViewDefinition viewDef, ValueColumn vc) throws IOException {
    Object value = getColumnValue(context, c, viewDef, vc);
    if(value!=null) {
        Converter cv = findConverter(context, c, viewDef, vc, value);
        String v = cv!=null ? cv.getAsString(context, c, value) : value.toString();
        return v;
    }
    return null;
}
 
Example 8
Source File: DojoComboBoxRenderer.java    From XPagesExtensionLibrary with Apache License 2.0 5 votes vote down vote up
public String convertValue(FacesContext context, UIComponent component, Converter converter, Object value) throws ConverterException {
    if(value!=null) {
        if(converter==null) {
            Application application = context.getApplication();
            converter = application.createConverter(value.getClass());
        }
        // Format it using the converter if necessary, or just converter it to a simple string
        String strValue = converter!=null ? converter.getAsString(context, component, value) 
                                          : value.toString(); 
        return strValue;
    }

    return "";
}
 
Example 9
Source File: CoreRenderer.java    From BootsFaces-OSP with Apache License 2.0 4 votes vote down vote up
/**
 * 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
 *
 * @param fc
 *            FacesContext instance
 * @param c
 *            UIComponent instance whose value will be returned
 * @return End text
 */
public String getValue2Render(FacesContext fc, UIComponent c) {
	if (c instanceof ValueHolder) {

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

		ValueHolder vh = (ValueHolder) c;
		Object val = vh.getValue();

		// format the value as string
		if (val != null) {
			/*
			 * OLD Converter converter = getConverter(fc, vh);
			 */

			/* NEW */
			Converter converter = vh.getConverter();
			if (converter == null) {
				Class<?> valueType = val.getClass();
				if (valueType == String.class && (null == fc.getApplication().createConverter(String.class))) {
					return (String) val;
				}

				converter = fc.getApplication().createConverter(valueType);
			}
			/* END NEW */

			if (converter != null)
				return converter.getAsString(fc, c, val);
			else
				return val.toString(); // Use toString as a fallback if
										// there is no explicit or implicit
										// converter
		} else {
			// component is a value holder but has no value
			return null;
		}
	}

	// component it not a value holder
	return null;
}
 
Example 10
Source File: Datepicker.java    From BootsFaces-OSP with Apache License 2.0 4 votes vote down vote up
private String getConvertedDateAsString(Object dt, String format, Locale locale) {

		Converter converter = getConverter();
		return converter == null ? getDateAsString(dt, format, locale)
				: converter.getAsString(getFacesContext(), this, dt);
	}
 
Example 11
Source File: SelectOneMenuRenderer.java    From BootsFaces-OSP with Apache License 2.0 4 votes vote down vote up
/** Receives the value from the client and sends it to the JSF bean. */
@Override
public void decode(FacesContext context, UIComponent component) {
	SelectOneMenu menu = (SelectOneMenu) component;
	if (menu.isDisabled() || menu.isReadonly()) {
		return;
	}
	String outerClientId = menu.getClientId(context);
	String clientId = outerClientId + "Inner";
	String submittedOptionValue = (String) context.getExternalContext().getRequestParameterMap().get(clientId);

	Converter converter = menu.getConverter();
	if (null == converter) {
		converter = findImplicitConverter(context, component);
	}
	List<SelectItemAndComponent> items = SelectItemUtils.collectOptions(context, menu, converter);

	if (null != submittedOptionValue) {
		for (int index = 0; index < items.size(); index++) {
			Object currentOption = items.get(index).getSelectItem();
			String currentOptionValueAsString;
			Object currentOptionValue = null;
			if (currentOption instanceof SelectItem) {
				if (!((SelectItem) currentOption).isDisabled()) {
					currentOptionValue = ((SelectItem) currentOption).getValue();
				}
			}
			if (currentOptionValue instanceof String) {
				currentOptionValueAsString = (String) currentOptionValue;
			} else if (null != converter) {
				currentOptionValueAsString = converter.getAsString(context, component, currentOptionValue);
			} else if (currentOptionValue != null) {
				currentOptionValueAsString = String.valueOf(index);
			} else {
				currentOptionValueAsString = ""; // null values are submitted as empty strings
			}
			if (submittedOptionValue.equals(currentOptionValueAsString)) {
				Object submittedValue = null;
				if (currentOptionValue == null) {
					submittedValue = null;
				} else {
					submittedValue = null != converter ? currentOptionValueAsString : currentOptionValue;
				}
				menu.setSubmittedValue(submittedValue);
				menu.setValid(true);
				
				menu.validateValue(context, submittedValue);
				new AJAXRenderer().decode(context, component, clientId);
				if (menu.isValid()) {
					if (currentOptionValue == null)  {
						menu.setLocalValueSet(true);
					}
				}
				return;
			}
		}
		menu.validateValue(context, null);
		menu.setSubmittedValue(null);
		menu.setValid(false);
		return;
	}

	menu.setValid(true);
	menu.validateValue(context, submittedOptionValue);
	menu.setSubmittedValue(submittedOptionValue);
	new AJAXRenderer().decode(context, component, clientId);
}
 
Example 12
Source File: SelectOneMenuRenderer.java    From BootsFaces-OSP with Apache License 2.0 4 votes vote down vote up
private void renderOption(FacesContext context, SelectOneMenu menu, ResponseWriter rw, int index, String itemLabel,
		final String description, final Object itemValue, boolean isDisabledOption, boolean isEscape,
		UIComponent itemComponent, boolean isSelected) throws IOException {

	Converter converter = menu.getConverter();
	if (converter == null && itemValue != null && (!(itemValue instanceof String))) {
		converter = findImplicitConverter(context, menu);
	}
	String itemValueAsString = getOptionAsString(context, menu, itemValue, converter);

	boolean isItemLabelBlank = itemLabel == null || itemLabel.trim().isEmpty();
	itemLabel = isItemLabelBlank ? itemValueAsString : itemLabel;

	rw.startElement("option", itemComponent);
	rw.writeAttribute("data-label", itemLabel, null);
	if (description != null) {
		rw.writeAttribute("title", description, null);
	}

	if (itemValue != null) {
		String value;
		if (null != converter) {
			value = converter.getAsString(context, menu, itemValue);
		}
		else if (itemValue instanceof String) {
			value = (String) itemValue;
		} else {
			value = String.valueOf(index); // this is used for objects
		}
		rw.writeAttribute("value", value, "value");
	}
	else {
		rw.writeAttribute("value", "", "value");
	}
	if (isSelected) {
		rw.writeAttribute("selected", "true", "selected");
	}

	if (isDisabledOption)
		rw.writeAttribute("disabled", "disabled", "disabled");

	if (isEscape && !isItemLabelBlank) {
		rw.writeText(itemLabel, null);
	} else {
		rw.write(itemLabel);
	}

	rw.endElement("option");
}