package Interface;
import java.util.Vector;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
import com.sun.corba.se.impl.io.FVDCodeBaseImpl;
import Logica.DecimalPlaces;
import Logica.rekeningItem;
public class AfrekenTable extends AbstractTableModel {
private String[] fColumnName = {"Product", "Prijs", "Hoeveelheid", "Al afgerekend", "Nog te betalen", "Nu betalen"};
private Vector<rekeningItem> fData;
private JLabel fNuBetalen;
private Vector<Integer> fBetalen;
public AfrekenTable(Vector<rekeningItem> rek, JLabel nuBetalen){
fData = rek;
fNuBetalen = nuBetalen;
fBetalen = new Vector<Integer>();
for(int i = 0; i < fData.size(); i++){
fBetalen.add(i, fData.get(i).getHoeveelheid() - fData.get(i).getAfgerekend());
}
calculateTotaal();
}
@Override
public int getColumnCount() {
return fColumnName.length;
}
@Override
public int getRowCount() {
return fBetalen.size();
}
@Override
public String getColumnName(int col) {
return fColumnName[col].toString();
}
@Override
public Object getValueAt(int row, int colm) {
rekeningItem rek = fData.get(row);
switch (colm) {
case 0:
return rek.getProduct().getName();
case 1:
return "€" + DecimalPlaces.set2decimals(rek.getProduct().getPrijs());
case 2:
return rek.getHoeveelheid();
case 3:
return rek.getAfgerekend();
case 4:
return rek.getHoeveelheid() - rek.getAfgerekend();
case 5:
return fBetalen.get(row);
default:
return 0;
}
}
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col != 5) {
return false;
} else {
return true;
}
}
public void setValueAt(Object value, int row, int col) {
if(col == 5){
String valueString = value.toString();
if(valueString.length() == 0)valueString = "0";
try{
fBetalen.setElementAt(Integer.parseInt(valueString), row);
}catch(Exception e){
JOptionPane.showMessageDialog(null, "Er ging iets mis met de invoer. Kijk de invoer na.\n" +
e.getMessage());
}
}
fireTableRowsUpdated(row, row);
calculateTotaal();
}
private void calculateTotaal(){
double NuBetalen = 0;
for(int i = 0; i < getRowCount(); i++){
NuBetalen += (fData.get(i).getProduct().getPrijs() * fBetalen.get(i));
}
fNuBetalen.setText("€" + DecimalPlaces.set2decimals(NuBetalen));
}
@SuppressWarnings("unchecked")
@Override
public Class getColumnClass(int c){
switch (c) {
case 0:
return String.class;
case 1:
return String.class;
case 2:
return Integer.class;
case 3:
return Integer.class;
case 4:
return Integer.class;
case 5:
return Integer.class;
default:
return Object.class;
}
}
}