1 package fr.ifremer.dali.ui.swing.content.home.survey;
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.data.survey.SurveyDTO;
27 import fr.ifremer.dali.ui.swing.action.AbstractDaliAction;
28
29 import javax.swing.*;
30
31 import static org.nuiton.i18n.I18n.t;
32
33
34
35
36 public class DuplicateSurveyAction extends AbstractDaliAction<SurveysTableUIModel, SurveysTableUI, SurveysTableUIHandler> {
37
38 private SurveyDTO duplicatedObservation;
39
40 private boolean fullDuplication;
41 private boolean duplicateCoordinate;
42
43
44
45
46
47
48 public DuplicateSurveyAction(SurveysTableUIHandler handler) {
49 super(handler, false);
50 }
51
52
53
54
55 @Override
56 public boolean prepareAction() throws Exception {
57 if (!super.prepareAction() || getModel().getSelectedRows().size() != 1) {
58 return false;
59 }
60
61
62 String title = t("dali.home.survey.duplicate.title");
63 JCheckBox checkBox = new JCheckBox(t("dali.home.survey.duplicate.coordinate"));
64 Object[] messageObjects = {checkBox};
65
66 Object[] options = {
67 t("dali.home.survey.duplicate.option.full"),
68 t("dali.home.survey.duplicate.option.simple"),
69 t("dali.common.cancel")};
70 int result = getContext().getDialogHelper().showOptionDialog(getUI(), messageObjects, title, JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_CANCEL_OPTION, options, options[0]);
71
72 if (result == JOptionPane.CANCEL_OPTION || result == JOptionPane.CLOSED_OPTION) {
73 return false;
74 }
75
76 fullDuplication = result == 0;
77 duplicateCoordinate = checkBox.isSelected();
78
79 return true;
80 }
81
82
83
84
85 @Override
86 public void doAction() throws Exception {
87
88
89 final SurveysTableRowModel surveysTableRowModel = getModel().getSelectedRows().iterator().next();
90 if (surveysTableRowModel != null) {
91
92
93 duplicatedObservation = getContext().getObservationService().duplicateSurvey(surveysTableRowModel.toBean(), fullDuplication, duplicateCoordinate);
94 }
95 }
96
97
98
99
100 @Override
101 public void postSuccessAction() {
102 super.postSuccessAction();
103
104 if (duplicatedObservation != null) {
105
106
107 getModel().addNewRow(duplicatedObservation);
108 getModel().setModify(true);
109 }
110 }
111
112 @Override
113 protected void releaseAction() {
114 super.releaseAction();
115
116 duplicatedObservation = null;
117 }
118 }