/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myschedulemanager;

import Modelnya.getFile;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Cahya_NF
 */
public class TestTableToday extends AbstractTableModel{

    private String[] columnNames = {"ID","Date",
        "Start",
        "End",
        "Subject",
        "Client",
        "Place",
        "Event"
    };


    /*private String[][] data = {
        {"Mary", "Campione","Snowboarding"},
        {"Alison", "Huml","Rowing"},
        {"Kathy", "Walrath","Knitting"},
        {"Sharon", "Zakhour","Speed reading"},
        {"Philip", "Milne","Pool"}
    };*/

    public String[][] getData() throws FileNotFoundException, IOException{
        String [] data=getFile.readFile();
        String[][] dataList=new String[data.length][8];
        int fldLength = 0;
        for (int i = 0; i < data.length; i++) {
            String[] fld=null;
            fld=data[i].split(";");
            for (int j = 0; j < fld.length; j++) {
                dataList[i][j]=fld[j];
            }
        }
        return dataList;
    }

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

    public int getRowCount() {
        String [][]datalist = null;
        try {
            datalist = getData();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(TestTableToday.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(TestTableToday.class.getName()).log(Level.SEVERE, null, ex);
        }

        return datalist.length;
    }

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

    public Object getValueAt(int row, int col) {
        String [][]datalist = null;
        try {
            datalist = getData();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(TestTableToday.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(TestTableToday.class.getName()).log(Level.SEVERE, null, ex);
        }
        return datalist[row][col];
    }
    /*
     * JTable uses this method to determine the default renderer/
     * editor for each cell. If we didn't implement this method,
     * then the last column would contain text ("true"/"false"),
     * rather than a check box.
     */

    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }
    /*
     * Don't need to implement this method unless your table's
     * editable.
     */

    public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
        if (col < 2) {
            return false;
        } else {
            return true;
        }
    }
    /*
     * Don't need to implement this method unless your table's
     * data can change.
     */

    public void setValueAt(Object value, int row, int col) {
        String [][]datalist = null;
        try {
            datalist = getData();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(TestTableToday.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(TestTableToday.class.getName()).log(Level.SEVERE, null, ex);
        }
        datalist[row][col] = (String) value;
        fireTableCellUpdated(row, col);
    }
}
