1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
| package view;
import javax.swing.table.AbstractTableModel;
import controller.Controller;
import enumeration.*;
@SuppressWarnings("serial")
public class TableModel extends AbstractTableModel {
private Object[][] data;
private String[] title;
private Controller controller;
public TableModel (Object[][] data, String[] title, Controller controller) {
this.data = data;
this.title = title;
this.controller = controller;
}
@Override
public int getColumnCount () {
return this.title.length;
}
@Override
public int getRowCount () {
return this.data.length;
}
public String getColumnName (int col) {
return this.title[col];
}
public boolean isCellEditable (int row, int col) {
return true; // Toutes les cellules sont éditables
}
@Override
public Object getValueAt (int row, int col) {
return this.data[row][col];
}
@Override
public void setValueAt (Object aValue, int rowIndex, int columnIndex) {
if (aValue != null) {
if (aValue instanceof Bac) {
data[rowIndex][columnIndex] = (Bac) aValue;
controller.modifStudent (aValue.toString (), columnIndex, Integer.parseInt ((String) data[rowIndex][controller.getModel ().getCohort ().getIndexID ()]));
}
else if (aValue instanceof Bourse) {
data[rowIndex][columnIndex] = (Bourse) aValue;
controller.modifStudent (aValue.toString (), columnIndex, Integer.parseInt ((String) data[rowIndex][controller.getModel ().getCohort ().getIndexID ()]));
}
else if (aValue instanceof Dut) {
data[rowIndex][columnIndex] = (Dut) aValue;
controller.modifStudent (aValue.toString (), columnIndex, Integer.parseInt ((String) data[rowIndex][controller.getModel ().getCohort ().getIndexID ()]));
}
else if (aValue instanceof PostDUT) {
data[rowIndex][columnIndex] = (PostDUT) aValue;
controller.modifStudent (aValue.toString (), columnIndex, Integer.parseInt ((String) data[rowIndex][controller.getModel ().getCohort ().getIndexID ()]));
}
else if (aValue instanceof Semester) {
data[rowIndex][columnIndex] = (Semester) aValue;
controller.modifStudent (aValue.toString (), columnIndex, Integer.parseInt ((String) data[rowIndex][controller.getModel ().getCohort ().getIndexID ()]));
}
else if (aValue instanceof SemesterPostDut) {
data[rowIndex][columnIndex] = (SemesterPostDut) aValue;
controller.modifStudent (aValue.toString (), columnIndex, Integer.parseInt ((String) data[rowIndex][controller.getModel ().getCohort ().getIndexID ()]));
}
else if (aValue instanceof Sexe) {
data[rowIndex][columnIndex] = (Sexe) aValue;
controller.modifStudent (aValue.toString (), columnIndex, Integer.parseInt ((String) data[rowIndex][controller.getModel ().getCohort ().getIndexID ()]));
}
else if (aValue instanceof Studies) {
data[rowIndex][columnIndex] = (Studies) aValue;
controller.modifStudent (aValue.toString (), columnIndex, Integer.parseInt ((String) data[rowIndex][controller.getModel ().getCohort ().getIndexID ()]));
}
else {
data[rowIndex][columnIndex] = (String) aValue;
controller.modifStudent ((String) aValue, columnIndex, Integer.parseInt ((String) data[rowIndex][controller.getModel ().getCohort ().getIndexID ()]));
}
}
}
@SuppressWarnings("unchecked")
@Override
public Class getColumnClass (int columnIndex) {
controller.getModel ().getModelPrefProperties ().getClassCol ();
switch (controller.getModel ().getModelPrefProperties ().getClassCol ()[columnIndex]) {
case 0:
return Object.class;
case 1:
return Sexe.class;
case 2:
return Bourse.class;
case 3:
return Bac.class;
case 4:
return Semester.class;
case 5:
return SemesterPostDut.class;
case 6:
return Dut.class;
case 7:
return PostDUT.class;
case 8:
return Studies.class;
default:
return Object.class;
}
}
public void addRow (Object[] data) {
int indice = 0, nbRow = this.getRowCount (), nbCol = this.getColumnCount ();
Object temp[][] = this.data;
this.data = new Object[nbRow + 1][nbCol];
for (Object[] value : temp)
this.data[indice++] = value;
this.data[indice] = data;
temp = null;
//Cette méthode permet d'avertir le tableau que les données ont été modifiées
//Ce qui permet une mise à jours complète du tableau
this.fireTableDataChanged ();
}
public void removeRow (int position) {
int indice = 0, indice2 = 0, nbRow = this.getRowCount () - 1, nbCol = this.getColumnCount ();
Object temp[][] = new Object[nbRow][nbCol];
for (Object[] value : this.data) {
if (indice != position) {
temp[indice2++] = value;
}
indice++;
}
this.data = temp;
temp = null;
// Cette méthode permet d'avertir le tableau que les données ont été modifiées
// Ce qui permet une mise à jours complète du tableau
this.fireTableDataChanged ();
}
public void changeStructure () {
this.fireTableStructureChanged ();
}
} |
Partager