/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package clips.service.discount;
import cli_fmw.delegate.directory.DirectoryItem;
import cli_fmw.utils.SelectorEditableExceptional;
import clips.delegate.directory.complex.DirectoryDiscountLevel;
import cli_fmw.delegate.directory.complex.DirectoryLocator;
import cli_fmw.main.ClipsException;
import clips.delegate.directory.complex.DirectoryDiscountLevelItem;
import clips.delegate.directory.simple.serviceClass.DirectoryServiceClassItem;
import clips.delegate.service.discount.Discounts;
import clips.delegate.directory.simple.serviceClass.DirectoryServiceClass;
import java.util.HashMap;
import javax.swing.table.AbstractTableModel;
/**
* Модель таблицы дисконтов, создается на основе интерфейса Discounts
* @author axe
*/
public class TableModelDiscount extends AbstractTableModel {
/**
* один столбец фиксирован
*/
public final int COL_SERVICES = 0;
Discounts discounts;
HashMap<Integer, DirectoryItem> columns = new HashMap<Integer, DirectoryItem>();
HashMap<Integer, DirectoryItem> rows = new HashMap<Integer, DirectoryItem>();
/**
* Создает новую модель
* @param discounts - интерфейс доступа к занчениям дисконта
* @throws cli_fmw.delegate.exception.ClipsException
*/
public TableModelDiscount(Discounts discounts) throws ClipsException {
this.discounts = discounts;
//row data
DirectoryServiceClass directory = DirectoryLocator.getDirectory(DirectoryServiceClass.class);
SelectorEditableExceptional<DirectoryServiceClassItem> ii = directory.getItems();
int row = 0;
for(int i=0; i<ii.size(); i++) {
DirectoryItem item = (DirectoryItem) ii.get(i);
if(item.getID() == 0) {
continue;
}
rows.put(row++, item);
}
//col data
DirectoryDiscountLevel discountDirectiry = DirectoryLocator.getDirectory(DirectoryDiscountLevel.class);
SelectorEditableExceptional<DirectoryDiscountLevelItem> jj = discountDirectiry.getItems();
int col = COL_SERVICES+1;
for(int i=0; i<jj.size(); i++) {
DirectoryItem item = jj.get(i);
if(item.getID() == 0) {
continue;
}
columns.put(col++, item);
}
}
@Override
public int getRowCount() {
return rows.size();
}
@Override
public int getColumnCount() {
return columns.size() + 1; //COL_SERVICES
}
@Override
public Object getValueAt(int row, int col) {
if(col == COL_SERVICES) {
DirectoryItem item = rows.get(row);
return item;
}
DirectoryServiceClassItem service = (DirectoryServiceClassItem) rows.get(row);
DirectoryDiscountLevelItem level = (DirectoryDiscountLevelItem) columns.get(col);
if(service != null && level != null) {
return discounts.getValue(level, service);
}
return null;
}
@Override
public String getColumnName(int col) {
if(col == COL_SERVICES) {
return "Классы услуг";
}
DirectoryItem item = columns.get(col);
return item.getTitle();
}
@Override
public boolean isCellEditable(int row, int col) {
if(col == COL_SERVICES) {
return false;
}
DirectoryItem service = rows.get(row);
DirectoryItem level = columns.get(col);
if(service == null || level == null) {
return false;
}
return true;
}
@Override
public Class<?> getColumnClass(int col) {
if(col == COL_SERVICES) {
return String.class;
}
return Integer.class;
}
/**
* Назначает новое значение, вызывается после изменения пользователем таблицы
* @param arg0 - новое значение
* @param row
* @param col
*/
@Override
public void setValueAt(Object arg0, int row, int col) {
DirectoryServiceClassItem service = (DirectoryServiceClassItem) rows.get(row);
DirectoryDiscountLevelItem level = (DirectoryDiscountLevelItem) columns.get(col);
if(service == null || level == null) {
return;
}
Integer res = (Integer)arg0;
if (res == null) {
res = 0;
}
discounts.setValue(level, service, res);
}
}