package com.connor.ml.tcm.outfile.ML_010;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;

public class TableCellComboBoxEditor extends DefaultCellEditor implements TableCellEditor {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JComboBox<Object> comboBox;
//    private Object value;
    private JTable table;
    private int row;
    private int column;
 
    @SuppressWarnings("unchecked")
	public TableCellComboBoxEditor(Object[] items, boolean editable) {
    	super(new JComboBox<>(items));
        comboBox = (JComboBox<Object>) getComponent();
        comboBox.setEditable(editable);
        comboBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                System.out.println("action1£º" + comboBox.getSelectedItem());
                fireEditingStopped();
//                System.out.println("action2£º" + comboBox.getSelectedItem());
                if(table != null)
                	table.setValueAt(comboBox.getSelectedItem(), row, column);
            }
        });
    }
 
    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
//        System.out.println("getTableCellEditorComponent1£º" + comboBox.getSelectedItem() + "==" + value);
        this.table = table;
        this.row = row;
        this.column = column;
    	if(value == null)
            comboBox.setSelectedItem("");
    	else
    		comboBox.setSelectedItem(value);
//        this.value = value;
//        System.out.println("getTableCellEditorComponent2£º" + comboBox.getSelectedItem() + "==" + table.getValueAt(row, column));
        return comboBox;
    }
 
    @Override
    public Object getCellEditorValue() {
        return comboBox.getSelectedItem();
    }
}
