package puntocassa;

import javax.swing.table.AbstractTableModel;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Marco
 */
public class MyTableModel extends AbstractTableModel {

    private String[] columnNames;
    private Object[][] data;
    private Boolean editable = false;
    private Boolean modificato = false;
    private int[] colModificabile;
    private int[] colBoolean;
    private Object[] row;

    public Double somma(int col) {
        Double tot = 0.0;
        if (data != null) {
            for (int i = 0; i < data.length; i++) {
                if (data[i][col] != null) {
                    tot = tot + Double.valueOf(data[i][col].toString());
                }
            }
        }
        return tot;
    }

    public int sommaInt(int col) {
        int tot = 0;
        if (data != null) {
            for (int i = 0; i < data.length; i++) {
                if (data[i][col] != null) {
                    tot = tot + (int) data[i][col];
                }
            }
        }
        return tot;
    }

    public Object[] getRow(int Row) {
        row = data[Row];
        return row;
    }

    public void addRow(Object[] rowData) {
        Object[][] data2;
        if (data != null) {
            data2 = new Object[data.length + 1][];
            int r = 0;
            for (int i = 0; i < data.length; i++) {
                data2[r] = data[i];
                r++;
            }
            data2[r] = rowData;
        } else {
            data2 = new Object[1][];
            data2[0] = rowData;
        }

        this.data = data2;
        fireTableDataChanged();

    }

    public void setRow(Object[] row) {
        Object[][] data2;
        try {
            data2 = new Object[data.length + 1][];
            for (int i = 0; i < data.length; i++) {
                data2[i] = data[i];
            }
            data2[data.length] = row;
            this.data = data2;
        } catch (Exception ex) {
        }

    }

    public void deleteRow(int row) {
        if (data.length > 0) {
            Object[][] data2;
            data2 = new Object[data.length - 1][];
            int r = 0;
            for (int i = 0; i < data.length; i++) {
                if (i != row) {
                    data2[r] = data[i];
                    r++;
                }
            }

            this.data = data2;
            this.fireTableDataChanged();
        }
    }

    public void clearTable() {
        for (Integer i = this.getRowCount() - 1; i >= 0; i--) {
            this.deleteRow(i);
        }
    }

    public int[] isColBoolean() {
        return colBoolean;
    }

    public void setColBoolean(int[] colBoolean) {
        this.colBoolean = colBoolean;
    }

    public int[] getColModificabile() {
        return colModificabile;
    }

    public void setColModificabile(int[] colModificabile) {
        this.colModificabile = colModificabile;
    }

    public Boolean isModificato() {
        return modificato;
    }

    public void setModificato(Boolean modificato) {
        this.modificato = modificato;
    }

    public Boolean isEditable() {
        return editable;
    }

    public void setEditable(Boolean editable) {
        this.editable = editable;
    }

    public void setColumname(String[] colnames) {
        columnNames = colnames;
    }

    public void setData(Object[][] rowdata) {
        data = rowdata;
    }

    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        int rows = 0;
        try {
            rows = data.length;
        } catch (Exception ex) {
        }
        return rows;
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
        try {
            return data[row][col];

        } catch (Exception ex) {
            return "";
        }

    }

    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    @Override
    public void setValueAt(Object value, int row, int col) {
        if (colBoolean != null) {
            for (int i = 0; i < colBoolean.length; i++) {
                if (colBoolean[i] == col) {
                    //data[row][col] = value;
                    //fireTableCellUpdated(row, col);
                    return;
                }
            }
        }
        if (colModificabile != null) {
            for (int i = 0; i < colModificabile.length; i++) {
                if (colModificabile[i] == col) {
                    data[row][col] = value;
                    fireTableCellUpdated(row, col);
                    return;
                }
            }
        } else {
            data[row][col] = value;
            fireTableCellUpdated(row, col);
            return;

        }

    }

    @Override
    public boolean isCellEditable(int row, int col) {
        if (editable) {
            boolean M = false;
            for (int i = 0; i < colModificabile.length; i++) {
                if (col == colModificabile[i]) {
                    if (data[row][col].toString().equalsIgnoreCase("true") || data[row][col].toString().equalsIgnoreCase("false")) {
                        data[row][col] = !(Boolean) data[row][col];

                    } else {

                    }
                    M = true;
                }
            }
            modificato = M;
            return M;
        } else {
            return false;
        }

    }

}
