/*
 * ColorCellEditor.java
 *
 * Created on September 21, 2000, 5:20 PM
 */
package CITableTest;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.TableCellEditor;
//import javax.vecmath.Color3f;

/** Cell editor for Color.
 *
 * @author  Somchai LIMSIRORATANA, Nuntiya LIMSIRORATANA(Yawai)
 * @version 1.1
 */
public class ColorCellEditor extends AbstractCellEditor implements TableCellEditor {

    protected JTextField textField;
    protected JPanel panel;
    protected Object oldValue;
    /** Creates new ColorCellEditor */
    public ColorCellEditor() {
        panel = new JPanel();
        panel.setLayout(new javax.swing.BoxLayout(panel, 0));
        textField = new JTextField();
        textField.addActionListener(this);
        textField.setEditable(true);
        textField.setHorizontalAlignment(SwingConstants.CENTER);
        panel.add(textField);
        JButton button = new JButton("..");
        Dimension d = new Dimension(20,20);
        button.setPreferredSize(d);
        button.setMaximumSize(d);
        button.addActionListener(new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                Object value = getCellEditorValue();
                Color cvalue = new Color(0,0,0);
                if (value instanceof Color)
                    cvalue = (Color)value;
//                else
//                    if (value instanceof Color3f) {
//                        Color3f c3f = (Color3f)value;
//                        cvalue = new Color(c3f.x,c3f.y,c3f.z);
//                    }
                Color c = JColorChooser.showDialog(panel,"ColorChooser",cvalue);
                if (c == null) c = cvalue;
                setTextField(c);
                fireEditingStopped();
            }
        });
        panel.add(button);
    }

    protected void setTextField(Object value) {
        if (value instanceof Color)
            setTextField((Color)value);
//        else
//            if (value instanceof Color3f)
//                setTextField((Color3f)value);
    }
    protected void setTextField(Color value) {
        String str = value.toString();
        str = str.substring(str.indexOf('['),str.lastIndexOf(']')+1);
        textField.setText(str);
    }
//    protected void setTextField(Color3f value) {
//        setTextField(new Color(value.x,value.y,value.z));
//    }
    /** Sets an initial <I>value</I> for the editor.  This will cause
     * the editor to stopEditing and lose any partially edited value
     * if the editor is editing when this method is called. <p>
     *
     * Returns the component that should be added to the client's
     * Component hierarchy.  Once installed in the client's hierarchy
     * this component will then be able to draw and receive user input.
     *
     * @param   table       the JTable that is asking the editor to edit
     *              This parameter can be null.
     * @param   value       the value of the cell to be edited.  It is
     *              up to the specific editor to interpret
     *              and draw the value.  eg. if value is the
     *              String "true", it could be rendered as a
     *              string or it could be rendered as a check
     *              box that is checked.  null is a valid value.
     * @param   isSelected  true is the cell is to be renderer with
     *              selection highlighting
     * @param   row         the row of the cell being edited
     * @param   column      the column of the cell being edited
     * @return  the component for editing
     */
    public Component getTableCellEditorComponent(JTable table,Object value,boolean isSelected,int row,int column) {
        setTextField(value);
        oldValue = value;
        return panel;
    }
    /** Returns the value contained in the editor */
    public Object getCellEditorValue() {
        try {
            String str = textField.getText();
            int redIndex = str.indexOf("r=");
            int greenIndex = str.indexOf("g=");
            int blueIndex = str.indexOf("b=");
            if ((redIndex != -1) && (greenIndex != -1) && (blueIndex != -1)) {
                redIndex += 2;
                greenIndex += 2;
                blueIndex += 2;
                String parse = str.substring(redIndex,str.indexOf(",",redIndex));
                int r = Integer.parseInt(parse);
                r = (r>255)?255:r; // not more than 255
                r = (r<0)?0:r;     // not less than 0
                int g = Integer.parseInt(str.substring(greenIndex,str.indexOf(",",greenIndex)));
                g = (g>255)?255:g; // not more than 255
                g = (g<0)?0:g;     // not less than 0
                int b = Integer.parseInt(str.substring(blueIndex,str.indexOf("]",blueIndex)));
                b = (b>255)?255:b; // not more than 255
                b = (b<0)?0:b;     // not less than 0
                Color value = new Color(r,g,b);
//                if (oldValue instanceof Color3f)
//                    oldValue = new Color3f(value.getColorComponents(null));
//                else
                    oldValue = value;
            }
        } catch (NumberFormatException e) {}
        return oldValue;
/* Parsing Color3f format
                Color3f value=null;
                try {
                    String str = textField.getText();
                    int beginRed = str.indexOf('(');
                    int endRed = str.indexOf(',');
                    int endGreen = str.indexOf(',',endRed+1);
                    int endBlue = str.indexOf(')',endGreen);
                    float x = Float.parseFloat(str.substring(beginRed+1,endRed));
                    x = (x>1.0f)?1.0f:x; // not more than 1.0
                    x = (x<0.0f)?0.0f:x; // not less than 0.0
                    float y = Float.parseFloat(str.substring(endRed+1,endGreen));
                    y = (y>1.0f)?1.0f:y; // not more than 1.0
                    y = (y<0.0f)?0.0f:y; // not less than 0.0
                    float z = Float.parseFloat(str.substring(endGreen+1,endBlue));
                    z = (z>1.0f)?1.0f:z; // not more than 1.0
                    z = (z<0.0f)?0.0f:z; // not less than 0.0
                    value = new Color3f(x,y,z);
                } catch (NumberFormatException e) {
                    value = new Color3f();
                }
*/
    }
}