View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.validation.actions;
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 fr.ifremer.tutti.persistence.entities.data.CatchBatch;
26  import fr.ifremer.tutti.persistence.entities.data.Cruise;
27  import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
28  import fr.ifremer.tutti.service.catches.ValidateCruiseOperationsService;
29  import fr.ifremer.tutti.ui.swing.content.operation.EditFishingOperationUIModel;
30  import fr.ifremer.tutti.ui.swing.content.operation.FishingOperationsUI;
31  import fr.ifremer.tutti.ui.swing.content.operation.catches.AbstractTuttiBatchUIModel;
32  import fr.ifremer.tutti.ui.swing.content.operation.catches.BatchSavedEvent;
33  import fr.ifremer.tutti.ui.swing.content.operation.catches.BatchSavedListener;
34  import fr.ifremer.tutti.ui.swing.content.operation.catches.EditCatchesUI;
35  import fr.ifremer.tutti.ui.swing.content.operation.catches.EditCatchesUIModel;
36  import fr.ifremer.tutti.ui.swing.content.operation.catches.species.edit.SpeciesBatchUI;
37  import fr.ifremer.tutti.ui.swing.content.validation.ValidateCruiseUI;
38  import fr.ifremer.tutti.ui.swing.content.validation.ValidateCruiseUIHandler;
39  import fr.ifremer.tutti.ui.swing.content.validation.ValidateCruiseUIModel;
40  import fr.ifremer.tutti.ui.swing.util.actions.LongActionSupport;
41  import org.nuiton.validator.NuitonValidatorResult;
42  
43  import java.beans.PropertyChangeEvent;
44  import java.beans.PropertyChangeListener;
45  import java.util.Arrays;
46  import java.util.List;
47  
48  import static org.nuiton.i18n.I18n.t;
49  
50  /**
51   * To edit the given fishing operation.
52   *
53   * @author Kevin Morin - kmorin@codelutin.com
54   * @since 1.4
55   */
56  public class EditFishingOperationInValidationUIAction extends LongActionSupport<ValidateCruiseUIModel, ValidateCruiseUI, ValidateCruiseUIHandler> {
57  
58      /** Validation service. */
59      private final ValidateCruiseOperationsService validationService;
60  
61      /**
62       * The incoming fishing operation to edit.
63       *
64       * Can be null (means do not edit any fishing operation), or with no id
65       * (means create a ne fishing operation), or with an id (means edit an
66       * existing fishing operation).
67       */
68      protected FishingOperation fishingOperation;
69  
70      protected final PropertyChangeListener editFishingOperationModelListener = new PropertyChangeListener() {
71  
72          protected final List<String> propertiesToIgnore = Arrays.asList(EditFishingOperationUIModel.PROPERTY_VALID, EditFishingOperationUIModel.PROPERTY_PERSISTED);
73  
74          @Override
75          public void propertyChange(PropertyChangeEvent evt) {
76  
77              EditFishingOperationUIModel model = (EditFishingOperationUIModel) evt.getSource();
78  
79              if (!model.isLoadingData() && model.getFishingOperation() != null) {
80  
81                  if (EditFishingOperationUIModel.PROPERTY_MODIFY.equals(evt.getPropertyName())) {
82  
83                      if (!model.isModify()) {
84  
85                          // after a save, or a reset, reload model cruise, since the synchronizationStatus may have changed
86                          Cruise cruise = getDataContext().reloadCruise();
87                          getModel().setCruise(cruise);
88                      }
89  
90                  } else if (!propertiesToIgnore.contains(evt.getPropertyName())) {
91                      model.convertGearShootingCoordinatesToDD();
92  
93                      FishingOperation operation = model.toEntity();
94                      NuitonValidatorResult validationResult = validationService.validateCruiseOperation(operation);
95  
96                      ValidateCruiseUIModel uiModel = getModel();
97                      uiModel.addValidatorResult(uiModel.getSelectedFishingOperation(), validationResult);
98  
99                      getHandler().updateCurrentOperationNode(validationResult);
100                 }
101             }
102         }
103     };
104 
105     protected final PropertyChangeListener editCatchesModelListener = new PropertyChangeListener() {
106 
107         protected final List<String> propertiesToIgnore = Arrays.asList(
108                 EditCatchesUIModel.PROPERTY_VALID,
109                 EditCatchesUIModel.PROPERTY_CATCH_TOTAL_COMPUTED_WEIGHT,
110                 EditCatchesUIModel.PROPERTY_CATCH_TOTAL_SORTED_COMPUTED_WEIGHT,
111                 EditCatchesUIModel.PROPERTY_CATCH_TOTAL_UNSORTED_COMPUTED_WEIGHT,
112                 EditCatchesUIModel.PROPERTY_CATCH_TOTAL_REJECTED_COMPUTED_WEIGHT,
113                 EditCatchesUIModel.PROPERTY_SPECIES_TOTAL_COMPUTED_WEIGHT,
114                 EditCatchesUIModel.PROPERTY_SPECIES_TOTAL_SORTED_COMPUTED_WEIGHT,
115                 EditCatchesUIModel.PROPERTY_SPECIES_TOTAL_UNSORTED_COMPUTED_WEIGHT,
116                 EditCatchesUIModel.PROPERTY_SPECIES_TOTAL_SAMPLE_SORTED_COMPUTED_WEIGHT,
117                 EditCatchesUIModel.PROPERTY_SPECIES_TOTAL_INERT_COMPUTED_WEIGHT,
118                 EditCatchesUIModel.PROPERTY_SPECIES_TOTAL_LIVING_NOT_ITEMIZED_COMPUTED_WEIGHT,
119                 EditCatchesUIModel.PROPERTY_BENTHOS_TOTAL_COMPUTED_WEIGHT,
120                 EditCatchesUIModel.PROPERTY_BENTHOS_TOTAL_SORTED_COMPUTED_WEIGHT,
121                 EditCatchesUIModel.PROPERTY_BENTHOS_TOTAL_UNSORTED_COMPUTED_WEIGHT,
122                 EditCatchesUIModel.PROPERTY_BENTHOS_TOTAL_SAMPLE_SORTED_COMPUTED_WEIGHT,
123                 EditCatchesUIModel.PROPERTY_BENTHOS_TOTAL_INERT_COMPUTED_WEIGHT,
124                 EditCatchesUIModel.PROPERTY_BENTHOS_TOTAL_LIVING_NOT_ITEMIZED_COMPUTED_WEIGHT
125         );
126 
127         @Override
128         public void propertyChange(PropertyChangeEvent evt) {
129             EditCatchesUIModel model = (EditCatchesUIModel) evt.getSource();
130 
131             if (!model.isLoadingData() && model.getFishingOperation() != null) {
132 
133                 if (EditFishingOperationUIModel.PROPERTY_MODIFY.equals(evt.getPropertyName())) {
134 
135                     if (!model.isModify()) {
136 
137                         // after a save, or a reset, reload model cruise, since the synchronizationStatus may have changed
138                         Cruise cruise = getDataContext().reloadCruise();
139                         getModel().setCruise(cruise);
140                     }
141 
142                 } else if (!propertiesToIgnore.contains(evt.getPropertyName())) {
143                     CatchBatch catchBatch = model.toEntity();
144                     NuitonValidatorResult validationResult = validationService.validateCruiseOperation(catchBatch);
145 
146                     ValidateCruiseUIModel uiModel = getModel();
147                     uiModel.addValidatorResult(uiModel.getSelectedFishingOperation(), validationResult);
148 
149                     getHandler().updateCurrentOperationNode(validationResult);
150                 }
151             }
152         }
153     };
154 
155     protected final BatchSavedListener batchSavedListener = new BatchSavedListener() {
156 
157         @Override
158         public void onBatchSaved(BatchSavedEvent evt) {
159 
160             AbstractTuttiBatchUIModel model = evt.getSource();
161 
162             if (model.getFishingOperation() != null) {
163 
164                 // after a save, or a reset, reload model cruise, since the synchronizationStatus may have changed
165                 Cruise cruise = getDataContext().reloadCruise();
166                 getModel().setCruise(cruise);
167 
168                 NuitonValidatorResult validationResult = validationService.validateCruiseOperation(model.getFishingOperation());
169 
170                 ValidateCruiseUIModel uiModel = getModel();
171                 uiModel.addValidatorResult(uiModel.getSelectedFishingOperation(), validationResult);
172 
173                 getHandler().updateCurrentOperationNode(validationResult);
174             }
175         }
176     };
177 
178     public EditFishingOperationInValidationUIAction(ValidateCruiseUIHandler handler) {
179         super(handler, true);
180         setActionDescription(t("tutti.editFishingOperation.action.editFishingOperation.tip"));
181         validationService = getContext().getValidateCruiseOperationsService();
182     }
183 
184     public void setFishingOperation(FishingOperation fishingOperation) {
185         this.fishingOperation = fishingOperation;
186     }
187 
188     @Override
189     public boolean prepareAction() throws Exception {
190 
191         removeListener();
192 
193         return super.prepareAction();
194     }
195 
196     public void removeListener() {
197         FishingOperationsUI operationPanel = getUI().getOperationPanel();
198         operationPanel.getFishingOperationTabContent().getModel().removePropertyChangeListener(editFishingOperationModelListener);
199 
200         EditCatchesUI catchesTabContent = operationPanel.getCatchesTabContent();
201 
202         catchesTabContent.getModel().removePropertyChangeListener(editCatchesModelListener);
203 
204         getSpeciesBatchUI().getHandler().removeBatchSavedListener(batchSavedListener);
205         getBenthosBatchUI().getHandler().removeBatchSavedListener(batchSavedListener);
206         catchesTabContent.getAccidentalTabContent().getHandler().removeBatchSavedListener(batchSavedListener);
207         catchesTabContent.getMarineLitterTabContent().getHandler().removeBatchSavedListener(batchSavedListener);
208     }
209 
210     @Override
211     public void doAction() throws Exception {
212         getUI().getOperationPanel().getModel().setSelectedFishingOperation(fishingOperation);
213     }
214 
215     @Override
216     public void postSuccessAction() {
217         super.postSuccessAction();
218 
219         FishingOperationsUI operationPanel = getUI().getOperationPanel();
220         operationPanel.getFishingOperationTabContent().getModel().addPropertyChangeListener(editFishingOperationModelListener);
221 
222         EditCatchesUI catchesTabContent = operationPanel.getCatchesTabContent();
223 
224         catchesTabContent.getModel().addPropertyChangeListener(editCatchesModelListener);
225 
226         getSpeciesBatchUI().getHandler().addBatchSavedListener(batchSavedListener);
227         getBenthosBatchUI().getHandler().addBatchSavedListener(batchSavedListener);
228         catchesTabContent.getAccidentalTabContent().getHandler().addBatchSavedListener(batchSavedListener);
229         catchesTabContent.getMarineLitterTabContent().getHandler().addBatchSavedListener(batchSavedListener);
230     }
231 
232     @Override
233     public void releaseAction() {
234         super.releaseAction();
235         fishingOperation = null;
236     }
237 
238     private SpeciesBatchUI getBenthosBatchUI() {
239         return getUI().getOperationPanel().getCatchesTabContent().getBenthosTabPanel().getEditBatchesUI();
240     }
241 
242     private SpeciesBatchUI getSpeciesBatchUI() {
243         return getUI().getOperationPanel().getCatchesTabContent().getSpeciesTabPanel().getEditBatchesUI();
244     }
245 }