/*
 * ICESOFT COMMERCIAL SOURCE CODE LICENSE V 1.1
 *
 * The contents of this file are subject to the ICEsoft Commercial Source
 * Code License Agreement V1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the
 * License at
 * http://www.icesoft.com/license/commercial-source-v1.1.html
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations under
 * the License.
 *
 * Copyright 2009-2015 ICEsoft Technologies Canada, Corp. All Rights Reserved.
 */
package com.icesoft.faces.facelets.component.table.richdatagrid;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;


public class RichDataTextColumn extends BaseRichDataColumn {

    private Class clazz;
    private Converter converter;

    public RichDataTextColumn(Class clazz, String property, String title, boolean editable,
                              boolean moveable, boolean shownByDefault, boolean sortable, boolean filterable) {
        super(property, title, COLUMN_TYPE_TEXT, editable, moveable, shownByDefault, sortable, filterable, true);
        this.clazz = clazz;
    }

    public RichDataTextColumn(Class clazz, String property, String title, boolean editable,
                              boolean moveable, boolean shownByDefault, boolean sortable, boolean filterable, boolean hideable) {
        super(property, title, COLUMN_TYPE_TEXT, editable, moveable, shownByDefault, sortable, filterable, hideable);
        this.clazz = clazz;
    }

    public RichDataTextColumn(Class clazz, String property, String title) {
        super(property, title, COLUMN_TYPE_TEXT);
        this.clazz = clazz;
    }

    public boolean isUseNumberConverter() {
        return clazz == Integer.class || clazz == Long.class || clazz == Double.class ||
                clazz == BigDecimal.class || clazz == BigInteger.class;
    }

    public Converter getConverter() {
        if (converter == null)
            converter = new RichDataTextColumnConverter();
        return converter;

    }

    class RichDataTextColumnConverter implements Converter, Serializable {

        public Object getAsObject(FacesContext fc, UIComponent uic, String str) {
            if (str == null || str.length() == 0)
                return null;
            if (clazz.isPrimitive()) {
                if (clazz.getName().equals("int"))
                    return Integer.parseInt(str);
                if (clazz.getName().equals("double"))
                    return Double.parseDouble(str);
                if (clazz.getName().equals("float"))
                    return Float.parseFloat(str);
                if (clazz.getName().equals("long"))
                    return Long.parseLong(str);
            }
            if (clazz == String.class)
                return str;
            if (clazz == Integer.class)
                return Integer.valueOf(str);
            if (clazz == Long.class)
                return Long.valueOf(str);
            if (clazz == Double.class)
                return Double.valueOf(str);
            if (clazz == Float.class)
                return Float.valueOf(str);
            if (clazz == BigInteger.class)
                return new BigInteger(str);
            if (clazz == BigDecimal.class)
                return new BigDecimal(str);
            return null;
        }

        public String getAsString(FacesContext fc, UIComponent uic, Object obj) {
            if (obj == null)
                return "";
            return obj.toString();
        }

    }

}
