/*
 * CellIndependentTable.java
 *
 * Created on September 18, 2000, 1:41 PM
 */
package CITableTest;

import java.awt.Color;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

/** Cell independent controlable table.
 * Because the javax.swing.JTable class manage same data type in each column. In some case, we want to put different 
 * data type in each cell in the same column. This class provide this capability by derive from javax.swing.JTable and
 * modify getCellClass(), geCellRenderer(), and getCellEditor() methods for return each cell class.
 *
 * @author  Somchai LIMSIRORATANA, Nuntiya LIMSIRORATANA(Yawai)
 * @version 1.1
 * @author Weerasak WITTAWASKUL
 * 1MAY2003 Move setDefaultCellEditorRenderer from main class (TestCITable.java)
 * */
public class CellIndependentTable extends JTable {
    /** Creates new CellIndependentTable */
    public CellIndependentTable(DefaultTableModel dtm) {
        super(dtm);
        setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        setRowSelectionAllowed(false);
        setColumnSelectionAllowed(false);
        setBackground(java.awt.Color.lightGray);
        setDefaultCellEditorRenderer();
    }
    
    private void setDefaultCellEditorRenderer(Class forClass, TableCellEditor editor, TableCellRenderer renderer) {
        setDefaultEditor(forClass, editor);
        setDefaultRenderer(forClass, renderer);
    }
    
    private void setDefaultCellEditorRenderer() {
        // Setting default editor&renderer of Boolean
        setDefaultCellEditorRenderer(Boolean.class, new BooleanCellEditor(), new BooleanCellRenderer());
        // Number class
        // Setting default editor&renderer of java.math.BigDecimal
        setDefaultCellEditorRenderer(java.math.BigDecimal.class,new NumberCellEditor(), new NumberCellRenderer());
        // Setting default editor&renderer of java.math.BigInteger
        setDefaultCellEditorRenderer(java.math.BigInteger.class,new NumberCellEditor(), new NumberCellRenderer());
        // Setting default editor&renderer of java.lang.Byte
        setDefaultCellEditorRenderer(Byte.class,new NumberCellEditor(), new NumberCellRenderer());
        // Setting default editor&renderer of Double
        setDefaultCellEditorRenderer(Double.class,new NumberCellEditor(), new NumberCellRenderer());
        // Setting default editor&renderer of Float
        setDefaultCellEditorRenderer(Float.class,new NumberCellEditor(), new NumberCellRenderer());
        // Setting default editor&renderer of Integer
        setDefaultCellEditorRenderer(Integer.class,new NumberCellEditor(), new NumberCellRenderer());
        // Setting default editor&renderer of Long
        setDefaultCellEditorRenderer(Long.class,new NumberCellEditor(), new NumberCellRenderer());
        // Setting default editor&renderer of Short
        setDefaultCellEditorRenderer(Short.class,new NumberCellEditor(), new NumberCellRenderer());
        // Setting default editor&renderer of String
        setDefaultCellEditorRenderer(String.class,new StringCellEditor(), new StringCellRenderer());
        // Setting default editor&renderer of FileName
        setDefaultCellEditorRenderer(FileName.class,new FileNameCellEditor(), new FileNameCellRenderer());
        // Setting default editor&renderer of Color
        setDefaultCellEditorRenderer(Color.class,new ColorCellEditor(), new ColorCellRenderer());
    }

    /** getCellClass will get the class from TableModel.
     * This class will work depend on TableModel.
     */
    public Class getCellClass(int row,int col) {
        TableModel model = getModel();
        if (model instanceof CellIndependentTableModel) {
            CellIndependentTableModel ptm = (CellIndependentTableModel)model;
            return ptm.getCellClass(row,convertColumnIndexToModel(col));
        }
        return model.getColumnClass(convertColumnIndexToModel(col));
    }
    
    public TableCellRenderer getCellRenderer(int row, int column) {
        TableColumn tableColumn = getColumnModel().getColumn(column);
        TableCellRenderer renderer = tableColumn.getCellRenderer();
        if (renderer == null) {
            renderer = getDefaultRenderer(getCellClass(row,column));
        }
        return renderer;
    }

    public TableCellEditor getCellEditor(int row, int column) {
        TableColumn tableColumn = getColumnModel().getColumn(column);
        TableCellEditor editor = tableColumn.getCellEditor();
        if (editor == null) {
            editor = getDefaultEditor(getCellClass(row,column));
        }
        return editor;
    }
}