View Javadoc
1   package fr.ifremer.dali.ui.swing.content.manage.program.locations.updatePeriod;
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.configuration.programStrategy.AppliedStrategyDTO;
27  import fr.ifremer.dali.dto.referential.DepartmentDTO;
28  import fr.ifremer.dali.service.StatusFilter;
29  import fr.ifremer.dali.ui.swing.util.AbstractDaliUIHandler;
30  import org.nuiton.jaxx.application.swing.util.Cancelable;
31  
32  import javax.swing.JComponent;
33  import java.time.LocalDate;
34  
35  import static org.nuiton.i18n.I18n.t;
36  
37  /**
38   * Controleur pour l'écran principal.
39   */
40  public class UpdatePeriodUIHandler extends AbstractDaliUIHandler<UpdatePeriodUIModel, UpdatePeriodUI> implements Cancelable {
41  
42      /** {@inheritDoc} */
43      @Override
44      public void beforeInit(UpdatePeriodUI ui) {
45          super.beforeInit(ui);
46  
47          ui.setContextValue(new UpdatePeriodUIModel());
48      }
49  
50      /** {@inheritDoc} */
51      @Override
52      public void afterInit(UpdatePeriodUI ui) {
53          initUI(ui);
54  
55          // Chargement de la combo
56          initBeanFilterableComboBox(
57                  getUI().getSamplerCombo(),
58                  getContext().getReferentialService().getDepartments(StatusFilter.ACTIVE),
59                  null);
60  
61          getUI().getSamplerCombo().setShowReset(true);
62          getUI().getSamplerCombo().setShowDecorator(false);
63  
64          initBeanFilterableComboBox(
65                  getUI().getAnalystCombo(),
66                  getContext().getReferentialService().getDepartments(StatusFilter.ACTIVE),
67                  null);
68  
69          getUI().getAnalystCombo().setShowReset(true);
70          getUI().getAnalystCombo().setShowDecorator(false);
71  
72      }
73  
74      /**
75       * <p>valid.</p>
76       */
77      public void valid() {
78  
79          // Recuperation des saisies
80          final LocalDate startDate = getUI().getStartDateEditor().getLocalDate();
81          final LocalDate endDate = getUI().getEndDateEditor().getLocalDate();
82          final DepartmentDTO samplingDepartment = (DepartmentDTO) getUI().getSamplerCombo().getSelectedItem();
83          final DepartmentDTO analystDepartment = (DepartmentDTO) getUI().getAnalystCombo().getSelectedItem();
84  
85          if (startDate == null ^ endDate == null) {
86  
87              // La date de debut doit être renseignee
88              getContext().getDialogHelper().showErrorDialog(getUI(),
89                      t("dali.program.location.periods.error.date.message"),
90                      t("dali.program.location.periods.error.titre"));
91  
92          } else if (startDate != null && endDate != null && endDate.isBefore(startDate)) {
93  
94              // la date de fin ne pas etre antérieur au début
95              getContext().getDialogHelper().showErrorDialog(getUI(),
96                      t("dali.program.location.periods.error.date.before.message"),
97                      t("dali.program.location.periods.error.titre"));
98  
99          } else {
100 
101             // Ajout des elements dans les lieux selectionnes
102             for (final AppliedStrategyDTO lieu : getModel().getTableModel().getSelectedRows()) {
103                 lieu.setStartDate(startDate);
104                 lieu.setEndDate(endDate);
105                 lieu.setSamplingDepartment(samplingDepartment);
106                 lieu.setAnalysisDepartment(analystDepartment);
107             }
108 
109             // Quitter la dialogue
110             closeDialog();
111         }
112     }
113 
114     /** {@inheritDoc} */
115     @Override
116     protected JComponent getComponentToFocus() {
117         return getUI().getStartDateEditor();
118     }
119 
120     /** {@inheritDoc} */
121     @Override
122     public void cancel() {
123         closeDialog();
124     }
125 }