1 package fr.ifremer.tutti.ui.swing.content.operation.catches.species.split;
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 fr.ifremer.tutti.ui.swing.content.operation.catches.species.SelectedCategoryAble;
26 import org.jdesktop.swingx.table.TableColumnModelExt;
27 import org.nuiton.jaxx.application.swing.table.AbstractApplicationTableModel;
28 import org.nuiton.jaxx.application.swing.table.ColumnIdentifier;
29
30 import java.util.List;
31 import java.util.Optional;
32
33 import static org.nuiton.i18n.I18n.n;
34
35
36
37
38
39
40
41 public class SplitSpeciesBatchTableModel
42 extends AbstractApplicationTableModel<SplitSpeciesBatchRowModel> {
43
44 private static final long serialVersionUID = 1L;
45
46 public static final ColumnIdentifier<SplitSpeciesBatchRowModel> SELECTED = ColumnIdentifier.newId(
47 SplitSpeciesBatchRowModel.PROPERTY_SELECTED,
48 n("tutti.splitSpeciesBatch.table.header.selected"),
49 n("tutti.splitSpeciesBatch.table.header.selected"));
50
51 public static final ColumnIdentifier<SplitSpeciesBatchRowModel> EDITABLE_CATEGORY_VALUE = ColumnIdentifier.newId(
52 SplitSpeciesBatchRowModel.PROPERTY_CATEGORY_VALUE,
53 n("tutti.splitSpeciesBatch.table.header.category"),
54 n("tutti.splitSpeciesBatch.table.header.category"));
55
56 public static final ColumnIdentifier<SplitSpeciesBatchRowModel> READ_ONLY_CATEGORY_VALUE = ColumnIdentifier.newId(
57 SplitSpeciesBatchRowModel.PROPERTY_CATEGORY_VALUE,
58 n("tutti.splitSpeciesBatch.table.header.category"),
59 n("tutti.splitSpeciesBatch.table.header.category"));
60
61 public static final ColumnIdentifier<SplitSpeciesBatchRowModel> WEIGHT = ColumnIdentifier.newId(
62 SplitSpeciesBatchRowModel.PROPERTY_WEIGHT,
63 n("tutti.splitSpeciesBatch.table.header.weight"),
64 n("tutti.splitSpeciesBatch.table.header.weight"));
65
66 private final SelectedCategoryAble uiModel;
67
68
69
70
71
72
73 private final boolean splitMode;
74
75 public SplitSpeciesBatchTableModel(TableColumnModelExt columnModel,
76 SelectedCategoryAble uiModel,
77 boolean createEmptyRowIsEmpty,
78 boolean splitMode) {
79 super(columnModel, createEmptyRowIsEmpty, createEmptyRowIsEmpty);
80 this.uiModel = uiModel;
81 this.splitMode = splitMode;
82 setNoneEditableCols(READ_ONLY_CATEGORY_VALUE);
83 }
84
85 @Override
86 public SplitSpeciesBatchRowModel createNewRow() {
87 SplitSpeciesBatchRowModel result = new SplitSpeciesBatchRowModel();
88 result.setCategoryType(uiModel.getSelectedCategory());
89 result.setValid(false);
90 result.setEditable(true);
91 return result;
92 }
93
94 @Override
95 protected boolean isCellEditable(int rowIndex,
96 int columnIndex,
97 ColumnIdentifier<SplitSpeciesBatchRowModel> propertyName) {
98
99 boolean result = super.isCellEditable(rowIndex,
100 columnIndex,
101 propertyName);
102 if (result && !splitMode) {
103
104 SplitSpeciesBatchRowModel entry = getEntry(rowIndex);
105 result = entry.isEditable();
106 }
107 return result;
108 }
109
110 public Optional<Float> getTotalWeight() {
111
112 boolean found = false;
113 float result = 0f;
114 List<SplitSpeciesBatchRowModel> rows = getRows();
115 for (SplitSpeciesBatchRowModel row : rows) {
116 if (row.isSelected()) {
117 Float weight = row.getWeight();
118 if (weight != null) {
119 found = true;
120 result += weight;
121 }
122 }
123 }
124 return Optional.ofNullable(found ? result : null);
125
126 }
127 }