/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rjconsultores.ventaboletos.web.utilerias;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import org.zkoss.zk.ui.Component;
import org.zkoss.zkplus.databind.TypeConverter;

/**
 * 
 * @author Administrador
 */
public class StringPercentToDecimalConverter implements TypeConverter {

	private static final String FORMAT = "#####0.00";

	public Object coerceToUi(Object val, Component comp) {
		if (val != null) {
			String format = (String) comp.getAttribute("format");
			if (format == null) {
				format = FORMAT;
			}
			DecimalFormat df = new DecimalFormat(format, new java.text.DecimalFormatSymbols(new Locale("pt", "BR")));
			return df.format(val);
		}
		return null;
	}

	public Object coerceToBean(Object val, Component cmpnt) {
		if (val == null) {
			return null;
		}

		if (val instanceof String) {

			return (val.toString().trim().isEmpty()) ? (BigDecimal) null : new BigDecimal(val.toString().replace(".", "").replace(",", "."));
		}
		return null;
	}
}
