1 package fr.ifremer.reefdb.ui.swing.content.observation.shared;
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 import com.google.common.collect.ImmutableList;
27 import fr.ifremer.quadrige3.core.dao.technical.Assert;
28 import fr.ifremer.quadrige3.ui.swing.table.SwingTable;
29 import fr.ifremer.quadrige3.ui.swing.table.state.SwingTableSessionState;
30 import fr.ifremer.reefdb.dto.ReefDbBeanFactory;
31 import fr.ifremer.reefdb.dto.ReefDbBeans;
32 import fr.ifremer.reefdb.dto.data.measurement.MeasurementAware;
33 import fr.ifremer.reefdb.dto.data.measurement.MeasurementDTO;
34 import fr.ifremer.reefdb.dto.referential.pmfm.PmfmDTO;
35 import fr.ifremer.reefdb.ui.swing.util.ReefDbUI;
36 import fr.ifremer.reefdb.ui.swing.util.table.ReefDbColumnIdentifier;
37 import fr.ifremer.reefdb.ui.swing.util.table.renderer.MultipleValueCellRenderer;
38 import jaxx.runtime.swing.session.State;
39 import org.nuiton.jaxx.application.swing.util.Cancelable;
40
41 import javax.swing.table.TableColumn;
42 import java.util.ArrayList;
43 import java.util.List;
44 import java.util.Objects;
45
46
47
48
49 public abstract class AbstractMeasurementsMultiEditUIHandler<
50 R extends AbstractMeasurementsGroupedRowModel<MeasurementDTO, R>,
51 M extends AbstractMeasurementsMultiEditUIModel<MeasurementDTO, R, M>,
52 UI extends ReefDbUI<M, ?>
53 >
54 extends AbstractMeasurementsGroupedTableUIHandler<R, M, UI>
55 implements Cancelable {
56
57
58
59
60 public AbstractMeasurementsMultiEditUIHandler() {
61 super(R.PROPERTY_TAXON_GROUP,
62 R.PROPERTY_TAXON,
63 R.PROPERTY_INDIVIDUAL_PMFMS,
64 R.PROPERTY_COMMENT);
65 }
66
67
68
69
70
71
72 protected abstract SwingTable getReferentTable();
73
74
75
76
77
78
79
80 @Override
81 protected List<R> buildRows(boolean readOnly) {
82
83 if (getModel().getRowsToEdit().isEmpty())
84 return null;
85
86
87 R multiEditRow = createNewRow(readOnly, null);
88 multiEditRow.setIndividualPmfms(new ArrayList<>(getModel().getPmfms()));
89 ReefDbBeans.createEmptyMeasurements(multiEditRow);
90
91 boolean firstRow = true;
92 for (R row : getModel().getRowsToEdit()) {
93
94
95 findMultipleValueOnIdentifier(row, multiEditRow, firstRow);
96
97
98 findMultipleValueOnPmfmIdentifier(row, multiEditRow, firstRow);
99
100 firstRow = false;
101 }
102
103 overrideColumnRenderersAndEditors(multiEditRow);
104
105 overrideColumnWidthAndPosition();
106
107 return ImmutableList.of(multiEditRow);
108 }
109
110
111 private void findMultipleValueOnIdentifier(R row, R multiEditRow, boolean firstRow) {
112
113 for (ReefDbColumnIdentifier<R> identifier : getModel().getIdentifiersToCheck()) {
114 if (!multiEditRow.getMultipleValuesOnIdentifier().contains(identifier)) {
115 if (!Objects.equals(identifier.getValue(row), identifier.getValue(multiEditRow))) {
116 if (firstRow) {
117
118 identifier.setValue(multiEditRow, identifier.getValue(row));
119 } else {
120
121 identifier.setValue(multiEditRow, null);
122 multiEditRow.addMultipleValuesOnIdentifier(identifier);
123 }
124 }
125 }
126 }
127 }
128
129
130 private void findMultipleValueOnPmfmIdentifier(R row, R multiEditRow, boolean firstRow) {
131
132 for (PmfmDTO pmfm : getModel().getPmfms()) {
133 if (!multiEditRow.getMultipleValuesOnPmfmIds().contains(pmfm.getId())) {
134 MeasurementDTO multiMeasurement = ReefDbBeans.getIndividualMeasurementByPmfmId(multiEditRow, pmfm.getId());
135 Assert.notNull(multiMeasurement, "should be already present");
136 MeasurementDTO measurement = ReefDbBeans.getIndividualMeasurementByPmfmId(row, pmfm.getId());
137 if (measurement == null) {
138
139 measurement = ReefDbBeanFactory.newMeasurementDTO();
140 measurement.setPmfm(pmfm);
141 }
142
143 if (!ReefDbBeans.measurementValuesEquals(multiMeasurement, measurement)) {
144 if (firstRow) {
145
146 multiMeasurement.setNumericalValue(measurement.getNumericalValue());
147 multiMeasurement.setQualitativeValue(measurement.getQualitativeValue());
148 } else {
149
150 multiMeasurement.setNumericalValue(null);
151 multiMeasurement.setQualitativeValue(null);
152 multiEditRow.addMultipleValuesOnPmfmId(pmfm.getId());
153 }
154 }
155 }
156 }
157 }
158
159
160
161
162
163
164 private void overrideColumnRenderersAndEditors(R multiEditRow) {
165
166 multiEditRow.getMultipleValuesOnIdentifier().forEach(identifier -> {
167 TableColumn col = getTable().getColumnExt(identifier);
168
169 col.setCellRenderer(new MultipleValueCellRenderer(col.getCellRenderer()));
170 });
171 multiEditRow.getMultipleValuesOnPmfmIds().forEach(pmfmId ->
172 getModel().getPmfmColumns().stream().filter(pmfmTableColumn -> pmfmTableColumn.getPmfmId() == pmfmId).findFirst()
173 .ifPresent(pmfmTableColumn -> {
174
175 pmfmTableColumn.setCellRenderer(new MultipleValueCellRenderer(pmfmTableColumn.getCellRenderer()));
176
177 if (getModel().getReadOnlyPmfmIds().contains(pmfmId))
178 pmfmTableColumn.setEditable(false);
179 }
180 ));
181 }
182
183 private void overrideColumnWidthAndPosition() {
184
185
186 State referentState = getContext().getSwingSession().findStateByComponentName(".*" + getReferentTable().getName());
187
188
189 if (referentState instanceof SwingTableSessionState) {
190 referentState.setState(getTable(), referentState);
191 }
192 }
193
194 @Override
195 protected void onRowsAdded(List<R> addedRows) {
196
197
198 }
199
200 @Override
201 protected List<? extends MeasurementAware> getMeasurementAwareModels() {
202
203 return null;
204 }
205
206 @Override
207 protected MeasurementAware getMeasurementAwareModelForRow(R row) {
208
209 return null;
210 }
211
212 @Override
213 protected void filterMeasurements() {
214 }
215
216 public void valid() {
217 if (getModel().isValid()) {
218 closeDialog();
219 }
220 }
221
222 @Override
223 public void cancel() {
224 stopListenValidatorValid(getValidator());
225 getModel().setValid(false);
226 closeDialog();
227 }
228
229 }