/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package clips.doctor.prescription;
import cli_fmw.delegate.directory.DirectoryItem;
import clips.delegate.directory.simple.takeDrug.DirectoryTakeDrugItem;
import clips.delegate.doctor.prescription.PrescriptionItemData;
import cli_fmw.main.ClipsException;
import cli_fmw.utils.ErrorValue;
import cli_fmw.utils.SelectorEditable;
import javax.swing.table.DefaultTableModel;
/**
*Модель таблицы для вывода рецепта
* @author lacoste
*/
public class TableModelPrescription extends DefaultTableModel {
private SelectorEditable<PrescriptionItemData> prescriptionItems;
public static final int COLCOUNT = 3;
public static final int COL_VIDAL = 0;
public static final int COL_TAKE_DRUG = 1;
public static final int COL_DESC = 2;
private boolean canBeEdit;
public TableModelPrescription(SelectorEditable<PrescriptionItemData> prescriptionItems, boolean canBeEdit) throws ClipsException {
this.canBeEdit = canBeEdit;
this.prescriptionItems = prescriptionItems;
}
@Override
public int getRowCount() {
if (prescriptionItems != null) {
return prescriptionItems.size();
}
return 0;
}
@Override
public int getColumnCount() {
return COLCOUNT;
}
@Override
public Object getValueAt(int r, int c) {
try {
PrescriptionItemData item = prescriptionItems.get(r);
switch (c) {
case COL_VIDAL:
return item.getVidal();
case COL_TAKE_DRUG:
return item.getTakeDrug();
case COL_DESC:
return item.getDescription();
default:
return null;
}
} catch (ClipsException ex) {
return new ErrorValue(ex);
}
}
@Override
public Class<?> getColumnClass(int columnIndex) {
if ((columnIndex == COL_VIDAL) || (columnIndex == COL_TAKE_DRUG)) {
return DirectoryItem.class;
}
return super.getColumnClass(columnIndex);
}
@Override
public void setValueAt(Object obj, int row, int col) {
if(col == COL_TAKE_DRUG) {
prescriptionItems.get(row).setTakeDrug((DirectoryTakeDrugItem) obj);
} else {
prescriptionItems.get(row).setDescription(obj.toString());
}
fireTableCellUpdated(row, col);
}
@Override
public String getColumnName(int col) {
String s;
switch (col) {
case COL_VIDAL:
s = "Лекарство";
break;
case COL_TAKE_DRUG:
s = "Способ приема";
break;
case COL_DESC:
s = "Описание";
break;
default:
s = "Не сработал switch-case";
}
return s;
}
@Override
public boolean isCellEditable(int row, int col) {
if (col == COL_VIDAL) {
return false;
} else {
return canBeEdit;
}
}
}