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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
| public class OrdonnanceurTableModel extends AbstractTableModel {
public static final int COLUMN_PROCESSUS = 0;
public static final int COLUMN_TEMP_ARRIVE = 1;
public static final int COLUMN_DUREE = 2;
public static final int COLUMN_FIN_EXECUTION = 3;
private List<Data> list;
public OrdonnanceurTableModel() {
this(0);
}
public OrdonnanceurTableModel(int nbProcess) {
this.list = new ArrayList<>();
for(int i=0; i<nbProcess; i++) {
this.list.add(createNewData(i+1));
}
}
@Override
public int getRowCount() {
return list.size();
}
@Override
public int getColumnCount() {
return 4;
}
@Override
public String getColumnName(int column) {
switch(column) {
case COLUMN_PROCESSUS:
return "processus";
case COLUMN_TEMP_ARRIVE:
return "temp arrive";
case COLUMN_DUREE:
return "duree";
case COLUMN_FIN_EXECUTION:
return "finExecution";
}
return null;
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
switch(columnIndex) {
case COLUMN_PROCESSUS:
case COLUMN_FIN_EXECUTION:
return false;
case COLUMN_TEMP_ARRIVE:
case COLUMN_DUREE:
return true;
}
return false;
}
public boolean removeData(int process) {
return removeData(toName(process));
}
public boolean removeData(String name) {
int index = find(p-> p.name.equals(name));
if ( index>=0 ) {
list.remove(index);
fireTableRowsDeleted(index, index);
return true;
}
return false;
}
private int find(int id) {
return find(toName(id));
}
private int find(String name) {
return find(p-> p.name.equals(name));
}
private int find(Predicate<Data> selector) {
for(int i=0;i<list.size(); i++) {
if ( selector.test(list.get(i)) ) {
return i;
}
}
return -1;
}
public void addData(int process) {
int index = find(process);
if ( index<0 ) {
Data data = createNewData(process);
index = list.size();
list.add(data);
fireTableRowsInserted(index, index);
}
}
public void setData(int process, long tempArrive, long duree) {
int index = find(process);
if ( index>=0 ) {
Data data = list.get(index);
data.tempArrive=tempArrive;
data.duree=duree;
fireTableRowsUpdated(index, index);
}
else {
Data data = createNewData(process);
data.tempArrive=tempArrive;
data.duree=duree;
index = list.size();
list.add(data);
fireTableRowsInserted(index, index);
}
}
public void setTempArrive(int process, long tempArrive) {
int index = find(process);
if ( index>=0 ) {
Data data = list.get(index);
data.tempArrive=tempArrive;
fireTableRowsUpdated(index, index);
}
else {
Data data = createNewData(process);
data.tempArrive=tempArrive;
index = list.size();
list.add(data);
fireTableRowsInserted(index, index);
}
}
public void setDuree(int process, long duree) {
int index = find(process);
if ( index>=0 ) {
Data data = list.get(index);
data.duree=duree;
fireTableRowsUpdated(index, index);
}
else {
Data data = createNewData(process);
data.duree=duree;
index = list.size();
list.add(data);
fireTableRowsInserted(index, index);
}
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
switch(columnIndex) {
case COLUMN_PROCESSUS: // processus
return list.get(rowIndex).name;
case COLUMN_TEMP_ARRIVE: // tempArrive
return list.get(rowIndex).tempArrive;
case COLUMN_DUREE: // duree
return list.get(rowIndex).duree;
case COLUMN_FIN_EXECUTION: // fin exécution
return list.get(rowIndex).getFinExecution();
}
return null;
}
@Override
public Class<?> getColumnClass(int columnIndex) {
switch(columnIndex) {
case COLUMN_PROCESSUS: // processus
return String.class;
case COLUMN_TEMP_ARRIVE: // tempArrive
case COLUMN_DUREE: // duree
case COLUMN_FIN_EXECUTION: // fin exécution
return Long.class;
}
return null;
}
@Override
public void setValueAt(Object value, int rowIndex, int columnIndex) {
switch(columnIndex) {
case COLUMN_PROCESSUS: // processus
case COLUMN_FIN_EXECUTION: // fin exécution
break;
case COLUMN_TEMP_ARRIVE: // tempArrive
if ( !Objects.equals(list.get(rowIndex).tempArrive, value) ) {
list.get(rowIndex).tempArrive = (Long) value;
fireTableCellUpdated(rowIndex, columnIndex);
}
break;
case COLUMN_DUREE: // duree
if ( !Objects.equals(list.get(rowIndex).duree, value) ) {
list.get(rowIndex).duree = (Long) value;
fireTableCellUpdated(rowIndex, columnIndex);
}
}
}
private Data createNewData(int id) {
return new Data(toName(id));
}
private String toName(int id) {
return "p"+id;
}
private class Data {
public Long duree;
public Long tempArrive;
private String name;
public Data(String name) {
this.name=name;
}
public Long getFinExecution() {
if ( duree==null || tempArrive==null ) {
return null;
}
return tempArrive + duree;
}
}
} |
Partager