All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.ulrice.databinding.converter.impl.StringToBooleanConverter Maven / Gradle / Ivy

The newest version!
package net.ulrice.databinding.converter.impl;

import net.ulrice.databinding.bufferedbinding.IFAttributeInfo;
import net.ulrice.databinding.converter.IFValueConverter;
import net.ulrice.databinding.converter.ValueConverterException;

/**
 * String to Integer converter
 * 
 * @author andre
 * 
 */
public class StringToBooleanConverter implements IFValueConverter {

    @Override
    public Class getViewType(Class modelType) {
        return String.class;
    }

    @Override
    public Class getModelType(Class viewType) {
        return Boolean.class;
    }
    
    @Override
    public Boolean viewToModel(String view, IFAttributeInfo attributeInfo) {
        if (view == null || "".equals(view.trim())) {
            return null;
        }
        if("X".equals(view.trim())){
            return true;
        }
        
        try {
            return Boolean.valueOf(view.toString());
        } catch (NumberFormatException ex) {
            throw new ValueConverterException(ex);
        }
        
    }

    @Override
    public String modelToView(Boolean model, IFAttributeInfo attributeInfo) {
        return model == null ? "" : String.valueOf(model);
    }

    @Override
    public boolean canHandle(Class modelType,
            Class viewType) {
        if (String.class.equals(viewType)) {
            if (Boolean.class.equals(modelType) || Boolean.TYPE.equals(modelType)) {
                return true;
            }
        }
        return false;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy