View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.protocol.calcifiedpiecessampling;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2016 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import com.google.common.collect.Lists;
28  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolSpeciesRowModel;
29  import org.jdesktop.swingx.table.TableColumnModelExt;
30  import org.nuiton.jaxx.application.swing.table.AbstractApplicationTableModel;
31  import org.nuiton.jaxx.application.swing.table.ColumnIdentifier;
32  
33  import java.util.LinkedHashSet;
34  import java.util.List;
35  import java.util.Objects;
36  import java.util.Set;
37  
38  import static org.nuiton.i18n.I18n.n;
39  
40  /**
41   * @author Kevin Morin (Code Lutin)
42   * @since 4.5
43   */
44  public class CalcifiedPiecesSamplingEditorTableModel extends AbstractApplicationTableModel<CalcifiedPiecesSamplingEditorRowModel> {
45  
46      public static final ColumnIdentifier<CalcifiedPiecesSamplingEditorRowModel> SPECIES = ColumnIdentifier.newId(
47              CalcifiedPiecesSamplingEditorRowModel.PROPERTY_PROTOCOL_SPECIES,
48              n("tutti.editCps.table.header.species.field"),
49              n("tutti.editCps.table.header.species.field.tip"));
50  
51      public static final ColumnIdentifier<CalcifiedPiecesSamplingEditorRowModel> MATURITY = ColumnIdentifier.newId(
52              CalcifiedPiecesSamplingEditorRowModel.PROPERTY_MATURITY,
53              n("tutti.editCps.table.header.maturity.field"),
54              n("tutti.editCps.table.header.maturity.field.tip"));
55  
56      public static final ColumnIdentifier<CalcifiedPiecesSamplingEditorRowModel> SEX = ColumnIdentifier.newId(
57              CalcifiedPiecesSamplingEditorRowModel.PROPERTY_SEX,
58              n("tutti.editCps.table.header.sex.field"),
59              n("tutti.editCps.table.header.sex.field.tip"));
60  
61      public static final ColumnIdentifier<CalcifiedPiecesSamplingEditorRowModel> MIN_SIZE = ColumnIdentifier.newId(
62              CalcifiedPiecesSamplingEditorRowModel.PROPERTY_MIN_SIZE,
63              n("tutti.editCps.table.header.minSize.field"),
64              n("tutti.editCps.table.header.minSize.field.tip"));
65  
66      public static final ColumnIdentifier<CalcifiedPiecesSamplingEditorRowModel> MAX_SIZE = ColumnIdentifier.newId(
67              CalcifiedPiecesSamplingEditorRowModel.PROPERTY_MAX_SIZE,
68              n("tutti.editCps.table.header.maxSize.field"),
69              n("tutti.editCps.table.header.maxSize.field.tip"));
70  
71      public static final ColumnIdentifier<CalcifiedPiecesSamplingEditorRowModel> MAX_BY_LENGHT_STEP = ColumnIdentifier.newId(
72              CalcifiedPiecesSamplingEditorRowModel.PROPERTY_MAX_BY_LENGHT_STEP,
73              n("tutti.editCps.table.header.maxByLengthStep.field"),
74              n("tutti.editCps.table.header.maxByLengthStep.field.tip"));
75  
76      public static final ColumnIdentifier<CalcifiedPiecesSamplingEditorRowModel> SAMPLING_INTERVAL = ColumnIdentifier.newId(
77              CalcifiedPiecesSamplingEditorRowModel.PROPERTY_SAMPLING_INTERVAL,
78              n("tutti.editCps.table.header.samplingInterval.field"),
79              n("tutti.editCps.table.header.samplingInterval.field.tip"));
80  
81      public static final ColumnIdentifier<CalcifiedPiecesSamplingEditorRowModel> OPERATION_LIMITATION = ColumnIdentifier.newId(
82              CalcifiedPiecesSamplingEditorRowModel.PROPERTY_OPERATION_LIMITATION,
83              n("tutti.editCps.table.header.operationLimitation.field"),
84              n("tutti.editCps.table.header.operationLimitation.field.tip"));
85  
86      public static final ColumnIdentifier<CalcifiedPiecesSamplingEditorRowModel> ZONE_LIMITATION = ColumnIdentifier.newId(
87              CalcifiedPiecesSamplingEditorRowModel.PROPERTY_ZONE_LIMITATION,
88              n("tutti.editCps.table.header.zoneLimitation.field"),
89              n("tutti.editCps.table.header.zoneLimitation.field.tip"));
90  
91  
92      public CalcifiedPiecesSamplingEditorTableModel(TableColumnModelExt columnModel) {
93          super(columnModel, false, false);
94          setNoneEditableCols(SPECIES, MAX_SIZE, MATURITY);
95      }
96  
97      @Override
98      public CalcifiedPiecesSamplingEditorRowModel createNewRow() {
99          return new CalcifiedPiecesSamplingEditorRowModel();
100     }
101 
102     public CalcifiedPiecesSamplingEditorRowModel createNewRow(EditProtocolSpeciesRowModel species,
103                                                               Boolean maturity,
104                                                               boolean sex) {
105         return createNewRow(species, maturity,sex, 0, null);
106     }
107 
108     public CalcifiedPiecesSamplingEditorRowModel createNewRow(EditProtocolSpeciesRowModel species,
109                                                               Boolean maturity,
110                                                               boolean sex,
111                                                               Integer minSize,
112                                                               Integer maxSize) {
113 
114         CalcifiedPiecesSamplingEditorRowModel newRow = createNewRow();
115         newRow.setValid(true);
116         newRow.setProtocolSpecies(species);
117         newRow.setMaturity(maturity);
118         newRow.setSex(sex);
119         newRow.setMinSize(minSize);
120         newRow.setMaxSize(maxSize);
121         newRow.setMaxByLenghtStep(0);
122         newRow.setSamplingInterval(0);
123         newRow.setOperationLimitation(0);
124         newRow.setZoneLimitation(0);
125 
126         return newRow;
127     }
128 
129     /**
130      * Return the list of used protocolSpecies in the table (used to fill the
131      * comparator cache for protocolSpecies sort)
132      *
133      * @return the list of used protocolSpecies in the table.
134      * @since 2.8
135      */
136     public List<EditProtocolSpeciesRowModel> getSpeciesList() {
137         List<EditProtocolSpeciesRowModel> result = Lists.newArrayList();
138         for (CalcifiedPiecesSamplingEditorRowModel row : rows) {
139             result.add(row.getProtocolSpecies());
140         }
141         return result;
142     }
143 
144     @Override
145     protected boolean isCellEditable(int rowIndex,
146                                      int columnIndex,
147                                      ColumnIdentifier<CalcifiedPiecesSamplingEditorRowModel> propertyName) {
148         boolean result = super.isCellEditable(rowIndex, columnIndex, propertyName);
149 
150         if (result && MIN_SIZE.equals(propertyName)) {
151             int minSize = (int) getValueAt(rowIndex, columnIndex);
152             result = minSize > 0;
153         }
154         return result;
155     }
156 
157     public boolean isSpeciesOrderEven(int rowIndex) {
158 
159         CalcifiedPiecesSamplingEditorRowModel selectedRow = getEntry(rowIndex);
160         Integer selectedSpeciesReferenceTaxonId = selectedRow.getProtocolSpecies().getSpeciesReferenceTaxonId();
161 
162         Set<Integer> speciesReferenceTaxonIds = new LinkedHashSet<>();
163         for (CalcifiedPiecesSamplingEditorRowModel row : rows) {
164             Integer speciesReferenceTaxonId = row.getProtocolSpecies().getSpeciesReferenceTaxonId();
165             speciesReferenceTaxonIds.add(speciesReferenceTaxonId);
166             if (Objects.equals(selectedSpeciesReferenceTaxonId, speciesReferenceTaxonId)) {
167                 break;
168             }
169         }
170         int nbSpecies = speciesReferenceTaxonIds.size();
171 
172         // Si on a trouvé des espèces, on doit alors incrémenter de 1 car l'espèce «0» est en fait la première trouvée ^^
173         return nbSpecies == 0 || ((nbSpecies + 1) % 2 == 0);
174 
175     }
176 
177 }