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

import javax.swing.table.TableCellRenderer;
import javax.swing.JTextField;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import java.awt.Component;
import java.awt.Color;
//import javax.vecmath.Color3f;
/**
 *
 * @author  Somchai LIMSIRORATANA, Nuntiya LIMSIRORATANA(Yawai)
 * @version 1.1
 */
public class ColorCellRenderer extends JTextField implements TableCellRenderer {

    /** Creates new ColorCellRenderer 
     * This renderer support 2 kind of color, Color and Color3f
     * if value is instance of Color, the interface will be integer value 0-255.
     * if value is instance of Color3f, the interface will be convert 
     * float value 0-1.0f to 0-255 value
     */
    public ColorCellRenderer() {
        setEditable(false);
        setHorizontalAlignment(SwingConstants.CENTER);
    }
    /** This method is sent to the renderer by the drawing table to
     * configure the renderer appropriately before drawing.  Return
     * the Component used for drawing.
     *
     * @param   table       the JTable that is asking the renderer to draw.
     *              This parameter can be null.
     * @param   value       the value of the cell to be rendered.  It is
     *              up to the specific renderer 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 index of the cell being drawn.  When
     *              drawing the header the rowIndex is -1.
     * @param   column          the column index of the cell being drawn
     */
    public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean hasFocus,int row,int column) {
        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);
//            }
        String str = cvalue.toString();
        str = str.substring(str.indexOf('['),str.lastIndexOf(']')+1);
        setText(str);
        setBackground(cvalue);
        // inverse color for text color
        // int is a 32bits value, to filter only 8bits right by 0xFFFFFF00
        int red   = ~(0xFFFFFF00 | cvalue.getRed());
        int green = ~(0xFFFFFF00 | cvalue.getGreen());
        int blue  = ~(0xFFFFFF00 | cvalue.getBlue());
        setForeground(new Color(red,green,blue));
        return this;
    }
}