/*
 * MyFormattedCellRenderer.java
 *
 * Created on November 14, 2005, 4:18 PM
 *
 * Allows a JCTable cell to be rendered with a format.  The underlying data is still available when editing.
 */

package usda.weru.mcrew;
import com.klg.jclass.cell.*;
import com.klg.jclass.cell.renderers.*;
import com.klg.jclass.table.JCTableCellInfo;
import java.awt.*;
import java.text.*;

/**
 *
 * @author Joseph Levin
 */
public class MyFormattedCellRenderer extends JCStringCellRenderer{
    private String c_formatString;
    private Format c_format;
    private int c_dataType;
    /*
     * 0 = Unknown
     * 1 = Date
     * 2 = Numeric
     */
    /*
     * 0 = Unknown
     * 1 = Date
     * 2 = Numeric
     */
    public MyFormattedCellRenderer(){
        super();
    }
    
    public MyFormattedCellRenderer (String formatString){
        super();
        c_formatString = formatString;
    }
    
    public void draw(Graphics gc, JCCellInfo cellInfo, Object o, boolean selected) {
        boolean readonly = false;
        if (cellInfo instanceof JCTableCellInfo){
            JCTableCellInfo info = (JCTableCellInfo) cellInfo;
            readonly = !info.getCellStyle().isEditable();
        }

        if(readonly || o == null || o == ManageData.EMPTY_OBJECT){
            Rectangle area = cellInfo.getDrawingArea();
            gc.setColor(selected ? cellInfo.getSelectedBackground() : ConfigData.getDisabledColor());
            gc.fillRect(area.x - 100, area.y - 100, area.width + 200, area.width + 200);
        }
        
        if (c_formatString == null){
            draw1(gc, cellInfo, o, selected);
        }
        else{
            draw2(gc, cellInfo, o, selected);
        }
    }
    
    public void draw1(Graphics gc, JCCellInfo cellInfo, Object o, boolean selected) {
        if (o == null){
            return;
        }
        c_format = null;
        if (isNumeric(o) && cellInfo instanceof JCTableCellInfo){
            JCTableCellInfo info = (JCTableCellInfo) cellInfo;
            int column = info.getColumn();
            ConfigData.ColumnDefn defn = ConfigData.getColumns().get(column);
            
            String format = defn.getNumberFormat();
            c_format = ConfigData.getFormat(format);
            
            if (o!= null && c_format != null){
                o = c_format.format(Double.parseDouble(o.toString()));
            }
        }                
        super.draw(gc, cellInfo, o, selected);
    }
    

    
    public void draw2(Graphics gc, JCCellInfo cellInfo, Object o, boolean selected) {
        int dataType;
        if (isDate(o)) dataType = 1;
        else if (isNumeric(o)) dataType = 2;
        else dataType = 0;
        
        if (dataType != c_dataType){
            c_format = null;
        }
        c_dataType = dataType;
        
        if (c_format == null){
            //We need to learn what type of format to use and make it.
             switch (c_dataType){
                 case 0:
                    break;
                 case 1:
                     try{
                        c_format = new SimpleDateFormat(c_formatString);
                    }
                    catch (NullPointerException npe){
                        c_format = null;
                    }
                    catch (IllegalArgumentException iae){
                        c_format = null;
                    }
                     break;
                 case 2:
                     try{
                        c_format = new DecimalFormat(c_formatString);
                    }
                    catch (NullPointerException npe){
                        c_format = null;
                    }
                    catch (IllegalArgumentException iae){
                        c_format = null;
                    }
                     break;
             }   
        }
        //did we make a format
        if (o!= null && c_format != null){
            switch (c_dataType){
                 case 0:
                    break;
                 case 1:
                     break;
                 case 2:
                     o = Double.parseDouble(o.toString());
                     break;
             }   
            o = (String) c_format.format(o);
        }
        super.draw(gc, cellInfo, o, selected);
    }
    
    private boolean isDate(Object obj){
        try{
            /*Date d =*/new SimpleDateFormat().parse(obj.toString());
            return true;
        }
        catch(Exception e){
            return false;
        }
    }

    private boolean isNumeric (Object obj){
        try{
            Double d = Double.parseDouble(obj.toString());
            if (!d.isNaN()) return true;
            else return false;
        }
        catch (Exception e){
            return false;
        }
    }
    
    
}
