View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.home;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import com.google.common.collect.Lists;
26  import fr.ifremer.tutti.persistence.entities.data.Cruise;
27  import fr.ifremer.tutti.persistence.entities.data.Program;
28  import fr.ifremer.tutti.persistence.entities.protocol.TuttiProtocol;
29  import fr.ifremer.tutti.service.PersistenceService;
30  import fr.ifremer.tutti.ui.swing.util.AbstractTuttiUIHandler;
31  import jaxx.runtime.swing.editor.bean.BeanFilterableComboBox;
32  import jaxx.runtime.validator.swing.SwingValidator;
33  import org.apache.commons.collections4.CollectionUtils;
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  
37  import javax.swing.JComboBox;
38  import javax.swing.JComponent;
39  import java.awt.Font;
40  import java.beans.PropertyChangeListener;
41  import java.util.List;
42  
43  /**
44   * Main ui content to select cruise.
45   *
46   * @author Tony Chemit - chemit@codelutin.com
47   * @since 0.1
48   */
49  public class SelectCruiseUIHandler extends AbstractTuttiUIHandler<SelectCruiseUIModel, SelectCruiseUI> {
50  
51      /** Logger. */
52      private static final Log log =
53              LogFactory.getLog(SelectCruiseUIHandler.class);
54  
55      @Override
56      public SwingValidator<SelectCruiseUIModel> getValidator() {
57          return ui.getValidator();
58      }
59  
60      @Override
61      public void beforeInit(SelectCruiseUI ui) {
62          super.beforeInit(ui);
63  
64          PersistenceService persistenceService = getContext().getPersistenceService();
65  
66          SelectCruiseUIModel model = new SelectCruiseUIModel();
67  
68          List<Program> programs = Lists.newArrayList(persistenceService.getAllProgram());
69          model.setPrograms(programs);
70  
71          Program selectedProgram = null;
72  
73          if (programs.isEmpty()) {
74              // do nothing
75  
76              if (log.isDebugEnabled()) {
77                  log.debug("No program found.");
78              }
79          } else {
80  
81              // get selected program (if any)
82  
83  
84              List<Cruise> cruises = null;
85              if (getContext().isProgramFilled()) {
86                  selectedProgram = getDataContext().getProgram();
87                  //TODO check selectprogram is not null
88                  cruises = Lists.newArrayList(persistenceService.getAllCruise(selectedProgram.getId()));
89              }
90  
91              model.setProgram(selectedProgram);
92              model.setCruises(cruises);
93  
94              if (CollectionUtils.isEmpty(cruises)) {
95  
96                  // nothing to select
97              } else {
98  
99                  Cruise selectedCruise = null;
100                 if (getContext().isCruiseFilled()) {
101 
102                     // always reload the cruise to be sure synchronizationStatus is ok
103                     selectedCruise = getDataContext().reloadCruise();
104                 }
105                 model.setCruise(selectedCruise);
106             }
107         }
108 
109         List<TuttiProtocol> protocols =
110                 Lists.newArrayList(persistenceService.getAllProtocol(selectedProgram == null ? null : selectedProgram.getId()));
111         model.setProtocols(protocols);
112 
113         if (protocols.isEmpty()) {
114             // do nothing
115 
116             if (log.isDebugEnabled()) {
117                 log.debug("No protocol found.");
118             }
119         } else {
120 
121             model.setProtocol(getDataContext().getProtocol());
122         }
123         ui.setContextValue(model);
124     }
125 
126     @Override
127     public void afterInit(SelectCruiseUI ui) {
128 
129         initUI(ui);
130 
131         Font font = ui.getEditCatchesButton().getFont();
132         ui.getEditCatchesButton().setFont(font.deriveFont(Font.BOLD, 14));
133         ui.getValidateCatchesButton().setFont(font.deriveFont(Font.BOLD, 14));
134 
135         SelectCruiseUIModel model = getModel();
136 
137         initBeanFilterableComboBox(ui.getProgramCombobox(), model.getPrograms(), model.getProgram());
138 
139         initBeanFilterableComboBox(ui.getCruiseCombobox(), model.getCruises(), model.getCruise());
140 
141         initBeanFilterableComboBox(ui.getProtocolCombobox(), model.getProtocols(), model.getProtocol());
142 
143         model.addPropertyChangeListener(SelectCruiseUIModel.PROPERTY_PROTOCOLS, evt -> {
144             // reload combo box
145             BeanFilterableComboBox<TuttiProtocol> combobox = SelectCruiseUIHandler.this.ui.getProtocolCombobox();
146             List<TuttiProtocol> protocols = (List<TuttiProtocol>) evt.getNewValue();
147 
148             combobox.setData(null);
149             if (protocols != null) {
150                 combobox.setData(protocols);
151             }
152 
153             SelectCruiseUIHandler.this.ui.applyDataBinding(SelectCruiseUI.BINDING_PROTOCOL_COMBOBOX_ENABLED);
154         });
155 
156         model.addPropertyChangeListener(SelectCruiseUIModel.PROPERTY_PROGRAM, evt -> {
157             Program newValue = (Program) evt.getNewValue();
158             boolean noProgram = newValue == null;
159             getContext().setProgramId(noProgram ? null : newValue.getId());
160             if (log.isInfoEnabled()) {
161                 log.info("Selected program: " + newValue);
162             }
163             List<Cruise> cruises;
164             List<TuttiProtocol> protocols;
165             if (noProgram) {
166                 cruises = Lists.newArrayList();
167                 protocols = Lists.newArrayList(getPersistenceService().getAllProtocol(null));
168 
169             } else {
170                 cruises = Lists.newArrayList(getPersistenceService().getAllCruise(newValue.getId()));
171                 protocols = Lists.newArrayList(getPersistenceService().getAllProtocol(newValue.getId()));
172             }
173             SelectCruiseUIModel source = (SelectCruiseUIModel) evt.getSource();
174             source.setCruises(cruises);
175             source.setCruise(null);
176             source.setProtocols(protocols);
177             source.setProtocol(null);
178         });
179 
180         model.addPropertyChangeListener(SelectCruiseUIModel.PROPERTY_CRUISES, evt -> {
181             // reload combo box
182             BeanFilterableComboBox<Cruise> combobox = SelectCruiseUIHandler.this.ui.getCruiseCombobox();
183             List<Cruise> campaigns = (List<Cruise>) evt.getNewValue();
184             combobox.setData(null);
185             if (campaigns != null) {
186                 combobox.setData(campaigns);
187             }
188         });
189 
190         model.addPropertyChangeListener(SelectCruiseUIModel.PROPERTY_CRUISE, evt -> {
191             Cruise newValue = (Cruise) evt.getNewValue();
192             getContext().setCruiseId(newValue == null ? null : newValue.getIdAsInt());
193         });
194 
195         model.addPropertyChangeListener(SelectCruiseUIModel.PROPERTY_PROTOCOLS, evt -> {
196             // reload combo box
197             BeanFilterableComboBox<TuttiProtocol> combobox = SelectCruiseUIHandler.this.ui.getProtocolCombobox();
198             List<TuttiProtocol> protocols = (List<TuttiProtocol>) evt.getNewValue();
199             combobox.setData(null);
200             if (protocols != null) {
201                 combobox.setData(protocols);
202             }
203         });
204 
205         model.addPropertyChangeListener(SelectCruiseUIModel.PROPERTY_PROTOCOL, evt -> {
206             TuttiProtocol newValue = (TuttiProtocol) evt.getNewValue();
207             getContext().setProtocolId(newValue == null ? null : newValue.getId());
208 
209             JComboBox editProtocolComboBox = SelectCruiseUIHandler.this.ui.getEditProtocolComboBox();
210 
211             // disable the actions on the combo during model modifications
212             editProtocolComboBox.putClientProperty(CAN_EDIT, false);
213 
214             try {
215 
216                 if (model.isProgramFound()) {
217 
218                     // can edit protocol
219 
220                     if (editProtocolComboBox.getItemCount() == 2) {
221 
222                         editProtocolComboBox.removeAllItems();
223                         editProtocolComboBox.addItem(getUI().getEditProtocolButton());
224                         editProtocolComboBox.addItem(getUI().getCloneProtocolButton());
225                         editProtocolComboBox.addItem(getUI().getExportProtocolButton());
226                         editProtocolComboBox.addItem(getUI().getDeleteProtocolButton());
227 
228                     }
229 
230                 } else {
231 
232                     // can just export and delete protocol
233                     if (editProtocolComboBox.getItemCount() == 4) {
234 
235                         editProtocolComboBox.removeItem(getUI().getEditProtocolButton());
236                         editProtocolComboBox.removeItem(getUI().getCloneProtocolButton());
237 
238                     }
239 
240                 }
241 
242             } finally {
243 
244                 //reenable the combo actions
245                 editProtocolComboBox.putClientProperty(CAN_EDIT, true);
246             }
247 
248         });
249 
250         registerValidators(getValidator());
251 
252         listenValidatorValid(getValidator(), model);
253 
254         getValidator().setBean(model);
255 
256         ui.applyDataBinding(SelectCruiseUI.BINDING_NEW_CRUISE_BUTTON_ENABLED);
257         ui.applyDataBinding(SelectCruiseUI.BINDING_EDIT_CATCHES_BUTTON_ENABLED);
258         ui.applyDataBinding(SelectCruiseUI.BINDING_VALIDATE_CATCHES_BUTTON_ENABLED);
259     }
260 
261     @Override
262     protected JComponent getComponentToFocus() {
263         return getUI().getEditCatchesButton();
264     }
265 
266     @Override
267     public void onCloseUI() {
268         if (log.isDebugEnabled()) {
269             log.debug("closing: " + ui);
270         }
271         PropertyChangeListener[] listeners = getModel().getPropertyChangeListeners();
272         for (PropertyChangeListener listener : listeners) {
273             getModel().removePropertyChangeListener(listener);
274         }
275         clearValidators();
276     }
277 
278 }