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.quadrige3.ui.swing.ApplicationUIUtil;
27  import fr.ifremer.quadrige3.ui.swing.DialogHelper;
28  import fr.ifremer.dali.ui.swing.action.AbstractDaliAction;
29  import fr.ifremer.dali.ui.swing.content.home.HomeUI;
30  import fr.ifremer.dali.ui.swing.content.home.SaveAction;
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  
34  import javax.swing.JOptionPane;
35  import java.util.Set;
36  
37  import static org.nuiton.i18n.I18n.t;
38  
39  /**
40   * Action permettant de changer l'état d'une observation dans le tableau des
41   * observations de l ecran d accueil.
42   */
43  public class ControlSurveyAction extends AbstractDaliAction<SurveysTableUIModel, SurveysTableUI, SurveysTableUIHandler> {
44  
45      /**
46       * Loggeur.
47       */
48      private static final Log LOG = LogFactory.getLog(ControlSurveyAction.class);
49  
50      private Set<SurveysTableRowModel> selectedSurveys;
51  
52      /**
53       * Constructor.
54       *
55       * @param handler Controleur
56       */
57      public ControlSurveyAction(final SurveysTableUIHandler handler) {
58          super(handler, false);
59      }
60  
61      /**
62       * {@inheritDoc}
63       */
64      @Override
65      public boolean prepareAction() throws Exception {
66          if (!super.prepareAction()) {
67              return false;
68          }
69  
70          if (getModel().getSelectedRows().isEmpty()) {
71              LOG.warn("Aucune Observation de selectionne");
72              return false;
73          }
74  
75          selectedSurveys = getModel().getSelectedRows();
76  
77          boolean alreadyControlled = false;
78          for (SurveysTableRowModel rowModel : selectedSurveys) {
79              if (rowModel.getValidationDate() != null) {
80                  // On ne peut pas controler une observation qui a déja été validée
81                  getContext().getDialogHelper().showWarningDialog(t("dali.action.control.survey.error.alreadyValid"));
82                  return false;
83              }
84              if (!alreadyControlled && rowModel.getControlDate() != null) {
85                  alreadyControlled = true;
86              }
87          }
88  
89          return getContext().getDialogHelper().showOptionDialog(null,
90                  ApplicationUIUtil.getHtmlString(alreadyControlled ? t("dali.action.control.survey.multiple.message") : t("dali.action.control.survey.message")),
91                  alreadyControlled ? t("dali.action.control.survey.multiple.titre") : t("dali.action.control.survey.titre"),
92                  JOptionPane.QUESTION_MESSAGE,
93                  DialogHelper.CUSTOM_OPTION,
94                  t("dali.common.control"),
95                  t("dali.common.cancel")
96          ) == JOptionPane.OK_OPTION;
97  
98      }
99  
100     /**
101      * {@inheritDoc}
102      */
103     @Override
104     public void doAction() throws Exception {
105 
106         SaveAction saveAction = getContext().getActionFactory().createLogicAction(getUI().getParentContainer(HomeUI.class).getHandler(), SaveAction.class);
107         saveAction.setSurveysToSave(selectedSurveys);
108         saveAction.setShowControlIfSuccess(true);
109         saveAction.setUpdateControlDateWhenControlSucceed(true);
110         getContext().getActionEngine().runFullInternalAction(saveAction);
111     }
112 }