package project;

import java.util.List;

import javax.swing.table.AbstractTableModel;

public class MyModel extends AbstractTableModel {
	List<Participant> tableData;
	String[] tableHeaders;
	
	public MyModel(List<Participant> tableData,String[] tableHeaders){
		this.tableData=tableData;
		this.tableHeaders=tableHeaders;
	}

	public boolean isCellEditable(int rowIndex, int mColIndex) {
        return true;
    }
    
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }
    
    public void setValueAt(Object value, int row, int col) {
    	Participant p=tableData.get(row);
    	fireTableCellUpdated(row, col);
    	
    }
    
    public String getColumnName(int col) {
        return tableHeaders[col].toString();
    }

	@Override
	public Object getValueAt(int row, int column) {
		// TODO Auto-generated method stub
		
		Participant p=tableData.get(row);
    	switch (column){
    	case 0:
    		return new Integer(p.getId());
    	case 1:
    		return new Integer(p.getAge());
    	case 2:
    		return p.getGender();
    	case 3:
    		return p.getEthnicity();
    	case 4:
    		return p.getOccupation();
    	case 5:
    		return p.getCountry();
    	case 6:
    		return p.getOrigin();
    	case 7:
    		return p.getEducation();
    	}
    	return "";
	}

	@Override
	public int getRowCount() {
		// TODO Auto-generated method stub
		return tableData.size();
	}

	@Override
	public int getColumnCount() {
		// TODO Auto-generated method stub
		return tableHeaders.length;
	}
	
	public void colUpdated(){
		fireTableDataChanged();
	}
	
	public void addRecord(Participant p){
		int rowCount=getRowCount();
		p.setId(rowCount+1);
		tableData.add(p);
		fireTableRowsInserted(rowCount, rowCount);
		
	}
	public void removeRecord(Participant p){
		int rowCount=getRowCount();
		tableData.remove(p);
		fireTableRowsDeleted(rowCount-1, rowCount-1);
		
	}
    
   
    

}
