/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package clips.accountant;
import cli_fmw.delegate.directory.complex.DirectoryLocator;
import cli_fmw.main.ClipsException;
import cli_fmw.utils.SelectorEditableExceptional;
import clips.delegate.directory.complex.DirectoryDistrict;
import clips.delegate.directory.complex.DirectoryDistrictItem;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;
/**
*
* @author petr
*/
public class TableModelDistricts extends AbstractTableModel {
public static final int CL_SELECT = 0;
public static final int CL_DISTRICT = 1;
private List<DirectoryDistrictItem> selectedItems;
private ArrayList<DirectoryDistrictItem> allItems = new ArrayList<DirectoryDistrictItem>();
public TableModelDistricts(List<DirectoryDistrictItem> districtItems) throws ClipsException {
this.selectedItems = districtItems;
SelectorEditableExceptional<DirectoryDistrictItem> items = DirectoryLocator.getDirectory(DirectoryDistrict.class).getItems();
for (int i = 0; i < items.size(); i++) {
DirectoryDistrictItem dsi = items.get(i);
if (dsi.getID() != 0){
allItems.add(dsi);
}
}
}
@Override
public Class<?> getColumnClass(int columnIndex) {
if(columnIndex == CL_SELECT){
return Boolean.class;
}else {
return super.getColumnClass(columnIndex);
}
}
@Override
public String getColumnName(int column) {
return column == CL_DISTRICT ? "Участок" : null;
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == CL_SELECT;
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
System.out.println(rowIndex + "x" + columnIndex + ": "+ aValue);
if (columnIndex == CL_SELECT && aValue instanceof Boolean){
if ((Boolean)aValue){
selectedItems.add(allItems.get(rowIndex));
}else{
selectedItems.remove(allItems.get(rowIndex));
}
fireTableCellUpdated(rowIndex, columnIndex);
}
}
@Override
public int getRowCount() {
return allItems.size();
}
@Override
public int getColumnCount() {
return 2;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
DirectoryDistrictItem ddi = allItems.get(rowIndex);
switch (columnIndex){
case CL_SELECT: return selectedItems.contains(ddi);
case CL_DISTRICT: return ddi;
default: return null;
}
}
}