/*
 * MyFormattedCellEditor.java
 *
 * Created on September 21, 2007, 2:33 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package usda.weru.mcrew;

import com.klg.jclass.cell.JCCellInfo;
import com.klg.jclass.cell.editors.JCStringCellEditor;
import com.klg.jclass.table.JCTableCellInfo;
import java.awt.AWTEvent;
import java.awt.Graphics;
import java.awt.event.FocusListener;
import java.text.Format;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

/**
 *
 * @author Joseph Levin
 */
public class MyFormattedCellEditor extends JCStringCellEditor{
    private static final long serialVersionUID = 1L;
    
    private Format c_format = null;
    
    private static final JComponent DUMMY = new JLabel();
    private boolean c_firstDraw;
    /** Creates a new instance of MyFormattedCellEditor */
    public MyFormattedCellEditor() {
    }
    
    @Override
    public void initialize(AWTEvent event, JCCellInfo cellInfo, Object o) {
        c_firstDraw = true;
        if (o == ManageData.EMPTY_OBJECT){
            o = null;
        }
        if (o == null){
            cancelCellEditing();
            data = null;
            return;
        }
        setEnabled(cellInfo.isEditable());                     
        
        
        if (isNumeric(o) && cellInfo instanceof JCTableCellInfo){
            if(c_format == null)
            {
                JCTableCellInfo info = (JCTableCellInfo) cellInfo;
                int column = info.getColumn();
                MCREWConfig.ColumnDefn defn = MCREWConfig.getColumns().get(column);
 
                String format = defn.getEditFormat();
                if (format == null){
                    format = "0.#####";
                }
                c_format = MCREWConfig.getFormat(format);
            }
            
            if (o!= null && c_format != null){
                o = c_format.format(Double.parseDouble(o.toString()));
            }
        }                
        super.initialize(event, cellInfo, o);

        SwingUtilities.invokeLater(new Runnable(){

            @Override
            public void run() {
                requestFocusInWindow();                
                selectAll();
            }

        });
    }

	/**
	 *
	 * @param g
	 */
	@Override
    public void paint(Graphics g) {
        if (data != null){
            super.paint(g);
            if (c_firstDraw){
                SwingUtilities.invokeLater(new Runnable(){
                    @Override
                    public void run() {
                        selectAll();
                    }
                    
                });
                c_firstDraw = false;
            }
        } else{
            DUMMY.paint(g);
            
        }
    }
    
    @Override
    public boolean stopCellEditing() {        
        String text = getText();
        return isNumeric(text);                    
    }
    
    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;
        }
    }

	/**
	 *
	 * @param l
	 */
	@Override
    public synchronized void addFocusListener(FocusListener l) {
        super.addFocusListener(l);
    }


    
    
}
