1 package fr.ifremer.dali.ui.swing.content.observation.operation.measurement;
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.base.Predicate;
27 import com.google.common.collect.Lists;
28 import fr.ifremer.dali.decorator.DecoratorService;
29 import fr.ifremer.dali.dto.DaliBeans;
30 import fr.ifremer.dali.dto.configuration.programStrategy.PmfmStrategyDTO;
31 import fr.ifremer.dali.dto.data.sampling.SamplingOperationDTO;
32 import fr.ifremer.dali.dto.referential.pmfm.PmfmDTO;
33 import fr.ifremer.dali.ui.swing.content.observation.ObservationUIModel;
34 import fr.ifremer.dali.ui.swing.content.observation.operation.measurement.grouped.OperationMeasurementsFilter;
35 import fr.ifremer.dali.ui.swing.content.observation.operation.measurement.grouped.OperationMeasurementsGroupedTableUIModel;
36 import fr.ifremer.dali.ui.swing.content.observation.operation.measurement.ungrouped.OperationMeasurementsUngroupedTableUIModel;
37 import fr.ifremer.dali.ui.swing.util.AbstractDaliBeanUIModel;
38 import fr.ifremer.dali.ui.swing.util.AbstractDaliUIHandler;
39 import fr.ifremer.dali.ui.swing.util.DaliUIs;
40 import jaxx.runtime.SwingUtil;
41 import jaxx.runtime.validator.swing.SwingValidator;
42 import org.apache.commons.collections4.CollectionUtils;
43 import org.nuiton.jaxx.application.swing.tab.TabHandler;
44
45 import java.util.List;
46 import java.util.stream.Collectors;
47
48
49
50
51 public class OperationMeasurementsTabUIHandler extends AbstractDaliUIHandler<OperationMeasurementsTabUIModel, OperationMeasurementsTabUI> implements TabHandler {
52
53
54
55
56 @Override
57 public void beforeInit(OperationMeasurementsTabUI ui) {
58 super.beforeInit(ui);
59
60
61 final OperationMeasurementsTabUIModel model = new OperationMeasurementsTabUIModel();
62 ui.setContextValue(model);
63
64 }
65
66
67
68
69 @Override
70 public void afterInit(OperationMeasurementsTabUI ui) {
71 initUI(ui);
72
73
74 initComboBox();
75
76 getModel().setUngroupedTableUIModel(getUI().getUngroupedTable().getModel());
77 getModel().setGroupedTableUIModel(getUI().getGroupedTable().getModel());
78
79
80 SwingUtil.setComponentWidth(getUI().getLeftImage(), ui.getMenuPanel().getPreferredSize().width * 9 / 10);
81 getUI().getLeftImage().setScaled(true);
82
83
84 initListeners();
85
86 registerValidators(getValidator());
87 listenValidatorValid(getValidator(), getModel());
88 }
89
90
91
92
93 private void initComboBox() {
94
95
96 initBeanFilterableComboBox(
97 getUI().getSelectionPrelevementsCombo(),
98 getModel().getSamplings(),
99 getModel().getSampling(),
100 DecoratorService.CONCAT);
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 DaliUIs.forceComponentSize(getUI().getSelectionPrelevementsCombo());
141
142
143 }
144
145
146
147
148 private void initListeners() {
149
150 getModel().addPropertyChangeListener(OperationMeasurementsTabUIModel.PROPERTY_OBSERVATION_MODEL, evt -> load(getModel().getObservationModel()));
151
152
153 getModel().addPropertyChangeListener(OperationMeasurementsTabUIModel.PROPERTY_SAMPLING, evt -> {
154
155 if (getModel().isAdjusting()) return;
156
157 getContext().setSelectedSamplingOperationId(getModel().getSampling() != null ? getModel().getSampling().getId() : null);
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 getModel().getUngroupedTableUIModel().addPropertyChangeListener(OperationMeasurementsUngroupedTableUIModel.PROPERTY_ROWS_IN_ERROR, evt -> {
183
184 getValidator().doValidate();
185 });
186 getModel().getGroupedTableUIModel().addPropertyChangeListener(OperationMeasurementsGroupedTableUIModel.PROPERTY_ROWS_IN_ERROR, evt -> {
187
188 getValidator().doValidate();
189 });
190
191
192 getModel().addPropertyChangeListener(AbstractDaliBeanUIModel.PROPERTY_LOADING, evt -> {
193 getModel().getUngroupedTableUIModel().setLoading((Boolean) evt.getNewValue());
194 getModel().getGroupedTableUIModel().setLoading((Boolean) evt.getNewValue());
195 });
196
197
198 listenModelModify(getModel().getUngroupedTableUIModel());
199 listenModelModify(getModel().getGroupedTableUIModel());
200
201 }
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238 @Override
239 public SwingValidator<OperationMeasurementsTabUIModel> getValidator() {
240 return getUI().getValidator();
241 }
242
243
244
245
246
247
248 private void load(final ObservationUIModel survey) {
249
250 if (survey == null || survey.getId() == null) {
251 return;
252 }
253
254 getModel().setAdjusting(true);
255
256 List<PmfmStrategyDTO> pmfmStrategies = DaliBeans.filterCollection(survey.getPmfmStrategies(), (Predicate<PmfmStrategyDTO>) input -> input != null && input.isSampling());
257
258
259 getUI().getSelectionPrelevementsCombo().setData(getModel().getSamplings());
260
261
262 {
263
264
265 List<PmfmDTO> pmfms = pmfmStrategies.stream()
266 .filter(pmfmStrategy -> !pmfmStrategy.isGrouping())
267 .map(PmfmStrategyDTO::getPmfm)
268 .collect(Collectors.toList());
269
270
271 if (CollectionUtils.isNotEmpty(getModel().getSamplings())) {
272
273 getModel().getSamplings().forEach(samplingOperation -> DaliBeans.fillListsEachOther(samplingOperation.getPmfms(), pmfms));
274 }
275
276
277 getModel().getUngroupedTableUIModel().setPmfms(pmfms);
278
279 getModel().getUngroupedTableUIModel().setSurvey(survey);
280 }
281
282
283 {
284
285 List<PmfmDTO> individualPmfms = Lists.newArrayList();
286
287 List<PmfmDTO> uniquePmfms = Lists.newArrayList();
288 for (PmfmStrategyDTO pmfmStrategy : pmfmStrategies) {
289 if (pmfmStrategy.isGrouping()) {
290 individualPmfms.add(pmfmStrategy.getPmfm());
291 if (pmfmStrategy.isUnique()) {
292 uniquePmfms.add(pmfmStrategy.getPmfm());
293 }
294 }
295 }
296
297
298 if (CollectionUtils.isNotEmpty(getModel().getSamplings())) {
299
300 getModel().getSamplings().forEach(samplingOperation -> DaliBeans.fillListsEachOther(samplingOperation.getIndividualPmfms(), individualPmfms));
301 }
302
303
304 getModel().getGroupedTableUIModel().setPmfms(individualPmfms);
305 getModel().getGroupedTableUIModel().setUniquePmfms(uniquePmfms);
306
307 getModel().getGroupedTableUIModel().setSurvey(survey);
308 }
309
310
311 if (getContext().getSelectedSamplingOperationId() != null && getModel().getSamplings() != null) {
312 for (SamplingOperationDTO samplingOperation : getModel().getSamplings()) {
313 if (getContext().getSelectedSamplingOperationId().equals(samplingOperation.getId())) {
314 getModel().setSampling(samplingOperation);
315 break;
316 }
317 }
318 }
319
320
321 if (CollectionUtils.isEmpty(getModel().getSamplings())) {
322 setEnabled(getUI(), false);
323 }
324
325
326 doSearch();
327
328 getModel().setAdjusting(false);
329 }
330
331
332
333
334 public void clearSearch() {
335
336
337
338
339
340
341
342
343 getModel().setSampling(null);
344
345 }
346
347
348
349
350 public void doSearch() {
351
352
353 getModel().getUngroupedTableUIModel().setSamplingFilter(getModel().getSampling());
354
355 OperationMeasurementsFilter measurementFilter = new OperationMeasurementsFilter();
356
357
358 measurementFilter.setSamplingOperation(getModel().getSampling());
359 getModel().getGroupedTableUIModel().setMeasurementFilter(measurementFilter);
360 }
361
362 public void save() {
363
364 try {
365
366
367 getModel().getUngroupedTableUIModel().setSamplingFilter(null);
368 getModel().getGroupedTableUIModel().setMeasurementFilter(null);
369
370
371 getUI().getUngroupedTable().getHandler().save();
372
373
374 getUI().getGroupedTable().getHandler().save();
375
376 } finally {
377
378 doSearch();
379 }
380 }
381
382
383
384
385 @Override
386 public boolean onHideTab(int currentIndex, int newIndex) {
387 return true;
388 }
389
390
391
392
393 @Override
394 public void onShowTab(int currentIndex, int newIndex) {
395 }
396
397
398
399
400 @Override
401 public boolean onRemoveTab() {
402 return false;
403 }
404
405 }