View Javadoc
1   package fr.ifremer.dali.ui.swing.content.synchro.program;
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 com.google.common.collect.Lists;
27  import fr.ifremer.dali.dto.configuration.programStrategy.ProgramDTO;
28  import fr.ifremer.dali.dto.enums.SearchDateValues;
29  import fr.ifremer.dali.service.StatusFilter;
30  import fr.ifremer.dali.ui.swing.util.AbstractDaliUIHandler;
31  import fr.ifremer.dali.ui.swing.util.DaliUIs;
32  import fr.ifremer.quadrige3.core.exception.Exceptions;
33  import fr.ifremer.quadrige3.core.exception.QuadrigeTechnicalException;
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.io.IOException;
40  import java.util.List;
41  import java.util.stream.Collectors;
42  
43  /**
44   * Program selector UI handler.
45   */
46  public class ProgramSelectUIHandler extends AbstractDaliUIHandler<ProgramSelectUIModel, ProgramSelectUI> implements Cancelable {
47  
48      /**
49       * Logger.
50       */
51      private static final Log LOG = LogFactory.getLog(ProgramSelectUIHandler.class);
52  
53      /**
54       * {@inheritDoc}
55       */
56      @Override
57      public void beforeInit(final ProgramSelectUI ui) {
58          super.beforeInit(ui);
59  
60          // create model and register to the JAXX context
61          final ProgramSelectUIModel model = new ProgramSelectUIModel();
62          ui.setContextValue(model);
63      }
64  
65      /**
66       * {@inheritDoc}
67       */
68      @Override
69      public void afterInit(final ProgramSelectUI ui) {
70  
71          initUI(ui);
72  
73          initOptions(ui);
74  
75          // load programs
76          StatusFilter programStatusFilter = ui.getProgramStatusFilter();
77          // Get program from synchronisation server
78          List<ProgramDTO> programs = null;
79          boolean useServer = Boolean.FALSE.equals(ui.fileSynchro) && getContext().isSynchroEnabled()
80                  && (programStatusFilter == StatusFilter.ALL || programStatusFilter == StatusFilter.ACTIVE);
81  
82          if (useServer) {
83              try {
84                  programs = getContext().getProgramStrategyService().getRemoteProgramsByUser(getContext().getAuthenticationInfo());
85              } catch (QuadrigeTechnicalException ex) {
86                  if (Exceptions.hasCause(ex, IOException.class)) {
87                      LOG.warn("Connection to synchronization server failed, will use local programs");
88                      useServer = false;
89                  } else throw ex;
90              }
91          }
92  
93          if (!useServer) {
94              programs = getContext().getProgramStrategyService().getWritableProgramsByStatus(programStatusFilter);
95          }
96  
97          List<ProgramDTO> selectedPrograms = Lists.newArrayList();
98  
99          if (CollectionUtils.isNotEmpty(programs) && CollectionUtils.isNotEmpty(ui.getInitialProgramCodes())) {
100             selectedPrograms = programs.stream().filter(program -> program != null && ui.getInitialProgramCodes().contains(program.getCode())).collect(Collectors.toList());
101         }
102 
103         // Apply default previously selected programs
104         getModel().setSelectedPrograms(selectedPrograms);
105         initBeanList(ui.getProgramDoubleList(), programs, selectedPrograms);
106 
107     }
108 
109     private void initOptions(ProgramSelectUI ui) {
110 
111         if (Boolean.TRUE.equals(ui.getFileSynchro())) {
112 
113             // show file options
114             ui.getOptionsPanel().setVisible(true);
115             ui.getOnlyDirtyCheckBox().setVisible(true);
116             ui.getDatesPanel().setVisible(true);
117 
118             // set 'dirty only' flag to true by default
119             getModel().setDirtyOnly(true);
120 
121             // init fields
122             ui.getStartDateEditor().setEnabled(false);
123             ui.getEndDateEditor().setEnabled(false);
124             ui.getAndLabel().setVisible(false);
125             ui.getEndDateEditor().setVisible(false);
126 
127             DaliUIs.forceComponentSize(ui.getSearchDateCombo(), 82);
128             DaliUIs.forceComponentSize(ui.getStartDateEditor(), 120);
129             DaliUIs.forceComponentSize(ui.getEndDateEditor(), 120);
130 
131             // init search date combo
132             initBeanFilterableComboBox(
133                     ui.getSearchDateCombo(),
134                     getContext().getSystemService().getSearchDates(),
135                     null);
136 
137             // add a listener on it
138             getModel().addPropertyChangeListener(ProgramSelectUIModel.PROPERTY_SEARCH_DATE, evt -> {
139 
140                 if (getModel().getSearchDateId() != null) {
141 
142                     // enable fields
143                     ui.getStartDateEditor().setEnabled(true);
144                     ui.getEndDateEditor().setEnabled(true);
145 
146                     final SearchDateValues searchDateValue = SearchDateValues.values()[getModel().getSearchDateId()];
147                     if (searchDateValue == SearchDateValues.BETWEEN) {
148                         // show all fields
149                         ui.getAndLabel().setVisible(true);
150                         ui.getEndDateEditor().setVisible(true);
151                     } else {
152                         // show start date only
153                         ui.getEndDateEditor().setVisible(false);
154                         ui.getAndLabel().setVisible(false);
155                         getModel().setEndDate(null);
156                     }
157                 } else {
158 
159                     // clear
160                     ui.getStartDateEditor().setEnabled(false);
161                     ui.getEndDateEditor().setEnabled(false);
162                     ui.getEndDateEditor().setVisible(false);
163                     ui.getAndLabel().setVisible(false);
164                     getModel().setStartDate(null);
165                     getModel().setEndDate(null);
166                 }
167             });
168 
169         } else {
170 
171             // hide file options
172             ui.getOnlyDirtyCheckBox().setVisible(false);
173             ui.getDatesPanel().setVisible(false);
174             // reset values
175             getModel().setDirtyOnly(false);
176             getModel().setSearchDate(null);
177             getModel().setStartDate(null);
178             getModel().setEndDate(null);
179 
180         }
181 
182         if (Boolean.TRUE.equals(ui.getPhotoSynchro())) {
183 
184             // show options
185             ui.getOptionsPanel().setVisible(true);
186             ui.getEnablePhotoCheckBox().setVisible(true);
187             // init the photo option
188             getModel().setEnablePhoto(getConfig().isSynchroPhotoEnabledByDefault());
189 
190         } else {
191 
192             // hide options
193             ui.getEnablePhotoCheckBox().setVisible(false);
194         }
195 
196         // Hide options panel if no option selected
197         if (Boolean.FALSE.equals(ui.getFileSynchro()) && Boolean.FALSE.equals(ui.getPhotoSynchro())) {
198             ui.getOptionsPanel().setVisible(false);
199         }
200     }
201 
202     /**
203      * {@inheritDoc}
204      */
205     @Override
206     public void cancel() {
207 
208         // clear selected programs
209         getModel().setSelectedPrograms(null);
210 
211         closeDialog();
212     }
213 
214     /**
215      * <p>select.</p>
216      */
217     public void select() {
218         closeDialog();
219     }
220 }