View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.protocol;
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.referential.Species;
27  import org.jdesktop.swingx.table.TableColumnModelExt;
28  import org.nuiton.jaxx.application.swing.table.AbstractApplicationTableModel;
29  import org.nuiton.jaxx.application.swing.table.ColumnIdentifier;
30  
31  import java.util.List;
32  import java.util.function.Supplier;
33  
34  import static org.nuiton.i18n.I18n.n;
35  
36  /**
37   * @author kmorin
38   * @since 0.3
39   */
40  public class EditProtocolSpeciesTableModel extends AbstractApplicationTableModel<EditProtocolSpeciesRowModel> {
41  
42      public static final ColumnIdentifier<EditProtocolSpeciesRowModel> SPECIES_ID = ColumnIdentifier.newId(
43              EditProtocolSpeciesRowModel.PROPERTY_SPECIES,
44              n("tutti.editProtocol.table.header.speciesReferenceTaxonId"),
45              n("tutti.editProtocol.table.header.speciesReferenceTaxonId.tip"));
46  
47      public static final ColumnIdentifier<EditProtocolSpeciesRowModel> SURVEY_CODE_ID = ColumnIdentifier.newId(
48              EditProtocolSpeciesRowModel.PROPERTY_SPECIES_SURVEY_CODE,
49              n("tutti.editProtocol.table.header.speciesSurveyCode"),
50              n("tutti.editProtocol.table.header.speciesSurveyCode.tip"));
51  
52      public static final ColumnIdentifier<EditProtocolSpeciesRowModel> LENGTH_STEP_PMFM_ID = ColumnIdentifier.newId(
53              EditProtocolSpeciesRowModel.PROPERTY_LENGTH_STEP_PMFM,
54              n("tutti.editProtocol.table.header.lengthStep"),
55              n("tutti.editProtocol.table.header.lengthStep.tip"));
56  
57      public static final ColumnIdentifier<EditProtocolSpeciesRowModel> WEIGHT_ENABLED = ColumnIdentifier.newId(
58              EditProtocolSpeciesRowModel.PROPERTY_WEIGHT_ENABLED,
59              n("tutti.editProtocol.table.header.weight"),
60              n("tutti.editProtocol.table.header.weight.tip"));
61  
62      public static final ColumnIdentifier<EditProtocolSpeciesRowModel> INDIVIDUAL_OBSERVATION_ENABLED = ColumnIdentifier.newId(
63              EditProtocolSpeciesRowModel.PROPERTY_INDIVIDUAL_OBSERVATION_ENABLED,
64              n("tutti.editProtocol.table.header.individualObservation"),
65              n("tutti.editProtocol.table.header.individualObservation.tip"));
66  
67      public static final ColumnIdentifier<EditProtocolSpeciesRowModel> MATURITY_PMFM_ID = ColumnIdentifier.newId(
68              EditProtocolSpeciesRowModel.PROPERTY_MATURITY_PMFM,
69              n("tutti.editProtocol.table.header.maturity"),
70              n("tutti.editProtocol.table.header.maturity.tip"));
71  
72      public static final ColumnIdentifier<EditProtocolSpeciesRowModel> CALCIFIED_PIECES_SAMPLING_TYPE_PMFM_ID = ColumnIdentifier.newId(
73              EditProtocolSpeciesRowModel.PROPERTY_CALCIFIED_PIECES_SAMPLING_TYPE,
74              n("tutti.editProtocol.table.header.calcifiedPiecesSamplingType"),
75              n("tutti.editProtocol.table.header.calcifiedPiecesSamplingType.tip"));
76  
77      public static final ColumnIdentifier<EditProtocolSpeciesRowModel> USE_RTP = ColumnIdentifier.newId(
78              EditProtocolSpeciesRowModel.PROPERTY_USE_RTP,
79              n("tutti.editProtocol.table.header.useRtp"),
80              n("tutti.editProtocol.table.header.useRtp.tip"));
81  
82      protected final Supplier<EditProtocolSpeciesRowModel> rowSupplier;
83  
84      private static final long serialVersionUID = 1L;
85  
86      public EditProtocolSpeciesTableModel(Supplier<EditProtocolSpeciesRowModel> rowSupplier,
87                                           TableColumnModelExt columnModel) {
88          super(columnModel, false, false);
89          this.rowSupplier = rowSupplier;
90          setNoneEditableCols(SPECIES_ID);
91  
92      }
93  
94      @Override
95      public EditProtocolSpeciesRowModel createNewRow() {
96          return rowSupplier.get();
97      }
98  
99      @Override
100     protected void setValueAt(Object aValue,
101                               int rowIndex,
102                               int columnIndex,
103                               ColumnIdentifier<EditProtocolSpeciesRowModel> propertyName,
104                               EditProtocolSpeciesRowModel entry) {
105         super.setValueAt(aValue, rowIndex, columnIndex, propertyName, entry);
106         fireTableCellUpdated(rowIndex, columnIndex);
107     }
108 
109     @Override
110     protected boolean isCellEditable(int rowIndex,
111                                      int columnIndex,
112                                      ColumnIdentifier<EditProtocolSpeciesRowModel> propertyName) {
113 
114         boolean result = super.isCellEditable(rowIndex,
115                                               columnIndex,
116                                               propertyName);
117         if (result) {
118 
119             if (USE_RTP.equals(propertyName) || INDIVIDUAL_OBSERVATION_ENABLED.equals(propertyName)) {
120 
121                 // must have filled a species to edit this column
122                 EditProtocolSpeciesRowModel entry = getEntry(rowIndex);
123                 result = entry.withLengthStepPmfm();
124 
125             }
126         }
127         return result;
128     }
129 
130     /**
131      * Return the list of used species in the table (used to fill the
132      * comparator cache for species sort)
133      *
134      * @return the list of used species in the table.
135      * @since 2.8
136      */
137     public List<Species> getSpeciesList() {
138         List<Species> result = Lists.newArrayList();
139         for (EditProtocolSpeciesRowModel row : rows) {
140             result.add(row.getSpecies());
141         }
142         return result;
143     }
144 
145 }