Do not know if you have a solution...
But I did like this....
Create a tag that have a converter. Register a tag in faces-config and use the new tag in a component.
faces-config.xml:
numberConverter
br.com.softcomex.components.gui.presentation.converters.NumberConverter
maxFractionDigitsMask
java.lang.Integer
taglib.xml:
http://www.site.com/jsf
maskNumberConverter
numberConverter
xhtml:
Converter Java:
public class NumberConverter implements Converter {
/**
* Número máxmimo de dígitos depois da vírgula. Passado como parâmetro pela tela.
*/
private int maxFractionDigitsMask;
/**
* Passado como parametro pela tela.
* @return Número Máximo de Digitos.
*/
public int getMaxFractionDigitsMask() {
return maxFractionDigitsMask;
}
/**
* Passado como parametro pela tela.
* @param maxFractionDigitsMask Número máximo de digitos depois da vírgula.
*/
public void setMaxFractionDigitsMask(int maxFractionDigitsMask) {
this.maxFractionDigitsMask = maxFractionDigitsMask;
}
/**
* Construtor.
*/
public NumberConverter() {
this.setMaxFractionDigitsMask(-1);
}
/**
* Conversão de String para BigDecimal.
* @param context FacesContext for the request being processed.
* @param component UIComponent with which this model object value is associated.
* @param value String value to be converted (may be null).
* @return Resultado da conversão Number.
* @see javax.faces.convert.Converter#getAsObject(javax.faces.context.FacesContext,
* javax.faces.component.UIComponent, java.lang.String)
*/
public Object getAsObject(FacesContext context, UIComponent component, String value) {
Locale locale = context.getViewRoot().getLocale();
DecimalFormat decimal = (DecimalFormat) NumberFormat.getInstance(locale);
decimal.setParseBigDecimal(true);
Object oReturn = null;
try {
oReturn = decimal.parseObject(value);
} catch (ParseException e) {
throw new SfwSystemException(e);
}
return oReturn;
}
/**
* Conversão de BigDecimal para String.
* @param context FacesContext for the request being processed
* @param component UIComponent with which this model object value is associated
* @param value Model object value to be converted (may be null)
* @return Resultado da conversão
* @see javax.faces.convert.Converter#getAsString(javax.faces.context.FacesContext,
* javax.faces.component.UIComponent, java.lang.Object)
*/
public String getAsString(FacesContext context, UIComponent component, Object value) {
String sReturn = null;
if (value != null && value.toString().length() > 0) {
double dNumber = Double.parseDouble(value.toString());
Locale locale = context.getViewRoot().getLocale();
DecimalFormat decimal = (DecimalFormat) NumberFormat.getInstance(locale);
if (this.maxFractionDigitsMask != -1) {
decimal.setMaximumFractionDigits(this.maxFractionDigitsMask);
}
sReturn = decimal.format(dNumber);
}
return sReturn;
}
}