/**
 * 
 */
package cz.neubauer.tomas.hazardanalysis.model.tablemodels;

import javax.swing.table.AbstractTableModel;

import cz.neubauer.tomas.hazardanalysis.model.analysis.Analysis;

/**
 * @author Tomáš Neubauer
 */
public class AnalysisTableModel extends AbstractTableModel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -4715872346724348539L;

	private Analysis analysis;

	private String[] columnNames = new String[] { "Název komopnenty systému",
			"Poloha komponenty v systému", "Typ nebezpečí dle ČSN EN 1050",
			"Upřesnit" };

	public AnalysisTableModel() {
		analysis = new Analysis();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#getColumnCount()
	 */
	@Override
	public int getColumnCount() {
		return 4;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#getRowCount()
	 */
	@Override
	public int getRowCount() {
		return analysis.getRecordsCount();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#getValueAt(int, int)
	 */
	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		switch (columnIndex) {
		case 0:
			return analysis.getAnalysisRecord(rowIndex).getComponent()
					.getName();
		case 1:
			return analysis.getAnalysisRecord(rowIndex).getComponent()
					.getLocation();
		case 2:
			return analysis.getAnalysisRecord(rowIndex).getComponent()
					.getDangersAsString();
		case 3:
			return "upřesnit...";
		default:
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.AbstractTableModel#isCellEditable(int, int)
	 */
	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return columnIndex == 0 | columnIndex == 1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.AbstractTableModel#setValueAt(java.lang.Object,
	 *      int, int)
	 */
	@Override
	public void setValueAt(Object value, int rowIndex, int columnIndex) {
		switch (columnIndex) {
		case 0:
			analysis.getAnalysisRecord(rowIndex).getComponent().setName(
					(String) value);
			break;
		case 1:
			analysis.getAnalysisRecord(rowIndex).getComponent().setLocation(
					(String) value);
			break;
		case 2:
			// nop
			break;
		default:
			// nop
			break;
		}
	}

	public Analysis getAnalysis() {
		return analysis;
	}

	public void setAnalysis(Analysis analysis) {
		this.analysis = analysis;
		fireTableDataChanged();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.AbstractTableModel#getColumnName(int)
	 */
	@Override
	public String getColumnName(int column) {
		return columnNames[column];
	}
}
