package uk.ac.reload.editor.style;

//M:
import java.util.ArrayList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.TableModelEvent;

public class PreferencesTableModel extends javax.swing.table.AbstractTableModel implements javax.swing.event.TableModelListener, javax.swing.event.ListSelectionListener {

    private java.util.ArrayList<Value> values;

    public PreferencesTableModel(java.util.ArrayList<Value> values) {
        this.values = values;
    }

    public int getRowCount() {
        return values.size();
    }

    public int getColumnCount() {
        return 2;
    }

    public Object getValueAt(int iRow, int iColumn) {
        if (iColumn == 0) { //columna de names
            return values.get(iRow).getName();
        } else //columna de values
            return (values.get(iRow).getValue());
    }

    public void setValueAt(Object oValue, int iRow, int iColumn) {
        if (iColumn == 1) {
            try {
                values.get(iRow).setValue(oValue);
            //!!!!!!!!!!!!!!!!!! fireTableCellUpdated(iRow, iColumn); // Avisa a los posibles Listeners
            } catch (Exception ex) {
                System.err.println(ex);
            }
        }
    }

    public void tableChanged(TableModelEvent e) {
    //!!!!!!!!!!!!!!
    }

    public void valueChanged(ListSelectionEvent e) {
    //!!!!!!!!!!!!!!!
    }

    public Value getValue(int iIndice) {
        return values.get(iIndice);
    }

    public java.util.ArrayList<Value> getValues(){
        return values;
    }
    
    public java.util.ArrayList<Value> getCloneValues(){

        ArrayList<Value> clonevalues= new ArrayList<Value>();
        for (int i=0; i<11; i++){

            Value clonevalue= new Value(values.get(i).getName(), values.get(i).getValue(), values.get(i).getXMLTag());
            clonevalues.add(clonevalue);
        }
        return clonevalues;
    }
    
    public boolean isCellEditable(int iRow, int iColumn) {
        //Sólo es editable la columna de valores
        if (iColumn == 0) return false;
        else return true;
    }

    //títulos
     public String getColumnName(int iColumn) {
        if (iColumn==0) return "Name";
        else return "Value";
    }
  
}
