1 package fr.ifremer.dali.ui.swing.content.home;
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 fr.ifremer.dali.dto.configuration.programStrategy.ProgramDTO;
27 import fr.ifremer.dali.dto.data.survey.CampaignDTO;
28 import fr.ifremer.dali.dto.data.survey.SurveyFilterDTO;
29 import fr.ifremer.dali.dto.enums.FilterTypeValues;
30 import fr.ifremer.dali.dto.enums.SearchDateValues;
31 import fr.ifremer.dali.dto.referential.LocationDTO;
32 import fr.ifremer.dali.service.DaliTechnicalException;
33 import fr.ifremer.dali.ui.swing.action.QuitScreenAction;
34 import fr.ifremer.dali.ui.swing.content.home.map.SurveysMapUI;
35 import fr.ifremer.dali.ui.swing.content.home.operation.OperationsTableUIModel;
36 import fr.ifremer.dali.ui.swing.content.home.survey.SurveysTableRowModel;
37 import fr.ifremer.dali.ui.swing.content.home.survey.SurveysTableUIModel;
38 import fr.ifremer.dali.ui.swing.util.AbstractDaliBeanUIModel;
39 import fr.ifremer.dali.ui.swing.util.AbstractDaliUIHandler;
40 import fr.ifremer.dali.ui.swing.util.DaliUIs;
41 import fr.ifremer.dali.ui.swing.util.map.MapParentUIModel;
42 import jaxx.runtime.SwingUtil;
43 import jaxx.runtime.validator.swing.SwingValidator;
44 import org.apache.commons.logging.Log;
45 import org.apache.commons.logging.LogFactory;
46 import org.nuiton.jaxx.application.swing.util.CloseableUI;
47
48 import javax.swing.JFrame;
49 import javax.swing.SwingUtilities;
50 import java.util.List;
51
52 import static org.nuiton.i18n.I18n.t;
53
54
55
56
57 public class HomeUIHandler extends AbstractDaliUIHandler<HomeUIModel, HomeUI> implements CloseableUI {
58
59
60
61
62 private static final Log LOG = LogFactory.getLog(HomeUIHandler.class);
63
64 public static final String SAMPLING_OPERATIONS_CARD = "samplingOperationsCard";
65 public static final String SURVEYS_MAP_CARD = "surveysMapCard";
66
67
68
69
70 @Override
71 public void beforeInit(final HomeUI ui) {
72 super.beforeInit(ui);
73
74
75 ui.setContextValue(new HomeUIModel());
76
77 ui.setContextValue(SwingUtil.createActionIcon("home"));
78 }
79
80
81
82
83 @Override
84 public void afterInit(HomeUI ui) {
85 initUI(ui);
86 ui.getCampaignLabel().setForeground(getConfig().getColorThematicLabel());
87 ui.getProgramLabel().setForeground(getConfig().getColorThematicLabel());
88 ui.getLocationLabel().setForeground(getConfig().getColorThematicLabel());
89 ui.getStateLabel().setForeground(getConfig().getColorThematicLabel());
90 ui.getSynchronizationStatusLabel().setForeground(getConfig().getColorThematicLabel());
91 ui.getCommentLabel().setForeground(getConfig().getColorThematicLabel());
92 ui.getMnemonicLabel().setForeground(getConfig().getColorThematicLabel());
93 ui.getStartDateLabel().setForeground(getConfig().getColorThematicLabel());
94
95
96 getModel().setSurveysTableUIModel(getUI().getSurveysTableUI().getModel());
97 getModel().setOperationsTableUIModel(getUI().getOperationsTableUI().getModel());
98 getModel().setMapUIModel(getUI().getSurveysMapUI().getModel());
99
100
101 initMap();
102
103
104 initComboBox();
105 applyContext();
106
107
108 initListeners();
109
110
111 registerValidators(getValidator());
112 listenValidatorValid(getValidator(), getModel());
113
114
115 SwingUtil.setComponentWidth(getUI().getLeftImage(), ui.getHomeMenu().getPreferredSize().width * 9 / 10);
116 getUI().getLeftImage().setScaled(true);
117
118 showSamplingOperations();
119
120
121 runLastSearch();
122 }
123
124 private void initMap() {
125
126
127 getModel().getMapUIModel().setSelectionModel(getModel().getSurveysTableUIModel());
128
129
130 getModel().addPropertyChangeListener(MapParentUIModel.EVENT_OPEN_FULLSCREEN, evt -> showSurveysMapFullScreen());
131 getModel().addPropertyChangeListener(MapParentUIModel.EVENT_CLOSE_FULLSCREEN, evt -> restoreSurveysMap());
132 }
133
134
135
136
137 private void initComboBox() {
138
139
140 initBeanFilterableComboBox(
141 getUI().getContextCombobox(),
142 getContext().getContextService().getAllContexts(),
143 getContext().getSelectedContext());
144
145
146 initBeanFilterableComboBox(
147 getUI().getCampaignCombo(),
148 null,
149 null);
150
151
152 initBeanFilterableComboBox(
153 getUI().getProgramCombo(),
154 null,
155 null);
156
157
158 initBeanFilterableComboBox(
159 getUI().getLocationCombo(),
160 null,
161 null);
162
163
164 initBeanFilterableComboBox(
165 getUI().getSearchDateCombo(),
166 getContext().getSystemService().getSearchDates(),
167 null);
168
169
170 getUI().getStartDateEditor().setEnabled(false);
171 getUI().getEndDateEditor().setEnabled(false);
172 getUI().getAndLabel().setVisible(false);
173 getUI().getEndDateEditor().setVisible(false);
174
175
176 initBeanFilterableComboBox(
177 getUI().getStateCombo(),
178 getContext().getSystemService().getStates(),
179 null);
180
181
182 initBeanFilterableComboBox(
183 getUI().getSynchronizationStatusCombo(),
184 getContext().getSystemService().getAllSynchronizationStatus(false) ,
185 null);
186
187
188
189
190
191
192
193
194 DaliUIs.forceComponentSize(getUI().getContextCombobox());
195 DaliUIs.forceComponentSize(getUI().getCampaignCombo());
196 DaliUIs.forceComponentSize(getUI().getProgramCombo());
197 DaliUIs.forceComponentSize(getUI().getLocationCombo());
198 DaliUIs.forceComponentSize(getUI().getStateCombo());
199 DaliUIs.forceComponentSize(getUI().getSynchronizationStatusCombo());
200 DaliUIs.forceComponentSize(getUI().getSearchDateCombo(), 82);
201 DaliUIs.forceComponentSize(getUI().getStartDateEditor(), 120);
202 DaliUIs.forceComponentSize(getUI().getEndDateEditor(), 120);
203
204
205 }
206
207
208
209
210 private void initListeners() {
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225 getUI().getCampaignCombo().setActionListener(e -> {
226 if (!askBefore(t("dali.common.unfilter"), t("dali.common.unfilter.confirmation"))) {
227 return;
228 }
229 getUI().getCampaignCombo().setActionEnabled(false);
230 getModel().setForceNoCampaignFilter(true);
231 applyContext();
232 });
233
234
235 getModel().addPropertyChangeListener(HomeUIModel.PROPERTY_PROGRAM, evt -> {
236
237 updateLocations();
238 getUI().getLocationCombo().requestFocus();
239 });
240
241
242 getUI().getProgramCombo().setActionListener(e -> {
243 if (!askBefore(t("dali.common.unfilter"), t("dali.common.unfilter.confirmation"))) {
244 return;
245 }
246 getUI().getProgramCombo().setActionEnabled(false);
247 getModel().setForceNoProgramFilter(true);
248 applyContext();
249 });
250
251
252 getModel().addPropertyChangeListener(HomeUIModel.PROPERTY_LOCATION, evt -> {
253
254 updatePrograms();
255 getUI().getStateCombo().requestFocus();
256 });
257
258
259 getUI().getLocationCombo().setActionListener(e -> {
260 if (!askBefore(t("dali.common.unfilter"), t("dali.common.unfilter.confirmation"))) {
261 return;
262 }
263 getUI().getLocationCombo().setActionEnabled(false);
264 getModel().setForceNoLocationFilter(true);
265 applyContext();
266 });
267
268
269 getModel().addPropertyChangeListener(HomeUIModel.PROPERTY_SEARCH_DATE, evt -> {
270
271 if (getModel().getSearchDateId() != null) {
272
273
274 getUI().getStartDateEditor().setEnabled(true);
275 getUI().getEndDateEditor().setEnabled(true);
276
277
278 final SearchDateValues searchDateValue = SearchDateValues.values()[getModel().getSearchDateId()];
279 switch (searchDateValue) {
280 case BETWEEN:
281
282
283 getUI().getAndLabel().setVisible(true);
284 getUI().getEndDateEditor().setVisible(true);
285 break;
286
287 default:
288
289
290 getUI().getEndDateEditor().setVisible(false);
291 getUI().getEndDateEditor().setLocalDate(null);
292 getUI().getAndLabel().setVisible(false);
293 break;
294 }
295 } else {
296
297
298 getUI().getStartDateEditor().setLocalDate(null);
299 getUI().getStartDateEditor().setEnabled(false);
300 getUI().getEndDateEditor().setLocalDate(null);
301 getUI().getEndDateEditor().setEnabled(false);
302 getUI().getEndDateEditor().setVisible(false);
303 getUI().getAndLabel().setVisible(false);
304 }
305 });
306
307
308 getModel().addPropertyChangeListener(HomeUIModel.PROPERTY_CONTEXT, evt -> {
309
310 getContext().setSelectedContext(getModel().getContext());
311 applyContext();
312 });
313
314 getModel().getOperationsTableUIModel().addPropertyChangeListener(OperationsTableUIModel.PROPERTY_LOADING, evt -> {
315
316 if (evt.getNewValue() instanceof Boolean) {
317 getModel().getSurveysTableUIModel().setSamplingOperationsLoading((Boolean) evt.getNewValue());
318 }
319 });
320
321
322 listenModelModify(getModel().getSurveysTableUIModel());
323 getModel().getOperationsTableUIModel().addPropertyChangeListener(AbstractDaliBeanUIModel.PROPERTY_MODIFY, evt -> {
324 Boolean modify = (Boolean) evt.getNewValue();
325 if (modify != null) {
326 getModel().setModify(modify);
327
328 if (modify && getModel().getSelectedSurvey() != null) {
329 getModel().getSelectedSurvey().setDirty(true);
330 }
331 }
332 });
333
334
335 getModel().getSurveysTableUIModel().addPropertyChangeListener(SurveysTableUIModel.PROPERTY_SINGLE_ROW_SELECTED, evt -> {
336
337
338 if (evt.getOldValue() instanceof SurveysTableRowModel) {
339 SurveysTableRowModel previousSelectedSurvey = (SurveysTableRowModel) evt.getOldValue();
340 getUI().getOperationsTableUI().getHandler().stopCellEditing();
341 getUI().getOperationsTableUI().getHandler().recomputeRowsValidState();
342 previousSelectedSurvey.setOperationsValid(getModel().getOperationsTableUIModel().isValid());
343 getUI().getSurveysTableUI().getHandler().recomputeRowValidState(previousSelectedSurvey);
344 getUI().getSurveysTableUI().getHandler().forceRevalidateModel();
345 }
346
347
348 final SurveysTableRowModel survey = (SurveysTableRowModel) evt.getNewValue();
349
350
351 getModel().getSurveysTableUIModel().setSelectedSurveyEditable(survey != null && survey.isEditable());
352
353
354 getContext().setSelectedSurveyId(survey == null ? null : survey.getId());
355
356
357 getModel().setSelectedSurvey(survey);
358
359 });
360
361 getModel().addPropertyChangeListener(HomeUIModel.PROPERTY_SELECTED_SURVEY, evt -> {
362
363
364 getUI().getOperationsTableUI().getHandler().loadOperations(getModel().getSelectedSurvey());
365 });
366
367
368 getModel().getSurveysTableUIModel().addPropertyChangeListener(AbstractDaliBeanUIModel.PROPERTY_VALID, evt -> getValidator().doValidate());
369 getModel().getOperationsTableUIModel().addPropertyChangeListener(AbstractDaliBeanUIModel.PROPERTY_VALID, evt -> {
370
371 if (getModel().getSelectedSurvey() != null) {
372
373
374 getModel().getSelectedSurvey().setOperationsValid(getModel().getOperationsTableUIModel().isValid());
375 if (!getUI().getSurveysTableUI().getSurveysTable().isEditing()) {
376 getUI().getSurveysTableUI().getHandler().recomputeRowValidState(getModel().getSelectedSurvey());
377 }
378 getValidator().doValidate();
379 getUI().getSurveysTableUI().getHandler().forceRevalidateModel();
380 forceRevalidateModel();
381 }
382 });
383
384 }
385
386 private void updatePrograms() {
387
388 if (getModel().isAdjusting()) {
389 return;
390 }
391 getModel().setAdjusting(true);
392
393 ProgramDTO oldProgram = getModel().getProgram();
394 getModel().setProgram(null);
395
396 List<ProgramDTO> programs = getContext().getObservationService().getAvailablePrograms(
397 getModel().getLocationId(),
398 null,
399 getModel().isForceNoProgramFilter());
400
401 getUI().getProgramCombo().setData(programs);
402
403 if (programs.size() == 1) {
404 getModel().setProgram(programs.get(0));
405 } else if (getModel().isProgramsFiltered()
406 && oldProgram != null && programs.contains(oldProgram)) {
407 getModel().setProgram(oldProgram);
408 }
409
410 getModel().setAdjusting(false);
411 }
412
413 private void updateCampaigns() {
414
415 if (getModel().isAdjusting()) {
416 return;
417 }
418 getModel().setAdjusting(true);
419
420 CampaignDTO oldCampaign = getModel().getCampaign();
421 getModel().setCampaign(null);
422
423 List<CampaignDTO> campaigns = getContext().getObservationService().getAvailableCampaigns(null, getModel().isForceNoCampaignFilter());
424
425 getUI().getCampaignCombo().setData(campaigns);
426
427 if (campaigns.size() == 1) {
428 getModel().setCampaign(campaigns.get(0));
429 } else if (getModel().isCampaignsFiltered()
430 && oldCampaign != null && campaigns.contains(oldCampaign)) {
431 getModel().setCampaign(oldCampaign);
432 }
433
434 getModel().setAdjusting(false);
435 }
436
437 private void updateLocations() {
438
439 if (getModel().isAdjusting()) {
440 return;
441 }
442 getModel().setAdjusting(true);
443
444 LocationDTO oldLocation = getModel().getLocation();
445 getModel().setLocation(null);
446
447 List<LocationDTO> locations = getContext().getObservationService().getAvailableLocations(
448 null,
449 getModel().getProgramCode(),
450 getModel().isForceNoLocationFilter());
451
452 getUI().getLocationCombo().setData(locations);
453
454 if (locations.size() == 1) {
455 getModel().setLocation(locations.get(0));
456 } else if (getModel().isLocationsFiltered()
457 && oldLocation != null && locations.contains(oldLocation)) {
458 getModel().setLocation(oldLocation);
459 }
460
461 getModel().setAdjusting(false);
462 }
463
464
465
466
467 private void applyContext() {
468
469 getModel().setProgramsFiltered(getContext().getDataContext().isContextFiltered(FilterTypeValues.PROGRAM));
470 getModel().setCampaignsFiltered(getContext().getDataContext().isContextFiltered(FilterTypeValues.CAMPAIGN));
471 getModel().setLocationsFiltered(getContext().getDataContext().isContextFiltered(FilterTypeValues.LOCATION));
472
473
474 updatePrograms();
475 updateCampaigns();
476 updateLocations();
477 }
478
479
480
481
482 private void runLastSearch() {
483
484 final SurveyFilterDTO surveyFilter = getContext().getSurveyFilter();
485 if (surveyFilter != null) {
486
487 if (surveyFilter.getCampaignId() != null) {
488 getUI().getCampaignCombo().getData().stream()
489 .filter(campaign -> campaign.getId().equals(surveyFilter.getCampaignId()))
490 .findFirst().ifPresent(campaign -> getModel().setCampaign(campaign));
491 }
492 if (surveyFilter.getProgramCode() != null) {
493 getUI().getProgramCombo().getData().stream()
494 .filter(program -> program.getCode().equals(surveyFilter.getProgramCode()))
495 .findFirst().ifPresent(program -> getModel().setProgram(program));
496 }
497 if (surveyFilter.getLocationId() != null) {
498 getUI().getLocationCombo().getData().stream()
499 .filter(location -> location.getId().equals(surveyFilter.getLocationId()))
500 .findFirst().ifPresent(location -> getModel().setLocation(location));
501 }
502 if (surveyFilter.getStateId() != null) {
503 getUI().getStateCombo().getData().stream()
504 .filter(state -> state.getId().equals(surveyFilter.getStateId()))
505 .findFirst().ifPresent(state -> getModel().setState(state));
506 }
507 if (surveyFilter.getShareId() != null) {
508 getUI().getSynchronizationStatusCombo().getData().stream()
509 .filter(synchronizationStatus -> synchronizationStatus.getId().equals(surveyFilter.getShareId()))
510 .findFirst().ifPresent(synchronizationStatus -> getModel().setSynchronizationStatus(synchronizationStatus));
511 }
512 if (surveyFilter.getSearchDateId() != null) {
513 getUI().getSearchDateCombo().getData().stream()
514 .filter(searchDate -> searchDate.getId().equals(surveyFilter.getSearchDateId()))
515 .findFirst().ifPresent(searchDate -> getModel().setSearchDate(searchDate));
516 }
517
518 getModel().setStartDate(surveyFilter.getDate1());
519 getModel().setEndDate(surveyFilter.getDate2());
520 getModel().setName(surveyFilter.getName());
521 getModel().setComment(surveyFilter.getComment());
522
523
524 SearchAction searchAction = getContext().getActionFactory().createLogicAction(HomeUIHandler.this, SearchAction.class);
525 searchAction.setControlAfterSelect(true);
526 SwingUtilities.invokeLater(() -> getContext().getActionEngine().runAction(searchAction));
527 }
528
529 }
530
531 public void showSurveysMap() {
532 getUI().getBottomPanelLayout().setSelected(SURVEYS_MAP_CARD);
533
534 SwingUtilities.invokeLater(() -> getUI().getSurveysTableUI().getShowMapButton().setVisible(false));
535 getUI().getSurveysMapUI().getHandler().openMap();
536 }
537
538 private JFrame mapFrame;
539
540 public void showSurveysMapFullScreen() {
541
542 if (mapFrame != null) {
543 mapFrame.dispose();
544 }
545
546 getUI().getBottomPanelLayout().setSelected(SAMPLING_OPERATIONS_CARD);
547
548 SurveysMapUI mapUI = getUI().getSurveysMapUI();
549 mapFrame = createFrame(mapUI, t("dali.home.map.title"), null, mapUI.getBounds());
550
551 SwingUtilities.invokeLater(() -> {
552 mapFrame.setExtendedState(mapFrame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
553 mapFrame.setVisible(true);
554 });
555
556 }
557
558 public void restoreSurveysMap() {
559
560 if (mapFrame == null || !(mapFrame.getContentPane() instanceof SurveysMapUI)) {
561 throw new DaliTechnicalException("Cannot restore map from the frame");
562 }
563
564 SurveysMapUI mapUI = (SurveysMapUI) mapFrame.getContentPane();
565 getUI().getBottomPanel().add(mapUI, HomeUIHandler.SURVEYS_MAP_CARD);
566
567 SwingUtilities.invokeLater(() -> getUI().getSurveysTableUI().getShowMapButton().setVisible(true));
568 }
569
570 public void showSamplingOperations() {
571 getUI().getBottomPanelLayout().setSelected(SAMPLING_OPERATIONS_CARD);
572 SwingUtilities.invokeLater(() -> getUI().getSurveysTableUI().getShowMapButton().setVisible(true));
573
574 if (mapFrame != null) {
575 restoreSurveysMap();
576 getUI().getSurveysMapUI().getFullScreenButton().setVisible(true);
577 mapFrame.dispose();
578 mapFrame = null;
579 }
580
581 getUI().getSurveysMapUI().getHandler().closeMap();
582 }
583
584
585
586
587 @Override
588 public SwingValidator<HomeUIModel> getValidator() {
589 return getUI().getValidator();
590 }
591
592
593
594
595 @Override
596 @SuppressWarnings("unchecked")
597 public boolean quitUI() {
598 try {
599 QuitScreenAction action = new QuitScreenAction(this, false, SaveAction.class);
600 if (action.prepareAction()) {
601 return true;
602 }
603 } catch (Exception e) {
604 LOG.error(e.getLocalizedMessage(), e);
605 }
606 return false;
607 }
608
609 @Override
610 public void onCloseUI() {
611
612 if (mapFrame != null) {
613 mapFrame.dispose();
614 }
615
616 super.onCloseUI();
617 }
618 }