View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.operation.catches.species.edit;
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.Iterables;
26  import com.google.common.collect.Lists;
27  import com.google.common.collect.Maps;
28  import com.google.common.collect.Multimap;
29  import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue;
30  import fr.ifremer.tutti.persistence.entities.referential.Species;
31  import fr.ifremer.tutti.type.WeightUnit;
32  import fr.ifremer.tutti.ui.swing.content.operation.catches.AbstractTuttiBatchUIModel;
33  import fr.ifremer.tutti.ui.swing.content.operation.catches.EditCatchesUIModel;
34  import fr.ifremer.tutti.ui.swing.content.operation.catches.species.SpeciesOrBenthosBatchUISupport;
35  import fr.ifremer.tutti.ui.swing.content.operation.catches.species.frequency.SpeciesFrequencyRowModel;
36  import fr.ifremer.tutti.ui.swing.util.computable.ComputableData;
37  import fr.ifremer.tutti.util.Numbers;
38  import org.apache.commons.collections4.CollectionUtils;
39  import org.nuiton.jaxx.application.swing.tab.TabContentModel;
40  
41  import java.util.List;
42  import java.util.Map;
43  
44  /**
45   * @author Tony Chemit - chemit@codelutin.com
46   * @since 0.1
47   */
48  public class SpeciesBatchUIModel extends AbstractTuttiBatchUIModel<SpeciesBatchRowModel, SpeciesBatchUIModel>
49          implements TabContentModel, SpeciesSortableRowModel {
50  
51      private static final long serialVersionUID = 1L;
52  
53      public static final String PROPERTY_SPLIT_BATCH_ENABLED = "splitBatchEnabled";
54  
55      public static final String PROPERTY_CHANGE_SAMPLE_CATEGORY_ENABLED = "changeSampleCategoryEnabled";
56  
57      public static final String PROPERTY_ADD_SAMPLE_CATEGORY_ENABLED = "addSampleCategoryEnabled";
58  
59      public static final String PROPERTY_REMOVE_SUB_BATCH_ENABLED = "removeSubBatchEnabled";
60  
61      public static final String PROPERTY_RENAME_BATCH_ENABLED = "renameBatchEnabled";
62  
63      public static final String PROPERTY_REMOVE_BATCH_ENABLED = "removeBatchEnabled";
64  
65      public static final String PROPERTY_CREATE_MELAG_ENABLED = "createMelagEnabled";
66  
67      public static final String PROPERTY_EDIT_FREQUENCIES_ENABLED = "editFrequenciesEnabled";
68  
69      public static final String PROPERTY_TABLE_VIEW_MODE = "tableViewMode";
70  
71      public static final String PROPERTY_TABLE_VIEW_MODE_ALL = "tableViewModeAll";
72  
73      public static final String PROPERTY_TABLE_VIEW_MODE_LEAF = "tableViewModeLeaf";
74  
75      public static final String PROPERTY_TABLE_VIEW_MODE_ROOT = "tableViewModeRoot";
76  
77      public static final String PROPERTY_ROOT_NUMBER = "rootNumber";
78  
79      public static final String PROPERTY_LEAF_NUMBER = "leafNumber";
80  
81      /**
82       * Can user split a selected species batch ?
83       *
84       * @since 0.3
85       */
86      protected boolean splitBatchEnabled;
87  
88      /**
89       * Can user change a sample category of the selected species batch ?
90       *
91       * @since 2.6
92       */
93      protected boolean changeSampleCategoryEnabled;
94  
95      /**
96       * Can user add a/some missing sample category of the selected species batch ?
97       *
98       * @since 2.6
99       */
100     protected boolean addSampleCategoryEnabled;
101 
102     /**
103      * Can user remove a selected species batch?
104      *
105      * @since 0.3
106      */
107     protected boolean removeBatchEnabled;
108 
109     /**
110      * Can user remove a selected species sub batches?
111      *
112      * @since 0.3
113      */
114     protected boolean removeSubBatchEnabled;
115 
116     /**
117      * Can user create a melag from the selected species?
118      *
119      * @since 0.3
120      */
121     protected boolean createMelagEnabled = true;
122 
123     /**
124      * Can user rename the selected species?
125      *
126      * @since 0.3
127      */
128     protected boolean renameBatchEnabled;
129 
130     /**
131      * Can user edit frequencies for the selected species?
132      *
133      * @since 2.3
134      */
135     private boolean editFrequenciesEnabled;
136 
137     /**
138      * What to show in the table.
139      *
140      * @since 0.3
141      */
142     protected TableViewMode tableViewMode;
143 
144     /** @since 1.0 */
145     protected int rootNumber;
146 
147     /** @since 1.0 */
148     protected int leafNumber;
149 
150     /**
151      * How to sort table.
152      *
153      * @since 3.0
154      */
155     protected SpeciesSortMode speciesSortMode;
156 
157     /**
158      * Which decorator index for species column decorate.
159      *
160      * @since 3.0
161      */
162     protected int speciesDecoratorContextIndex;
163 
164     /**
165      * Délégation de différences entre species et benthos.
166      *
167      * @since 4.5
168      */
169     protected final SpeciesOrBenthosBatchUISupport speciesOrBenthosBatchUISupport;
170 
171     public SpeciesBatchUIModel(SpeciesOrBenthosBatchUISupport speciesOrBenthosBatchUISupport) {
172         super(speciesOrBenthosBatchUISupport,
173               Iterables.toArray(speciesOrBenthosBatchUISupport.getCatchesUIModelPropertiesMapping().keySet(), String.class));
174 //              EditCatchesUIModel.PROPERTY_SPECIES_TOTAL_COMPUTED_WEIGHT,
175 //              EditCatchesUIModel.PROPERTY_SPECIES_TOTAL_SORTED_WEIGHT,
176 //              EditCatchesUIModel.PROPERTY_SPECIES_TOTAL_UNSORTED_COMPUTED_WEIGHT,
177 //              EditCatchesUIModel.PROPERTY_SPECIES_TOTAL_SAMPLE_SORTED_COMPUTED_WEIGHT,
178 //              EditCatchesUIModel.PROPERTY_SPECIES_TOTAL_INERT_WEIGHT,
179 //              EditCatchesUIModel.PROPERTY_SPECIES_TOTAL_LIVING_NOT_ITEMIZED_WEIGHT);
180         this.speciesOrBenthosBatchUISupport = speciesOrBenthosBatchUISupport;
181     }
182 
183     public SpeciesOrBenthosBatchUISupport getSpeciesOrBenthosBatchUISupport() {
184         return speciesOrBenthosBatchUISupport;
185     }
186 
187     public EditCatchesUIModel getCatchesUIModel() {
188         return speciesOrBenthosBatchUISupport.getCatchesUIModel();
189     }
190 
191     public WeightUnit getWeightUnit() {
192         return speciesOrBenthosBatchUISupport.getWeightUnit();
193     }
194 
195     public boolean canPupitriImport() {
196         return speciesOrBenthosBatchUISupport.canPupitriImport();
197     }
198 
199     public boolean canPsionImport() {
200         return speciesOrBenthosBatchUISupport.canPsionImport();
201     }
202 
203     public boolean canBigfinImport() {
204         return speciesOrBenthosBatchUISupport.canBigfinImport();
205     }
206 
207     public Float getTotalComputedWeight() {
208         return speciesOrBenthosBatchUISupport.getTotalComputedWeight();
209     }
210 
211     public void setTotalComputedWeight(Float totalComputedWeight) {
212         speciesOrBenthosBatchUISupport.setTotalComputedWeight(totalComputedWeight);
213     }
214 
215     public ComputableData<Float> getTotalSortedComputedOrNotWeight() {
216         return speciesOrBenthosBatchUISupport.getTotalSortedComputedOrNotWeight();
217     }
218 
219     public Float getTotalSortedWeight() {
220         return speciesOrBenthosBatchUISupport.getTotalSortedWeight();
221     }
222 
223     public void setTotalSortedWeight(Float totalSortedWeight) {
224         speciesOrBenthosBatchUISupport.setTotalSortedWeight(totalSortedWeight);
225     }
226 
227     public Float getTotalSortedComputedWeight() {
228         return speciesOrBenthosBatchUISupport.getTotalSortedComputedWeight();
229     }
230 
231     public void setTotalSortedComputedWeight(Float totalSortedComputedWeight) {
232         speciesOrBenthosBatchUISupport.setTotalSortedComputedWeight(totalSortedComputedWeight);
233     }
234 
235     public Float getTotalUnsortedComputedWeight() {
236         return speciesOrBenthosBatchUISupport.getTotalUnsortedComputedWeight();
237     }
238 
239     public void setTotalUnsortedComputedWeight(Float totalUnsortedComputedWeight) {
240         speciesOrBenthosBatchUISupport.setTotalUnsortedComputedWeight(totalUnsortedComputedWeight);
241     }
242 
243     public Float getTotalSampleSortedComputedWeight() {
244         return speciesOrBenthosBatchUISupport.getTotalSampleSortedComputedWeight();
245     }
246 
247     public void setTotalSampleSortedComputedWeight(Float totalSampleSortedComputedWeight) {
248         speciesOrBenthosBatchUISupport.setTotalSampleSortedComputedWeight(totalSampleSortedComputedWeight);
249     }
250 
251     public ComputableData<Float> getTotalInertComputedOrNotWeight() {
252         return speciesOrBenthosBatchUISupport.getTotalInertComputedOrNotWeight();
253     }
254 
255     public Float getTotalInertWeight() {
256         return speciesOrBenthosBatchUISupport.getTotalInertWeight();
257     }
258 
259     public void setTotalInertWeight(Float totalInertWeight) {
260         speciesOrBenthosBatchUISupport.setTotalInertWeight(totalInertWeight);
261     }
262 
263     public Float getTotalInertComputedWeight() {
264         return speciesOrBenthosBatchUISupport.getTotalInertComputedWeight();
265     }
266 
267     public void setTotalInertComputedWeight(Float totalInertComputedWeight) {
268         speciesOrBenthosBatchUISupport.setTotalInertComputedWeight(totalInertComputedWeight);
269     }
270 
271     public ComputableData<Float> getTotalLivingNotItemizedComputedOrNotWeight() {
272         return speciesOrBenthosBatchUISupport.getTotalLivingNotItemizedComputedOrNotWeight();
273     }
274 
275     public Float getTotalLivingNotItemizedWeight() {
276         return speciesOrBenthosBatchUISupport.getTotalLivingNotItemizedWeight();
277     }
278 
279     public void setTotalLivingNotItemizedWeight(Float totalLivingNotItemizedWeight) {
280         speciesOrBenthosBatchUISupport.setTotalLivingNotItemizedWeight(totalLivingNotItemizedWeight);
281     }
282 
283     public Float getTotalLivingNotItemizedComputedWeight() {
284         return speciesOrBenthosBatchUISupport.getTotalLivingNotItemizedComputedWeight();
285     }
286 
287     public void setTotalLivingNotItemizedComputedWeight(Float totalLivingNotItemizedComputedWeight) {
288         speciesOrBenthosBatchUISupport.setTotalLivingNotItemizedComputedWeight(totalLivingNotItemizedComputedWeight);
289     }
290 
291     public Integer getDistinctSortedSpeciesCount() {
292         return speciesOrBenthosBatchUISupport.getDistinctSortedSpeciesCount();
293     }
294 
295     public void setDistinctSortedSpeciesCount(Integer distinctSortedSpeciesCount) {
296         speciesOrBenthosBatchUISupport.setDistinctSortedSpeciesCount(distinctSortedSpeciesCount);
297     }
298 
299     public Integer getDistinctUnsortedSpeciesCount() {
300         return speciesOrBenthosBatchUISupport.getDistinctUnsortedSpeciesCount();
301     }
302 
303     public void setDistinctUnsortedSpeciesCount(Integer distinctUnsortedSpeciesCount) {
304         speciesOrBenthosBatchUISupport.setDistinctUnsortedSpeciesCount(distinctUnsortedSpeciesCount);
305     }
306 
307     public void incDistinctSortedSpeciesCount() {
308         Integer speciesCount = getDistinctSortedSpeciesCount();
309         if (speciesCount == null) {
310             speciesCount = 0;
311         }
312         setDistinctSortedSpeciesCount(speciesCount + 1);
313     }
314 
315     public void decDistinctSortedSpeciesCount() {
316         Integer speciesCount = getDistinctSortedSpeciesCount();
317         if (speciesCount != null) {
318             setDistinctSortedSpeciesCount(speciesCount - 1);
319         }
320     }
321 
322     public void incDistinctUnsortedSpeciesCount() {
323         Integer speciesCount = getDistinctUnsortedSpeciesCount();
324         if (speciesCount == null) {
325             speciesCount = 0;
326         }
327         setDistinctUnsortedSpeciesCount(speciesCount + 1);
328     }
329 
330     public void decDistinctUnsortedSpeciesCount() {
331         Integer speciesCount = getDistinctUnsortedSpeciesCount();
332         if (speciesCount != null) {
333             setDistinctUnsortedSpeciesCount(speciesCount - 1);
334         }
335     }
336 
337     public TableViewMode getTableViewMode() {
338         return tableViewMode;
339     }
340 
341     public void setTableViewMode(TableViewMode tableViewMode) {
342         Object oldValue = getTableViewMode();
343         this.tableViewMode = tableViewMode;
344         firePropertyChange(PROPERTY_TABLE_VIEW_MODE, oldValue, tableViewMode);
345         firePropertyChange(PROPERTY_TABLE_VIEW_MODE_ALL, null, isTableViewModeAll());
346         firePropertyChange(PROPERTY_TABLE_VIEW_MODE_LEAF, null, isTableViewModeLeaf());
347         firePropertyChange(PROPERTY_TABLE_VIEW_MODE_ROOT, null, isTableViewModeRoot());
348     }
349 
350     public boolean isTableViewModeAll() {
351         return TableViewMode.ALL.equals(tableViewMode);
352     }
353 
354     public boolean isTableViewModeLeaf() {
355         return TableViewMode.LEAF.equals(tableViewMode);
356     }
357 
358     public boolean isTableViewModeRoot() {
359         return TableViewMode.ROOT.equals(tableViewMode);
360     }
361 
362     public Multimap<CaracteristicQualitativeValue, Species> getSpeciesUsed() {
363         return catchesUIModel.getSpeciesUsed();
364     }
365 
366     public int getRootNumber() {
367         return rootNumber;
368     }
369 
370     public void setRootNumber(int rootNumber) {
371         Object oldValue = getRootNumber();
372         this.rootNumber = rootNumber;
373         firePropertyChange(PROPERTY_ROOT_NUMBER, oldValue, rootNumber);
374     }
375 
376     public int getLeafNumber() {
377         return leafNumber;
378     }
379 
380     public void setLeafNumber(int leafNumber) {
381         Object oldValue = getLeafNumber();
382         this.leafNumber = leafNumber;
383         firePropertyChange(PROPERTY_LEAF_NUMBER, oldValue, leafNumber);
384     }
385 
386     public boolean isSplitBatchEnabled() {
387         return splitBatchEnabled;
388     }
389 
390     public void setSplitBatchEnabled(boolean splitBatchEnabled) {
391         this.splitBatchEnabled = splitBatchEnabled;
392         firePropertyChange(PROPERTY_SPLIT_BATCH_ENABLED, null, splitBatchEnabled);
393     }
394 
395     public boolean isChangeSampleCategoryEnabled() {
396         return changeSampleCategoryEnabled;
397     }
398 
399     public void setChangeSampleCategoryEnabled(boolean changeSampleCategoryEnabled) {
400         this.changeSampleCategoryEnabled = changeSampleCategoryEnabled;
401         firePropertyChange(PROPERTY_CHANGE_SAMPLE_CATEGORY_ENABLED, null, changeSampleCategoryEnabled);
402     }
403 
404     public boolean isAddSampleCategoryEnabled() {
405         return addSampleCategoryEnabled;
406     }
407 
408     public void setAddSampleCategoryEnabled(boolean addSampleCategoryEnabled) {
409         this.addSampleCategoryEnabled = addSampleCategoryEnabled;
410         firePropertyChange(PROPERTY_ADD_SAMPLE_CATEGORY_ENABLED, null, addSampleCategoryEnabled);
411     }
412 
413     public boolean isRemoveBatchEnabled() {
414         return removeBatchEnabled;
415     }
416 
417     public void setRemoveBatchEnabled(boolean removeBatchEnabled) {
418         this.removeBatchEnabled = removeBatchEnabled;
419         firePropertyChange(PROPERTY_REMOVE_BATCH_ENABLED, null, removeBatchEnabled);
420     }
421 
422     public boolean isRemoveSubBatchEnabled() {
423         return removeSubBatchEnabled;
424     }
425 
426     public void setRemoveSubBatchEnabled(boolean removeSubBatchEnabled) {
427         this.removeSubBatchEnabled = removeSubBatchEnabled;
428         firePropertyChange(PROPERTY_REMOVE_SUB_BATCH_ENABLED, null, removeSubBatchEnabled);
429     }
430 
431     public boolean isRenameBatchEnabled() {
432         return renameBatchEnabled;
433     }
434 
435     public void setRenameBatchEnabled(boolean renameBatchEnabled) {
436         this.renameBatchEnabled = renameBatchEnabled;
437         firePropertyChange(PROPERTY_RENAME_BATCH_ENABLED, null, renameBatchEnabled);
438     }
439 
440     public boolean isCreateMelagEnabled() {
441         return createMelagEnabled;
442     }
443 
444     public void setCreateMelagEnabled(boolean createMelagEnabled) {
445         this.createMelagEnabled = createMelagEnabled;
446         firePropertyChange(PROPERTY_CREATE_MELAG_ENABLED, null, createMelagEnabled);
447     }
448 
449     public boolean isEditFrequenciesEnabled() {
450         return editFrequenciesEnabled;
451     }
452 
453     public void setEditFrequenciesEnabled(boolean editFrequenciesEnabled) {
454         this.editFrequenciesEnabled = editFrequenciesEnabled;
455         firePropertyChange(PROPERTY_EDIT_FREQUENCIES_ENABLED, null, editFrequenciesEnabled);
456     }
457 
458     //------------------------------------------------------------------------//
459     //-- TabContentModel                                                    --//
460     //------------------------------------------------------------------------//
461 
462     @Override
463     public boolean isEmpty() {
464         return CollectionUtils.isEmpty(getRows())
465                 && getTotalSortedWeight() == null
466                 && getTotalInertWeight() == null
467                 && getTotalLivingNotItemizedWeight() == null;
468     }
469 
470     @Override
471     public String getTitle() {
472         return speciesOrBenthosBatchUISupport.getTitle();
473     }
474 
475     @Override
476     public String getIcon() {
477         return null;
478     }
479 
480     @Override
481     public boolean isCloseable() {
482         return false;
483     }
484 
485     public List<SpeciesBatchRowModel> getLeafs(Species species) {
486         List<SpeciesBatchRowModel> result = Lists.newArrayList();
487         for (SpeciesBatchRowModel rowModel : getRows()) {
488             if (rowModel.isBatchRoot()) {
489                 getLeafs(species, rowModel, result);
490             }
491         }
492         return result;
493     }
494 
495     public void getLeafs(Species species, SpeciesBatchRowModel rowModel, List<SpeciesBatchRowModel> used) {
496 
497         if (rowModel.isBatchLeaf()) {
498 
499             if (species.equals(rowModel.getSpecies())) {
500                 used.add(rowModel);
501             }
502         } else {
503             for (SpeciesBatchRowModel child : rowModel.getChildBatch()) {
504                 getLeafs(species, child, used);
505             }
506         }
507     }
508 
509     public Map<Species, Integer> getSpeciesCount() {
510         Map<Species, Integer> result = Maps.newHashMap();
511         for (SpeciesBatchRowModel row : getRows()) {
512             if (row.isBatchLeaf()) {
513 
514                 Species species = row.getSpecies();
515                 Integer count =
516                         Numbers.getValueOrComputedValue(result.get(species),
517                                                         0);
518 
519                 List<SpeciesFrequencyRowModel> frequencies = row.getFrequency();
520                 if (CollectionUtils.isEmpty(frequencies)) {
521 
522                     //
523                     Integer number = row.getNumber();
524                     if (number != null) {
525                         count += number;
526                     }
527                 } else {
528                     for (SpeciesFrequencyRowModel frequency : frequencies) {
529                         Integer number = frequency.getNumber();
530                         if (number != null) {
531                             count += number;
532                         }
533                     }
534                 }
535                 result.put(species, count);
536             }
537         }
538         return result;
539     }
540 
541     //------------------------------------------------------------------------//
542     //-- SpeciesSortableRowModel                                            --//
543     //------------------------------------------------------------------------//
544 
545     @Override
546     public SpeciesSortMode getSpeciesSortMode() {
547         return speciesSortMode;
548     }
549 
550     @Override
551     public void setSpeciesSortMode(SpeciesSortMode speciesSortMode) {
552         Object oldValue = getSpeciesSortMode();
553         this.speciesSortMode = speciesSortMode;
554         firePropertyChange(PROPERTY_SPECIES_SORT_MODE, oldValue, speciesSortMode);
555         firePropertyChange(PROPERTY_SPECIES_SORT_MODE_NONE, null, isSpeciesSortModeNone());
556         firePropertyChange(PROPERTY_SPECIES_SORT_MODE_ASC, null, isSpeciesSortModeAsc());
557         firePropertyChange(PROPERTY_SPECIES_SORT_MODE_DESC, null, isSpeciesSortModeDesc());
558     }
559 
560     @Override
561     public boolean isSpeciesSortModeNone() {
562         return SpeciesSortMode.NONE.equals(speciesSortMode);
563     }
564 
565     @Override
566     public boolean isSpeciesSortModeAsc() {
567         return SpeciesSortMode.ASC.equals(speciesSortMode);
568     }
569 
570     @Override
571     public boolean isSpeciesSortModeDesc() {
572         return SpeciesSortMode.DESC.equals(speciesSortMode);
573     }
574 
575     @Override
576     public int getSpeciesDecoratorContextIndex() {
577         return speciesDecoratorContextIndex;
578     }
579 
580     @Override
581     public void setSpeciesDecoratorContextIndex(int speciesDecoratorContextIndex) {
582         int oldValue = getSpeciesDecoratorContextIndex();
583         this.speciesDecoratorContextIndex = speciesDecoratorContextIndex;
584         firePropertyChange(PROPERTY_SPECIES_DECORATOR_CONTEXT_INDEX, oldValue, speciesDecoratorContextIndex);
585     }
586 }