View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.operation;
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.TuttiEntities;
27  import fr.ifremer.tutti.persistence.entities.data.Cruise;
28  import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
29  import fr.ifremer.tutti.persistence.entities.protocol.TuttiProtocol;
30  import fr.ifremer.tutti.service.PersistenceService;
31  import fr.ifremer.tutti.ui.swing.TuttiUIContext;
32  import fr.ifremer.tutti.ui.swing.content.operation.catches.species.BenthosBatchUISupportImpl;
33  import fr.ifremer.tutti.ui.swing.content.operation.catches.EditCatchesUI;
34  import fr.ifremer.tutti.ui.swing.content.operation.catches.EditCatchesUIModel;
35  import fr.ifremer.tutti.ui.swing.content.operation.catches.species.SpeciesBatchUISupportImpl;
36  import fr.ifremer.tutti.ui.swing.content.operation.catches.species.SpeciesOrBenthosBatchUISupport;
37  import fr.ifremer.tutti.ui.swing.content.operation.fishing.actions.EditFishingOperationAction;
38  import fr.ifremer.tutti.ui.swing.util.AbstractTuttiTabContainerUIHandler;
39  import jaxx.runtime.validator.swing.SwingValidator;
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  import org.nuiton.jaxx.application.swing.util.CloseableUI;
43  
44  import javax.swing.JComponent;
45  import javax.swing.JTabbedPane;
46  import javax.swing.SwingUtilities;
47  import java.util.List;
48  
49  import static org.nuiton.i18n.I18n.t;
50  
51  /**
52   * Handler of UI {@link FishingOperationsUI}.
53   *
54   * @author Tony Chemit - chemit@codelutin.com
55   * @since 0.1
56   */
57  public class FishingOperationsUIHandler extends AbstractTuttiTabContainerUIHandler<FishingOperationsUIModel, FishingOperationsUI> implements CloseableUI {
58  
59      /** Logger. */
60      private static final Log log =
61              LogFactory.getLog(FishingOperationsUIHandler.class);
62  
63      protected EditFishingOperationAction editFishingOperationAction;
64  
65      //------------------------------------------------------------------------//
66      //-- AbstractTuttiUIHandler methods                                     --//
67      //------------------------------------------------------------------------//
68  
69      @Override
70      public void beforeInit(FishingOperationsUI ui) {
71  
72          super.beforeInit(ui);
73  
74          if (log.isDebugEnabled()) {
75              log.debug("for " + this.ui);
76          }
77          FishingOperationsUIModel model = new FishingOperationsUIModel();
78  
79          EditCatchesUIModel catchesUIModel = new EditCatchesUIModel(
80                  getConfig().getSpeciesWeightUnit(),
81                  getConfig().getBenthosWeightUnit(),
82                  getConfig().getMarineLitterWeightUnit());
83  
84          SpeciesOrBenthosBatchUISupport speciesBatchUISupport = new SpeciesBatchUISupportImpl(
85                  getContext(),
86                  catchesUIModel,
87                  getConfig().getSpeciesWeightUnit());
88  
89          SpeciesOrBenthosBatchUISupport benthosBatchUISupport = new BenthosBatchUISupportImpl(
90                  getContext(),
91                  catchesUIModel,
92                  getConfig().getBenthosWeightUnit());
93  
94  
95          // load existing cruise
96          Cruise cruise = getDataContext().getCruise();
97  
98          PersistenceService persistenceService = getPersistenceService();
99  
100         TuttiProtocol protocol;
101         if (getContext().isProtocolFilled()) {
102 
103             // load existing protocol
104 
105             protocol = persistenceService.getProtocol(
106                     getContext().getProtocolId());
107 
108             if (log.isInfoEnabled()) {
109                 log.info("Loading existing protocol: " + protocol);
110             }
111         }
112 
113         List<FishingOperation> fishingOperations =
114                 Lists.newArrayList(persistenceService.getAllFishingOperation(cruise.getIdAsInt()));
115 
116         for (FishingOperation fishingOperation : fishingOperations) {
117             fishingOperation.setCruise(cruise);
118         }
119         model.setFishingOperation(fishingOperations);
120 
121         if (log.isInfoEnabled()) {
122             log.info("Loaded " + fishingOperations.size() +
123                      " fishingOperation(s).");
124         }
125 
126         this.ui.setContextValue(model);
127         this.ui.setContextValue(catchesUIModel);
128         this.ui.setContextValue(speciesBatchUISupport, SpeciesOrBenthosBatchUISupport.SPECIES);
129         this.ui.setContextValue(benthosBatchUISupport, SpeciesOrBenthosBatchUISupport.BENTHOS);
130     }
131 
132     @Override
133     public void afterInit(FishingOperationsUI ui) {
134 
135         initUI(this.ui);
136 
137         editFishingOperationAction =
138                 getContext().getActionFactory().createLogicAction(this, EditFishingOperationAction.class);
139 
140         FishingOperationsUIModel model = getModel();
141 
142         List<FishingOperation> fishingOperations = model.getFishingOperation();
143 
144         initBeanFilterableComboBox(this.ui.getFishingOperationComboBox(),
145                                    fishingOperations,
146                                    model.getSelectedFishingOperation());
147 
148         model.addPropertyChangeListener(FishingOperationsUIModel.PROPERTY_SELECTED_FISHING_OPERATION, evt -> {
149             if (log.isDebugEnabled()) {
150                 log.debug("propertyChange " + FishingOperationsUIModel.PROPERTY_SELECTED_FISHING_OPERATION);
151             }
152 
153             // selected fishing operation is now the editing one
154             FishingOperation newValue = (FishingOperation) evt.getNewValue();
155 
156             if (!getModel().isEditionAdjusting()) {
157 
158                 FishingOperation operation;
159                 if (newValue == null) {
160                     operation = null;
161                 } else {
162                     operation = getPersistenceService().getFishingOperation(newValue.getIdAsInt());
163                     Cruise cruise = getDataContext().getCruise();
164                     operation.setCruise(cruise);
165                 }
166                 editFishingOperationAction.setFishingOperation(operation);
167                 if (SwingUtilities.isEventDispatchThread()) {
168 
169                     // launch a long action
170                     getContext().getActionEngine().runAction(editFishingOperationAction);
171                 } else {
172 
173                     // run as an internal action (of embedded action)
174                     getContext().getActionEngine().runInternalAction(editFishingOperationAction);
175                 }
176             }
177 
178             // done here instead of in the action in order to update the headers
179             // when the operation is saved
180             String fishingOperationText = getFishingOperationTitle(newValue);
181 
182             EditFishingOperationUI efoUI = getUI().getFishingOperationTabContent();
183             efoUI.getTraitGeneralTabPane().setTitle(fishingOperationText);
184             efoUI.getVesselUseFeatureTabPane().setTitle(fishingOperationText);
185             efoUI.getGearUseFeatureTabPane().setTitle(fishingOperationText);
186 
187             EditCatchesUI ecUI = getUI().getCatchesTabContent();
188             ecUI.getCatchesCaracteristicsTabPane().setTitle(fishingOperationText);
189             ecUI.getSpeciesTabPanel().getEditBatchesUIPanel().setTitle(fishingOperationText);
190             ecUI.getBenthosTabPanel().getEditBatchesUIPanel().setTitle(fishingOperationText);
191             ecUI.getMarineLitterTabFishingOperationReminderLabel().setTitle(fishingOperationText);
192             ecUI.getAccidentalTabFishingOperationReminderLabel().setTitle(fishingOperationText);
193 
194         });
195 
196         model.addPropertyChangeListener(FishingOperationsUIModel.PROPERTY_FISHING_OPERATION, evt -> {
197             if (log.isDebugEnabled()) {
198                 log.debug("propertyChange " + FishingOperationsUIModel.PROPERTY_FISHING_OPERATION);
199             }
200             FishingOperationsUIHandler.this.ui.getFishingOperationComboBox().setData(null);
201             FishingOperationsUIHandler.this.ui.getFishingOperationComboBox().setData((List<FishingOperation>) evt.getNewValue());
202         });
203 
204 //        FishingOperation selectedOperation = null;
205 //        for (FishingOperation fishingOperation : fishingOperations) {
206 //            if (selectedOperation == null ||
207 //                fishingOperation.getGearShootingStartDate()
208 //                        .after(selectedOperation.getGearShootingStartDate())
209 //                && fishingOperation.getFishingOperationNumber()
210 //                   > selectedOperation.getFishingOperationNumber()
211 //                    ) {
212 //                selectedOperation = fishingOperation;
213 //            }
214 //        }
215 
216         getContext().addPropertyChangeListener(TuttiUIContext.PROPERTY_HIDE_BODY, evt -> {
217             Boolean hideBody = (Boolean) evt.getNewValue();
218             if (hideBody != null && hideBody) {
219                 if (getModel().getSelectedFishingOperation() == null) {
220                     getUI().getFishingOperationComboBox().requestFocus();
221                 }
222             }
223         });
224     }
225 
226     public EditFishingOperationAction getEditFishingOperationAction() {
227         return editFishingOperationAction;
228     }
229 
230     @Override
231     protected JComponent getComponentToFocus() {
232         return null;
233     }
234 
235     @Override
236     public void onCloseUI() {
237 
238         // ui will be saved so we do not want to keep selected tab indexes
239         ui.getTabPane().setSelectedIndex(0);
240         ui.getFishingOperationTabContent().getFishingOperationTabPane().setSelectedIndex(0);
241 
242         closeUI(ui.getFishingOperationTabContent());
243         closeUI(ui.getCatchesTabContent());
244     }
245 
246     @Override
247     public boolean quitUI() {
248 
249         // reuse the editFishingOperationAction#prepareAction code
250         FishingOperation editFishingOperation = getModel().getEditFishingOperation();
251         editFishingOperationAction.setFishingOperation(editFishingOperation);
252         editFishingOperationAction.setCheckPreviousEdit(true);
253 
254         try {
255             return editFishingOperationAction.prepareAction();
256         } finally {
257             editFishingOperationAction.releaseAction();
258         }
259     }
260 
261     @Override
262     public SwingValidator<FishingOperationsUIModel> getValidator() {
263         return null;
264     }
265 
266     //------------------------------------------------------------------------//
267     //-- AbstractTuttiTabContainerUIHandler methods                         --//
268     //------------------------------------------------------------------------//
269 
270     @Override
271     public JTabbedPane getTabPanel() {
272         return ui.getTabPane();
273     }
274 
275     @Override
276     public boolean removeTab(int i) {
277         return false;
278     }
279 
280     //------------------------------------------------------------------------//
281     //-- Public methods                                                     --//
282     //------------------------------------------------------------------------//
283 
284     public boolean isFishingOperationModified() {
285         return getModel().getEditFishingOperation() != null &&
286                getUI().getFishingOperationTabContent().getModel().isModify();
287     }
288 
289     public boolean isFishingOperationValid() {
290         return getModel().getEditFishingOperation() != null &&
291                getUI().getFishingOperationTabContent().getModel().isValid();
292     }
293 
294     public boolean isCatchBatchModified() {
295         return getModel().getEditFishingOperation() != null &&
296                getUI().getCatchesTabContent().getModel().isModify();
297     }
298 
299     public boolean isCatchBatchValid() {
300         return getModel().getEditFishingOperation() != null &&
301                getUI().getCatchesTabContent().getModel().isValid();
302     }
303 
304     public String getFishingOperationTitle(FishingOperation bean) {
305         String fishingOperationText;
306 
307         if (bean == null) {
308             fishingOperationText = null;
309         } else if (TuttiEntities.isNew(bean)) {
310             fishingOperationText = t("tutti.editFishingOperation.label.traitReminder",
311                                      t("tutti.editFishingOperation.label.traitReminder.inCreation"));
312         } else {
313             fishingOperationText = t("tutti.editFishingOperation.label.traitReminder",
314                                      decorate(bean));
315         }
316         return fishingOperationText;
317     }
318 }