1 package fr.ifremer.tutti.ui.swing.content.operation.catches.species.edit;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import com.google.common.base.Preconditions;
26 import com.google.common.collect.ImmutableSet;
27 import com.google.common.collect.Lists;
28 import com.google.common.collect.Sets;
29 import fr.ifremer.tutti.persistence.entities.data.SampleCategory;
30 import fr.ifremer.tutti.persistence.entities.data.SampleCategoryModel;
31 import fr.ifremer.tutti.persistence.entities.referential.Species;
32 import fr.ifremer.tutti.type.WeightUnit;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.jdesktop.swingx.table.TableColumnModelExt;
36 import org.nuiton.jaxx.application.swing.table.AbstractApplicationTableModel;
37 import org.nuiton.jaxx.application.swing.table.ColumnIdentifier;
38
39 import javax.swing.table.TableColumn;
40 import java.io.Serializable;
41 import java.util.Enumeration;
42 import java.util.List;
43 import java.util.Set;
44
45 import static org.nuiton.i18n.I18n.n;
46
47
48
49
50
51 public class SpeciesBatchTableModel extends AbstractApplicationTableModel<SpeciesBatchRowModel> {
52
53 private static final long serialVersionUID = 1L;
54
55
56 private static final Log log = LogFactory.getLog(SpeciesBatchTableModel.class);
57
58 public static final ColumnIdentifier<SpeciesBatchRowModel> ID = ColumnIdentifier.newId(
59 SpeciesBatchRowModel.PROPERTY_ID,
60 n("tutti.editSpeciesBatch.table.header.id"),
61 n("tutti.editSpeciesBatch.table.header.id.tip"));
62
63 public static final ColumnIdentifier<SpeciesBatchRowModel> SPECIES = ColumnIdentifier.newId(
64 SpeciesBatchRowModel.PROPERTY_SPECIES_ROW,
65 n("tutti.editSpeciesBatch.table.header.species"),
66 n("tutti.editSpeciesBatch.table.header.species.tip"));
67
68 public static final ColumnIdentifier<SpeciesBatchRowModel> WEIGHT = ColumnIdentifier.newId(
69 SpeciesBatchRowModel.PROPERTY_COMPUTED_WEIGHT,
70 n("tutti.editSpeciesBatch.table.header.weight"),
71 n("tutti.editSpeciesBatch.table.header.weight.tip"));
72
73 public static final ColumnIdentifier<SpeciesBatchRowModel> COMPUTED_NUMBER = ColumnIdentifier.newId(
74 SpeciesBatchRowModel.PROPERTY_COMPUTED_NUMBER,
75 n("tutti.editSpeciesBatch.table.header.computedNumber"),
76 n("tutti.editSpeciesBatch.table.header.computedNumber.tip"));
77
78 public static final ColumnIdentifier<SpeciesBatchRowModel> COMMENT = ColumnIdentifier.newId(
79 SpeciesBatchRowModel.PROPERTY_COMMENT,
80 n("tutti.editSpeciesBatch.table.header.comment"),
81 n("tutti.editSpeciesBatch.table.header.comment.tip"));
82
83 public static final ColumnIdentifier<SpeciesBatchRowModel> ATTACHMENT = ColumnIdentifier.newReadOnlyId(
84 SpeciesBatchRowModel.PROPERTY_ATTACHMENT,
85 n("tutti.editSpeciesBatch.table.header.file"),
86 n("tutti.editSpeciesBatch.table.header.file.tip"));
87
88 public static final ColumnIdentifier<SpeciesBatchRowModel> SPECIES_TO_CONFIRM = ColumnIdentifier.newId(
89 SpeciesBatchRowModel.PROPERTY_SPECIES_TO_CONFIRM,
90 "",
91
92
93
94
95
96
97
98 protected final Set<ColumnIdentifier<SpeciesBatchRowModel>> frequencyCols;
99
100
101
102
103
104
105 protected final Set<SampleCategoryColumnIdentifier> sampleCols;
106
107
108
109
110
111
112 protected final SampleCategoryModel sampleCategoryModel;
113
114
115
116
117
118
119 protected final WeightUnit weightUnit;
120
121 public SpeciesBatchTableModel(WeightUnit weightUnit,
122 SampleCategoryModel sampleCategoryModel,
123 TableColumnModelExt columnModel) {
124 super(columnModel, false, false);
125 this.weightUnit = weightUnit;
126 this.sampleCategoryModel = sampleCategoryModel;
127 setNoneEditableCols(ID, SPECIES);
128
129 frequencyCols = Sets.newHashSet();
130 frequencyCols.add(COMPUTED_NUMBER);
131 frequencyCols.add(WEIGHT);
132
133 Set<SampleCategoryColumnIdentifier> sampleCols = Sets.newLinkedHashSet();
134
135 Enumeration<TableColumn> columns = columnModel.getColumns();
136 while (columns.hasMoreElements()) {
137 TableColumn tableColumn = columns.nextElement();
138 Object identifier = tableColumn.getIdentifier();
139 if (identifier instanceof SampleCategoryColumnIdentifier) {
140 sampleCols.add((SampleCategoryColumnIdentifier) identifier);
141 }
142 }
143 this.sampleCols = ImmutableSet.copyOf(sampleCols);
144 }
145
146 public Set<SampleCategoryColumnIdentifier> getSampleCols() {
147 return sampleCols;
148 }
149
150 @Override
151 protected void collectShell(SpeciesBatchRowModel row,
152 Set<SpeciesBatchRowModel> collectedRows) {
153 super.collectShell(row, collectedRows);
154
155 if (!row.isChildBatchsEmpty()) {
156 for (SpeciesBatchRowModel child : row.getChildBatch()) {
157 collectShell(child, collectedRows);
158 }
159 }
160 }
161
162 @Override
163 public SpeciesBatchRowModel createNewRow() {
164 SpeciesBatchRowModel result =
165 new SpeciesBatchRowModel(weightUnit, sampleCategoryModel);
166
167
168 result.setValid(false);
169 return result;
170 }
171
172 @Override
173 public void setValueAt(Object aValue,
174 int rowIndex,
175 int columnIndex,
176 ColumnIdentifier<SpeciesBatchRowModel> propertyName,
177 SpeciesBatchRowModel entry) {
178 if (sampleCols.contains(propertyName)) {
179
180 SampleCategoryColumnIdentifier sampleCategoryColumnIdentifier = (SampleCategoryColumnIdentifier) propertyName;
181 sampleCategoryColumnIdentifier.setWeightValue(entry, aValue);
182
183
184 Integer sampleCategoryId = sampleCategoryColumnIdentifier.getSampleCategoryId();
185 if (log.isDebugEnabled()) {
186 log.debug("Sample category: " + sampleCategoryId + " modified at row: " + rowIndex);
187 }
188 SampleCategory<?> sampleCategory = entry.getSampleCategoryById(sampleCategoryId);
189 SpeciesBatchRowModel firstAncestor = entry.getFirstAncestor(sampleCategory);
190
191 int firstRowIndex = getRowIndex(firstAncestor);
192 if (log.isDebugEnabled()) {
193 log.debug("First ancestor row: " + firstRowIndex);
194 }
195
196
197 updateShell(firstAncestor, columnIndex);
198
199 } else {
200 super.setValueAt(aValue, rowIndex, columnIndex, propertyName, entry);
201 }
202 }
203
204 @Override
205 protected boolean isCellEditable(int rowIndex,
206 int columnIndex,
207 ColumnIdentifier<SpeciesBatchRowModel> propertyName) {
208
209 boolean result = super.isCellEditable(rowIndex,
210 columnIndex,
211 propertyName);
212 if (result) {
213
214 if (frequencyCols.contains(propertyName)) {
215
216
217 SpeciesBatchRowModel entry = getEntry(rowIndex);
218 result = entry.isBatchLeaf();
219
220 } else if (sampleCols.contains(propertyName)) {
221
222
223 SpeciesBatchRowModel entry = getEntry(rowIndex);
224
225
226 Species species = entry.getSpecies();
227
228 if (species == null) {
229
230
231 result = false;
232 } else {
233
234
235
236
237 SampleCategory<?> value =
238 (SampleCategory<?>) propertyName.getValue(entry);
239
240 result = value.getCategoryValue() != null;
241
242 }
243 }
244 }
245 return result;
246 }
247
248
249
250
251
252
253
254
255
256 public Integer getNextEditableFrequencyRow(Integer rowIndex) {
257 Integer result = null;
258
259 for (int i = rowIndex, max = getRowCount(); i < max; i++) {
260 SpeciesBatchRowModel entry = getEntry(i);
261 if (entry.isBatchLeaf()) {
262 result = i;
263 break;
264 }
265 }
266 return result;
267 }
268
269
270
271
272
273
274
275
276
277
278 public final int getNextChildRowIndex(SpeciesBatchRowModel row) {
279
280
281 int parentRowIndex = getRowIndex(row);
282
283
284 Preconditions.checkState(getRowIndex(row) != -1);
285
286 int result = parentRowIndex;
287
288 if (!row.isChildBatchsEmpty()) {
289
290
291 SpeciesBatchRowModel lastChild =
292 row.getChildBatch().get(row.sizeChildBatchs() - 1);
293
294
295 Set<SpeciesBatchRowModel> childs = Sets.newHashSet();
296 childs.add(lastChild);
297 lastChild.collectShell(childs);
298
299
300 for (SpeciesBatchRowModel child : childs) {
301 int childRowIndex = getRowIndex(child);
302 result = Math.max(childRowIndex, result);
303 }
304 }
305
306
307 result++;
308
309 return result;
310 }
311
312
313
314
315
316
317
318
319
320
321 public Integer getSampleCategoryId(int columnIndex) {
322 Integer result = null;
323 ColumnIdentifier<SpeciesBatchRowModel> identifier = getIdentifier(columnIndex);
324 if (sampleCols.contains(identifier)) {
325 SampleCategoryColumnIdentifier sampleId = (SampleCategoryColumnIdentifier) identifier;
326 result = sampleId.getSampleCategoryId();
327 }
328 return result;
329 }
330
331
332
333
334
335
336
337
338
339
340 public Serializable updateSampleCategorieValue(SpeciesBatchRowModel row,
341 int columnIndex,
342 Serializable newValue) {
343
344 SampleCategoryColumnIdentifier identifier = (SampleCategoryColumnIdentifier) getIdentifier(columnIndex);
345
346 Serializable oldValue = identifier.getCategoryValue(row);
347
348
349 identifier.setCategoryValue(row, newValue);
350
351
352 updateShell(row, columnIndex);
353
354 return oldValue;
355
356 }
357
358
359
360
361
362
363
364
365 public SpeciesBatchRowModel getPreviousSibling(SpeciesBatchRowModel row) {
366 SpeciesBatchRowModel result = null;
367 SpeciesBatchRowModel parentBatch = row.getParentBatch();
368 if (parentBatch != null) {
369 List<SpeciesBatchRowModel> childBatch = parentBatch.getChildBatch();
370 int i = childBatch.indexOf(row);
371 if (i > 0) {
372 result = childBatch.get(i - 1);
373 }
374 }
375 return result;
376 }
377
378
379
380
381
382
383
384
385 public void updateShell(Set<SpeciesBatchRowModel> shell, int columnIndex) {
386 for (SpeciesBatchRowModel batchRowModel : shell) {
387 int currentRowIndex = getRowIndex(batchRowModel);
388 if (log.isDebugEnabled()) {
389 log.debug("Update shell row: " + currentRowIndex);
390 }
391 fireTableCellUpdated(currentRowIndex, columnIndex);
392 }
393 }
394
395
396
397
398
399
400
401
402 public List<Species> getSpeciesList() {
403 List<Species> result = Lists.newArrayList();
404 for (SpeciesBatchRowModel row : rows) {
405 if (row.isBatchRoot()) {
406 result.add(row.getSpecies());
407 }
408 }
409 return result;
410 }
411
412
413
414
415
416
417
418
419 protected void updateShell(SpeciesBatchRowModel entry, int columnIndex) {
420
421 Set<SpeciesBatchRowModel> shell = Sets.newHashSet();
422 entry.collectShell(shell);
423 shell.add(entry);
424 updateShell(shell, columnIndex);
425 }
426 }