View Javadoc
1   package fr.ifremer.dali.ui.swing.content.home.survey;
2   
3   /*
4    * #%L
5    * Dali :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
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   * Duplicate survey Action
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       * Constructor.
45       *
46       * @param handler the handler
47       */
48      public DuplicateSurveyAction(SurveysTableUIHandler handler) {
49          super(handler, false);
50      }
51  
52      /**
53       * {@inheritDoc}
54       */
55      @Override
56      public boolean prepareAction() throws Exception {
57          if (!super.prepareAction() || getModel().getSelectedRows().size() != 1) {
58              return false;
59          }
60  
61          // Change message and buttons (Mantis #52404)
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       * {@inheritDoc}
84       */
85      @Override
86      public void doAction() throws Exception {
87  
88          // Selected observation
89          final SurveysTableRowModel surveysTableRowModel = getModel().getSelectedRows().iterator().next();
90          if (surveysTableRowModel != null) {
91  
92              // Duplicate observation
93              duplicatedObservation = getContext().getObservationService().duplicateSurvey(surveysTableRowModel.toBean(), fullDuplication, duplicateCoordinate);
94          }
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     @Override
101     public void postSuccessAction() {
102         super.postSuccessAction();
103 
104         if (duplicatedObservation != null) {
105 
106             // Add focus on duplicate row
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 }