View Javadoc
1   package fr.ifremer.reefdb.ui.swing.content.synchro.program;
2   
3   /*
4    * #%L
5    * Reef DB :: 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 com.google.common.base.Predicate;
27  import com.google.common.collect.Lists;
28  import fr.ifremer.reefdb.dto.ReefDbBeans;
29  import fr.ifremer.reefdb.dto.configuration.programStrategy.ProgramDTO;
30  import fr.ifremer.reefdb.dto.enums.SearchDateValues;
31  import fr.ifremer.reefdb.service.StatusFilter;
32  import fr.ifremer.reefdb.ui.swing.util.AbstractReefDbUIHandler;
33  import fr.ifremer.reefdb.ui.swing.util.ReefDbUIs;
34  import org.apache.commons.collections4.CollectionUtils;
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  import org.nuiton.jaxx.application.swing.util.Cancelable;
38  
39  import java.util.List;
40  
41  /**
42   * Controleur pour la zone des programmes.
43   */
44  public class ProgramSelectUIHandler extends AbstractReefDbUIHandler<ProgramSelectUIModel, ProgramSelectUI> implements Cancelable {
45  
46      /**
47       * Logger.
48       */
49      private static final Log LOG = LogFactory.getLog(ProgramSelectUIHandler.class);
50  
51      /** {@inheritDoc} */
52      @Override
53      public void beforeInit(final ProgramSelectUI ui) {
54          super.beforeInit(ui);
55  
56          // create model and register to the JAXX context
57          final ProgramSelectUIModel model = new ProgramSelectUIModel();
58          ui.setContextValue(model);
59      }
60  
61      /** {@inheritDoc} */
62      @Override
63      public void afterInit(final ProgramSelectUI ui) {
64  
65          // Initialiser l UI
66          initUI(ui);
67  
68          initOptions(ui);
69  
70          // load programs
71          StatusFilter programStatusFilter = ui.getProgramStatusFilter();
72          // Get program from synchronisation server
73          List<ProgramDTO> programs;
74          if (getContext().isSynchroEnabled()
75                  && (programStatusFilter == StatusFilter.NATIONAL || programStatusFilter == StatusFilter.NATIONAL_ACTIVE)) {
76  
77              programs = getContext().getProgramStrategyService().getRemoteProgramsByUser(getContext().getAuthenticationInfo());
78          } else {
79              if (ui.getUserIdFilter() != null) {
80                  programs = getContext().getProgramStrategyService().getWritableProgramsByUserAndStatus(ui.getUserIdFilter(), programStatusFilter);
81              } else {
82                  // Deprecated, should not happens
83                  programs = getContext().getProgramStrategyService().getProgramsByStatus(programStatusFilter);
84              }
85          }
86  
87          List<ProgramDTO> selectedPrograms = Lists.newArrayList();
88  
89          if (CollectionUtils.isNotEmpty(programs) && CollectionUtils.isNotEmpty(ui.getInitialProgramCodes())) {
90              selectedPrograms = ReefDbBeans.filterCollection(programs, (Predicate<ProgramDTO>) input -> input != null && ui.getInitialProgramCodes().contains(input.getCode()));
91          }
92  
93          // Apply default previously selected programs
94          getModel().setSelectedPrograms(selectedPrograms);
95          initBeanList(ui.getProgramDoubleList(), programs, selectedPrograms);
96  
97      }
98  
99      private void initOptions(ProgramSelectUI ui) {
100 
101         if (Boolean.TRUE.equals(ui.getFileSynchro())) {
102 
103             // show file options
104             ui.getOptionsPanel().setVisible(true);
105             ui.getOnlyDirtyCheckBox().setVisible(true);
106             ui.getDatesPanel().setVisible(true);
107 
108             // set 'dirty only' flag to true by default
109             getModel().setDirtyOnly(true);
110 
111             // init fields
112             ui.getStartDateEditor().setEnabled(false);
113             ui.getEndDateEditor().setEnabled(false);
114             ui.getAndLabel().setVisible(false);
115             ui.getEndDateEditor().setVisible(false);
116 
117             ReefDbUIs.forceComponentSize(ui.getSearchDateCombo(), 82);
118             ReefDbUIs.forceComponentSize(ui.getStartDateEditor(), 120);
119             ReefDbUIs.forceComponentSize(ui.getEndDateEditor(), 120);
120 
121             // init search date combo
122             initBeanFilterableComboBox(
123                     ui.getSearchDateCombo(),
124                     getContext().getSystemService().getSearchDates(),
125                     null);
126 
127             // add a listener on it
128             getModel().addPropertyChangeListener(ProgramSelectUIModel.PROPERTY_SEARCH_DATE, evt -> {
129 
130                 if (getModel().getSearchDateId() != null) {
131 
132                     // enable fields
133                     ui.getStartDateEditor().setEnabled(true);
134                     ui.getEndDateEditor().setEnabled(true);
135 
136                     final SearchDateValues searchDateValue = SearchDateValues.values()[getModel().getSearchDateId()];
137                     if (searchDateValue == SearchDateValues.BETWEEN) {
138                             // show all fields
139                         ui.getAndLabel().setVisible(true);
140                         ui.getEndDateEditor().setVisible(true);
141                     } else {
142                             // show start date only
143                         ui.getEndDateEditor().setVisible(false);
144                         ui.getAndLabel().setVisible(false);
145                             getModel().setEndDate(null);
146                     }
147                 } else {
148 
149                     // clear
150                     ui.getStartDateEditor().setEnabled(false);
151                     ui.getEndDateEditor().setEnabled(false);
152                     ui.getEndDateEditor().setVisible(false);
153                     ui.getAndLabel().setVisible(false);
154                     getModel().setStartDate(null);
155                     getModel().setEndDate(null);
156                 }
157             });
158 
159 
160         } else {
161 
162             // hide file options
163             ui.getOnlyDirtyCheckBox().setVisible(false);
164             ui.getDatesPanel().setVisible(false);
165             // reset values
166             getModel().setDirtyOnly(false);
167             getModel().setSearchDate(null);
168             getModel().setStartDate(null);
169             getModel().setEndDate(null);
170         }
171 
172         if (Boolean.TRUE.equals(ui.getPhotoSynchro())) {
173 
174             // show options
175             ui.getOptionsPanel().setVisible(true);
176             ui.getEnablePhotoCheckBox().setVisible(true);
177             // init the photo option
178             getModel().setEnablePhoto(getConfig().isSynchroPhotoEnabledByDefault());
179 
180         } else {
181 
182             // hide options
183             ui.getEnablePhotoCheckBox().setVisible(false);
184         }
185 
186         // Hide options panel if no option selected
187         if (Boolean.FALSE.equals(ui.getFileSynchro()) && Boolean.FALSE.equals(ui.getPhotoSynchro())) {
188             ui.getOptionsPanel().setVisible(false);
189         }
190     }
191 
192     /** {@inheritDoc} */
193     @Override
194     public void cancel() {
195 
196         // clear selected programs
197         getModel().setSelectedPrograms(null);
198 
199         closeDialog();
200     }
201 
202     /**
203      * <p>select.</p>
204      */
205     public void select() {
206         closeDialog();
207     }
208 }