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

import java.util.Iterator;
import org.zkoss.zk.ui.Component;
import org.zkoss.zkplus.databind.TypeConverter;
import org.zkoss.zul.Comboitem;

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

    public Object coerceToUi(Object val, Component comp) {
        if (val != null) {
            //iterate to find the selected radio via the value
            for (Iterator it = comp.getChildren().iterator(); it.hasNext();) {
                final Component child = (Component) it.next();
                if (child instanceof Comboitem) {
                    String ret = null;
                    if (val instanceof Short) {
                        ret = String.valueOf(val);
                    }

                    if (ret.equals(((Comboitem) child).getValue())) {
                        return child;
                    }
                }
            }
        }
        return null;
    }

    public Object coerceToBean(Object val, Component cmpnt) {
        val = val != null ? ((Comboitem) val).getValue() : null;
        if (val instanceof String) {
            return Short.parseShort((String) val);
        }
        return null;
    }
}
