package View;

import javax.swing.table.AbstractTableModel;

/**
 * Class which overwrites some methods of the original Table model, to make the table uneditable a the right cells and adds some functionality like to empty it or to write a whole row.
 * @author simon
 *
 */

public class MyTableModel extends AbstractTableModel {

	private String[] columnNames;
	private Object[][] data;
	private boolean firstColumnEdit;
	
	public MyTableModel(String[] columnnames, Object[][] data, boolean editFirst){
		super();
		firstColumnEdit=editFirst;
		columnNames=columnnames;
		this.data=data;
	}
	
	
	@Override
	public boolean isCellEditable(int rowIndex,int columnIndex){

		if(columnIndex==0 && firstColumnEdit){
			return true;
		}
		else{
			return false;
		}
		
	}
	
	
	
	
	@Override
	public int getColumnCount() {
		return columnNames.length;
	}

	@Override
	public int getRowCount() {
		return data.length;
	}

	public String getColumnName(int col) {
        return columnNames[col];
    }
	
	@Override
	public Object getValueAt(int row, int col) {
		return data[row][col];
	}
	
	@Override
	public void setValueAt(Object obj, int row, int col){
		data[row][col]=obj;
	}

	public void setRow(Object[] data,int row){
		for(int i=0;i<data.length && i<this.getColumnCount();i++){
			this.data[row][i]=data[i];
		}
	}
	
	public void empty(){
		for(int row=0;row<this.getRowCount();row++){
			for(int col=0;col<this.getColumnCount();col++){
				data[row][col]=null;
			}
		}
	}
	
	public void emptyResize(int rows, int column){
		data=new Object[rows][column];
	}
	
}
