View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.operation.catches.marinelitter;
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.base.Preconditions;
26  import com.google.common.collect.Lists;
27  import fr.ifremer.tutti.persistence.entities.TuttiEntities;
28  import fr.ifremer.tutti.persistence.entities.data.Attachment;
29  import fr.ifremer.tutti.persistence.entities.data.BatchContainer;
30  import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
31  import fr.ifremer.tutti.persistence.entities.data.MarineLitterBatch;
32  import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue;
33  import fr.ifremer.tutti.type.WeightUnit;
34  import fr.ifremer.tutti.ui.swing.content.operation.catches.AbstractTuttiBatchTableUIHandler;
35  import fr.ifremer.tutti.ui.swing.content.operation.catches.EditCatchesUIModel;
36  import fr.ifremer.tutti.ui.swing.content.operation.catches.marinelitter.create.CreateMarineLitterBatchUIModel;
37  import fr.ifremer.tutti.ui.swing.util.TuttiBeanMonitor;
38  import fr.ifremer.tutti.ui.swing.util.TuttiUI;
39  import fr.ifremer.tutti.ui.swing.util.TuttiUIUtil;
40  import fr.ifremer.tutti.ui.swing.util.attachment.AttachmentCellEditor;
41  import fr.ifremer.tutti.ui.swing.util.attachment.AttachmentCellRenderer;
42  import fr.ifremer.tutti.ui.swing.util.comment.CommentCellEditor;
43  import fr.ifremer.tutti.ui.swing.util.comment.CommentCellRenderer;
44  import jaxx.runtime.validator.swing.SwingValidator;
45  import org.apache.commons.logging.Log;
46  import org.apache.commons.logging.LogFactory;
47  import org.jdesktop.swingx.JXTable;
48  import org.jdesktop.swingx.table.DefaultTableColumnModelExt;
49  import org.nuiton.jaxx.application.bean.JavaBeanObjectPropagateChangeListener;
50  import org.nuiton.jaxx.application.swing.table.ColumnIdentifier;
51  import org.nuiton.validator.NuitonValidatorResult;
52  
53  import javax.swing.JComponent;
54  import java.util.List;
55  
56  /**
57   * @author Tony Chemit - chemit@codelutin.com
58   * @since 0.2
59   */
60  public class MarineLitterBatchUIHandler extends AbstractTuttiBatchTableUIHandler<MarineLitterBatchRowModel, MarineLitterBatchUIModel, MarineLitterBatchTableModel, MarineLitterBatchUI> {
61  
62      private static final Log log = LogFactory.getLog(MarineLitterBatchUIHandler.class);
63  
64      public MarineLitterBatchUIHandler() {
65          super(
66                  MarineLitterBatchRowModel.PROPERTY_MARINE_LITTER_CATEGORY,
67                  MarineLitterBatchRowModel.PROPERTY_MARINE_LITTER_SIZE_CATEGORY,
68                  MarineLitterBatchRowModel.PROPERTY_WEIGHT,
69                  MarineLitterBatchRowModel.PROPERTY_NUMBER,
70                  MarineLitterBatchRowModel.PROPERTY_COMMENT);
71      }
72  
73      //------------------------------------------------------------------------//
74      //-- AbstractTuttiBatchTableUIHandler methods                           --//
75      //------------------------------------------------------------------------//
76  
77      @Override
78      protected ColumnIdentifier<MarineLitterBatchRowModel> getCommentIdentifier() {
79          return MarineLitterBatchTableModel.COMMENT;
80      }
81  
82      @Override
83      protected ColumnIdentifier<MarineLitterBatchRowModel> getAttachementIdentifier() {
84          return MarineLitterBatchTableModel.ATTACHMENT;
85      }
86  
87      @Override
88      public void selectFishingOperation(FishingOperation bean) {
89  
90          boolean empty = bean == null;
91  
92          MarineLitterBatchUIModel model = getModel();
93  
94          List<MarineLitterBatchRowModel> rows;
95  
96          if (empty) {
97              rows = null;
98          } else {
99  
100             if (log.isDebugEnabled()) {
101                 log.debug("Get marineLitter batch for fishingOperation: " +
102                                   bean.getId());
103             }
104             rows = Lists.newArrayList();
105 
106             if (!TuttiEntities.isNew(bean)) {
107 
108                 // get all marine litter root
109                 BatchContainer<MarineLitterBatch> batchContainer =
110                         getPersistenceService().getRootMarineLitterBatch(bean.getIdAsInt());
111 
112                 for (MarineLitterBatch aBatch : batchContainer.getChildren()) {
113                     MarineLitterBatchRowModel entry = loadBatch(aBatch);
114                     rows.add(entry);
115                 }
116             }
117         }
118         model.setRows(rows);
119     }
120 
121     //------------------------------------------------------------------------//
122     //-- AbstractTuttiTableUIHandler methods                                --//
123     //------------------------------------------------------------------------//
124 
125     @Override
126     public MarineLitterBatchTableModel getTableModel() {
127         return (MarineLitterBatchTableModel) getTable().getModel();
128     }
129 
130     @Override
131     public JXTable getTable() {
132         return ui.getTable();
133     }
134 
135     @Override
136     protected boolean isRowValid(MarineLitterBatchRowModel row) {
137         MarineLitterBatch batch = row.toEntity();
138         NuitonValidatorResult validator = getValidationService().validateEditMarineLitterBatch(batch);
139         return !validator.hasErrorMessagess();
140     }
141 
142     @Override
143     protected void saveSelectedRowIfRequired(TuttiBeanMonitor<MarineLitterBatchRowModel> rowMonitor,
144                                              MarineLitterBatchRowModel row) {
145 
146         if (row != null && row.isValid() && rowMonitor.wasModified()) {
147 
148             // monitored bean was modified, save it
149             if (log.isDebugEnabled()) {
150                 log.debug("Row " + row + " was modified, will save it");
151             }
152 
153             String title = buildReminderLabelTitle(decorate(row.getMarineLitterCategory()) + " - " + decorate(row.getMarineLitterSizeCategory()),
154                                                    null,
155                                                    "Sauvegarde du lot Macro déchet : ",
156                                                    "Ligne :" + (getTableModel().getRowIndex(row) + 1));
157 
158             showInformationMessage(title);
159 
160             rowMonitor.setBean(null);
161             saveRow(row);
162             rowMonitor.setBean(row);
163 
164             // clear modified flag on the monitor
165             rowMonitor.clearModified();
166         }
167     }
168 
169     @Override
170     protected void onModelRowsChanged(List<MarineLitterBatchRowModel> rows) {
171         super.onModelRowsChanged(rows);
172 
173         for (MarineLitterBatchRowModel row : rows) {
174             // update categoriesUsed
175             addToMarineLitterCategoriesUsed(row);
176         }
177     }
178 
179     //------------------------------------------------------------------------//
180     //-- AbstractTuttiUIHandler methods                                     --//
181     //------------------------------------------------------------------------//
182 
183     @Override
184     public SwingValidator<MarineLitterBatchUIModel> getValidator() {
185         return ui.getValidator();
186     }
187 
188     @Override
189     public void beforeInit(MarineLitterBatchUI ui) {
190 
191         super.beforeInit(ui);
192         if (log.isDebugEnabled()) {
193             log.debug("beforeInit: " + ui);
194         }
195 
196         EditCatchesUIModel catchesUIModel = ui.getContextValue(EditCatchesUIModel.class);
197 
198         MarineLitterBatchUIModel model = new MarineLitterBatchUIModel(catchesUIModel, getConfig().getMarineLitterWeightUnit());
199         ui.setContextValue(model);
200 
201         // propagate when value is changing
202         JavaBeanObjectPropagateChangeListener.listenAndPropagate(
203                 catchesUIModel,
204                 model,
205                 EditCatchesUIModel.PROPERTY_MARINE_LITTER_TOTAL_WEIGHT,
206                 EditCatchesUIModel.PROPERTY_MARINE_LITTER_TOTAL_WEIGHT);
207     }
208 
209     @Override
210     public void afterInit(MarineLitterBatchUI ui) {
211 
212         if (log.isDebugEnabled()) {
213             log.debug("afterInit: " + ui);
214         }
215 
216         initUI(ui);
217 
218         JXTable table = getTable();
219 
220         // create table column model
221         DefaultTableColumnModelExt columnModel = new DefaultTableColumnModelExt();
222 
223         {
224             // Id column
225 
226             addIdColumnToModel(columnModel, MarineLitterBatchTableModel.ID, table);
227 
228         }
229 
230         {
231             // MarineLitter Category column
232 
233             addColumnToModel(columnModel,
234                              null,
235                              newTableCellRender(CaracteristicQualitativeValue.class, null),
236                              MarineLitterBatchTableModel.MACRO_WASTE_CATEGORY);
237         }
238 
239         {
240             // MarineLitter Size Category column
241 
242             addColumnToModel(columnModel,
243                              null,
244                              newTableCellRender(CaracteristicQualitativeValue.class, null),
245                              MarineLitterBatchTableModel.MACRO_WASTE_SIZE_CATEGORY);
246         }
247 
248         { // Number column
249 
250             addIntegerColumnToModel(columnModel,
251                                     MarineLitterBatchTableModel.NUMBER,
252                                     TuttiUI.INT_3_DIGITS_PATTERN,
253                                     table);
254         }
255 
256         WeightUnit weightUnit = getModel().getWeightUnit();
257 
258         { // Weight column
259 
260             addFloatColumnToModel(columnModel,
261                                   MarineLitterBatchTableModel.WEIGHT,
262                                   weightUnit,
263                                   table);
264         }
265 
266         { // Comment column
267 
268             addColumnToModel(columnModel,
269                              CommentCellEditor.newEditor(ui),
270                              CommentCellRenderer.newRender(),
271                              MarineLitterBatchTableModel.COMMENT);
272         }
273 
274         { // File column
275 
276             addColumnToModel(columnModel,
277                              AttachmentCellEditor.newEditor(ui),
278                              AttachmentCellRenderer.newRender(getDecorator(Attachment.class, null)),
279                              MarineLitterBatchTableModel.ATTACHMENT);
280         }
281 
282         // create table model
283         MarineLitterBatchTableModel tableModel = new MarineLitterBatchTableModel(weightUnit, columnModel);
284 
285         table.setModel(tableModel);
286         table.setColumnModel(columnModel);
287 
288         initBatchTable(table, columnModel, tableModel);
289     }
290 
291     @Override
292     protected JComponent getComponentToFocus() {
293         return getUI().getTable();
294     }
295 
296     @Override
297     public void onCloseUI() {
298         if (log.isDebugEnabled()) {
299             log.debug("closing: " + ui);
300         }
301         ui.getMarineLitterBatchAttachmentsButton().onCloseUI();
302     }
303 
304     @Override
305     protected void beforeOpenPopup(int rowIndex, int columnIndex) {
306         super.beforeOpenPopup(rowIndex, columnIndex);
307 
308         boolean enableRemove = false;
309 
310         if (rowIndex != -1) {
311 
312             // there is a selected row
313             enableRemove = true;
314         }
315         MarineLitterBatchUIModel model = getModel();
316         model.setRemoveBatchEnabled(enableRemove);
317     }
318 
319     //------------------------------------------------------------------------//
320     //-- Public methods                                                     --//
321     //------------------------------------------------------------------------//
322 
323     public void addBatch(CreateMarineLitterBatchUIModel model) {
324         if (model.isValid()) {
325 
326             MarineLitterBatchTableModel tableModel = getTableModel();
327 
328             MarineLitterBatchRowModel newRow = tableModel.createNewRow();
329             newRow.setMarineLitterCategory(model.getMarineLitterCategory());
330             newRow.setMarineLitterSizeCategory(model.getMarineLitterSizeCategory());
331             newRow.setNumber(model.getNumber());
332             newRow.setWeight(model.getWeight());
333 
334             recomputeRowValidState(newRow);
335 
336             saveRow(newRow);
337 
338             tableModel.addNewRow(newRow);
339             TuttiUIUtil.selectFirstCellOnLastRow(getTable());
340 
341             //update categories used
342             addToMarineLitterCategoriesUsed(newRow);
343         }
344     }
345 
346     //------------------------------------------------------------------------//
347     //-- Internal methods                                                   --//
348     //------------------------------------------------------------------------//
349 
350     protected MarineLitterBatchRowModel loadBatch(MarineLitterBatch aBatch) {
351 
352         MarineLitterBatchRowModel newRow =
353                 new MarineLitterBatchRowModel(getModel().getWeightUnit(), aBatch);
354 
355         List<Attachment> attachments =
356                 getPersistenceService().getAllAttachments(newRow.getObjectType(), newRow.getObjectId());
357 
358         newRow.addAllAttachment(attachments);
359         return newRow;
360     }
361 
362     protected void saveRow(MarineLitterBatchRowModel row) {
363 
364         MarineLitterBatch entityToSave = row.toEntity();
365 
366         FishingOperation fishingOperation = getModel().getFishingOperation();
367         entityToSave.setFishingOperation(fishingOperation);
368         if (log.isInfoEnabled()) {
369             log.info("Selected fishingOperation: " + fishingOperation.getId());
370         }
371 
372         if (TuttiEntities.isNew(entityToSave)) {
373 
374             entityToSave = getPersistenceService().createMarineLitterBatch(entityToSave);
375             row.setId(entityToSave.getId());
376         } else {
377             getPersistenceService().saveMarineLitterBatch(entityToSave);
378         }
379 
380         fireBatchSaved(row);
381 
382     }
383 
384     public void removeFromMarineLitterCategoriesUsed(MarineLitterBatchRowModel row) {
385         Preconditions.checkNotNull(row);
386         Preconditions.checkNotNull(row.getMarineLitterCategory());
387         Preconditions.checkNotNull(row.getMarineLitterSizeCategory());
388         if (log.isInfoEnabled()) {
389             log.info("Remove from speciesUsed: " +
390                              decorate(row.getMarineLitterSizeCategory()) +
391                              " - " + decorate(row.getMarineLitterCategory()));
392         }
393         MarineLitterBatchUIModel model = getModel();
394         model.getMarineLitterCategoriesUsed().remove(row.getMarineLitterSizeCategory(),
395                                                      row.getMarineLitterCategory());
396 
397     }
398 
399     protected void addToMarineLitterCategoriesUsed(MarineLitterBatchRowModel row) {
400         Preconditions.checkNotNull(row);
401         Preconditions.checkNotNull(row.getMarineLitterCategory());
402         Preconditions.checkNotNull(row.getMarineLitterSizeCategory());
403         if (log.isDebugEnabled()) {
404             log.debug("Add to marineLitterCategoriesUsed: " +
405                               decorate(row.getMarineLitterSizeCategory()) +
406                               " - " + decorate(row.getMarineLitterCategory()));
407         }
408         MarineLitterBatchUIModel model = getModel();
409         model.getMarineLitterCategoriesUsed().put(row.getMarineLitterSizeCategory(),
410                                                   row.getMarineLitterCategory());
411 
412     }
413 }