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

The following examples show how to use javax.faces.convert.Converter#getAsObject() . 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: SelectOneMenuRenderer.java    From BootsFaces-OSP with Apache License 2.0 6 votes vote down vote up
private boolean isSelected(FacesContext context, SelectOneMenu menu, Object value, Object itemValue,
		Converter converter) {
	if (itemValue == null && value == null) {
		return true;
	}

	if (value != null) {
		Object compareValue;
		if (converter == null) {
			compareValue = coerceToModelType(context, itemValue, value.getClass());
		} else {
			compareValue = itemValue;

			if (compareValue instanceof String && !(value instanceof String)) {
				compareValue = converter.getAsObject(context, menu, (String) compareValue);
			}
		}

		if (value.equals(compareValue)) {
			return true;
		}

	}
	return false;
}
 
Example 2
Source File: SketchPadRenderer.java    From ctsms with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public Object getConvertedValue(FacesContext context, UIComponent component, Object submittedValue)
		throws ConverterException {
	Converter converter = getConverter(context, component);
	if (converter != null) {
		return converter.getAsObject(context, component, (String) submittedValue);
	} else {
		return submittedValue;
	}
}
 
Example 3
Source File: CoreRenderer.java    From BootsFaces-OSP with Apache License 2.0 5 votes vote down vote up
/**
 * This method is called by the JSF framework to get the type-safe value of the
 * attribute. Do not delete this method.
 */
@Override
public Object getConvertedValue(FacesContext fc, UIComponent c, Object sval) throws ConverterException {
	Converter cnv = resolveConverter(fc, c, sval);

	if (cnv != null) {
		if (sval == null || sval instanceof String) {
			return cnv.getAsObject(fc, c, (String) sval);
		} else {
			return cnv.getAsObject(fc, c, String.valueOf(sval));
		}
	} else {
		return sval;
	}
}
 
Example 4
Source File: Datepicker.java    From BootsFaces-OSP with Apache License 2.0 5 votes vote down vote up
@Override
protected Object getConvertedValue(FacesContext fc, Object sval) throws ConverterException {
	if (sval == null) {
		return null;
	}

	String val = (String) sval;
	// If the Trimmed submitted value is empty, return null
	if (val.trim().length() == 0) {
		return null;
	}

	Converter converter = getConverter();

	// If the user supplied a converter, use it
	if (converter != null) {
		return converter.getAsObject(fc, this, val);
	}
	// Else we use our own converter
	sloc = selectLocale(fc.getViewRoot().getLocale(), A.asString(getAttributes().get(JQ.LOCALE)));
	sdf = selectDateFormat(sloc, A.asString(getAttributes().get(JQ.DTFORMAT)));

	Calendar cal = Calendar.getInstance(sloc);
	SimpleDateFormat format = new SimpleDateFormat(sdf, sloc);
	format.setTimeZone(cal.getTimeZone());

	try {
		cal.setTime(format.parse(val));
		cal.set(Calendar.HOUR_OF_DAY, 12);

		return cal.getTime();
	} catch (ParseException e) {
		this.setValid(false);
		throw new ConverterException(
				BsfUtils.getMessage("javax.faces.converter.DateTimeConverter.DATE", val, sdf, getLabel(fc)));
	}
}
 
Example 5
Source File: TouchSpinRenderer.java    From BootsFaces-OSP with Apache License 2.0 5 votes vote down vote up
@Override
public Object getConvertedValue(FacesContext fc, UIComponent c, Object sval) throws ConverterException {
	Converter cnv = resolveConverter(fc, c, sval);

	if (cnv != null) {
		return cnv.getAsObject(fc, c, (String) sval);
	} else {
		cnv = new DoubleConverter();
		return cnv.getAsObject(fc, c, (String) sval);
	}
}
 
Example 6
Source File: DateTimePicker.java    From BootsFaces-OSP with Apache License 2.0 4 votes vote down vote up
/**
 * Converts the date from the moment.js format to a java.util.Date.
 */
@Override
public Object getConvertedValue(FacesContext context, Object submittedValue) throws ConverterException {
	if (submittedValue == null) {
		return null;
	}

	String val = (String) submittedValue;
	// If the Trimmed submitted value is empty, return null
	if (val.trim().length() == 0) {
		return null;
	}

	Converter converter = this.getConverter();

	// If the user supplied a converter, use it
	if (converter != null) {
		return converter.getAsObject(context, this, val);
	}
	// Else we use our own converter
	Locale sloc = BsfUtils.selectLocale(context.getViewRoot().getLocale(), this.getLocale(), this);
	String momentJSFormat = BsfUtils.selectMomentJSDateTimeFormat(sloc, this.getFormat(), this.isShowDate(),
			this.isShowTime());
	String javaFormat = LocaleUtils.momentToJavaFormat(momentJSFormat);

	Calendar cal = Calendar.getInstance(sloc);
	SimpleDateFormat format = new SimpleDateFormat(javaFormat, sloc);
	format.setTimeZone(cal.getTimeZone());

	try {
		cal.setTime(format.parse(val));

		return cal.getTime();
	} catch (ParseException e) {

		// FIRST STEP GONE: TRY THE AUTO-PARSER
		try {
			cal.setTime(LocaleUtils.autoParseDateFormat(val));
			return cal.getTime();
		} catch (Exception pe) {
			this.setValid(false);
			throw new ConverterException(BsfUtils.getMessage("javax.faces.converter.DateTimeConverter.DATE", val,
					javaFormat, BsfUtils.getLabel(context, this)));
		}
	}
}
 
Example 7
Source File: JsfUtil.java    From tomee with Apache License 2.0 4 votes vote down vote up
public static Object getObjectFromRequestParameter(final String requestParameterName, final Converter converter, final UIComponent component) {
    final String theId = JsfUtil.getRequestParameter(requestParameterName);
    return converter.getAsObject(FacesContext.getCurrentInstance(), component, theId);
}