/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package clips.contract.info.packet;
import clips.delegate.directory.complex.DirectoryPacketTemplateItem;
import clips.delegate.directory.complex.DirectoryPacketTemplateListItem;
import cli_fmw.utils.ErrorValue;
import cli_fmw.utils.MessageBox;
import cli_fmw.main.ClipsException;
import java.util.ArrayList;
import java.util.Comparator;
import javax.swing.table.AbstractTableModel;
/**
*
* @author petr
*/
public class TableModelPacketServices extends AbstractTableModel {
public static final int COLCOUNT = 4;
public static final int COL_SERVICE = 0;
public static final int COL_WEIGHT = 1;
public static final int COL_DURATION = 2;
public static final int COL_DISCOUNT = 3;
private DirectoryPacketTemplateItem packet;
private ArrayList<DirectoryPacketTemplateListItem> list = new ArrayList<DirectoryPacketTemplateListItem>();
public TableModelPacketServices(DirectoryPacketTemplateItem packet) throws ClipsException {
this.packet = packet;
list.clear();
if (packet != null) {
for (int i = 0; i < packet.getPacketItemCount(); i++) {
list.add(packet.getPacketItem(i));
}
}
java.util.Collections.sort(list, new PacketItemComparator());
}
@Override
public Class<?> getColumnClass(int col) {
if (col == COL_DISCOUNT || col == COL_DURATION || col == COL_WEIGHT) {
return Integer.class;
}
return super.getColumnClass(col);
}
@Override
public String getColumnName(int column) {
switch (column) {
case COL_SERVICE: return "Услуга";
case COL_WEIGHT: return "Вес услуги";
case COL_DURATION: return "Продолжительность";
case COL_DISCOUNT: return "Скидка";
default: return "";
}
}
@Override
public boolean isCellEditable(int rowIndex, int col) {
return false;
}
@Override
public void setValueAt(Object aValue, int rowIndex, int col) {
try {
DirectoryPacketTemplateListItem item = list.get(rowIndex);
if (col == COL_DISCOUNT) {
item.setDiscount(Integer.parseInt(aValue.toString()));
}
if (col == COL_DURATION) {
item.setDefaultDuration(Integer.parseInt(aValue.toString()));
}
if (col == COL_WEIGHT) {
item.setWeight(Integer.parseInt(aValue.toString()));
}
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
}
@Override
public int getRowCount() {
return list.size();
}
@Override
public int getColumnCount() {
return COLCOUNT;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
DirectoryPacketTemplateListItem item = list.get(rowIndex);
switch (columnIndex) {
case COL_SERVICE: {
try {
return item.getService();
} catch (ClipsException e) {
return new ErrorValue(e);
}
}
case COL_WEIGHT: return item.getWeight();
case COL_DURATION: return item.getDefaultDuration();
case COL_DISCOUNT: return item.getDiscount();
default: return null;
}
}
class PacketItemComparator implements Comparator {
@Override
public int compare(Object o1, Object o2) {
DirectoryPacketTemplateListItem p1 = (DirectoryPacketTemplateListItem) o1;
DirectoryPacketTemplateListItem p2 = (DirectoryPacketTemplateListItem) o2;
if (p1.getWeight() < p2.getWeight()) {
return 1;
} else {
return -1;
}
}
}
}