View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.operation.catches.species.split;
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.ui.swing.content.operation.catches.species.SelectedCategoryAble;
26  import org.jdesktop.swingx.table.TableColumnModelExt;
27  import org.nuiton.jaxx.application.swing.table.AbstractApplicationTableModel;
28  import org.nuiton.jaxx.application.swing.table.ColumnIdentifier;
29  
30  import java.util.List;
31  import java.util.Optional;
32  
33  import static org.nuiton.i18n.I18n.n;
34  
35  /**
36   * Table model of sample categories values.
37   *
38   * @author Tony Chemit - chemit@codelutin.com
39   * @since 0.3
40   */
41  public class SplitSpeciesBatchTableModel
42          extends AbstractApplicationTableModel<SplitSpeciesBatchRowModel> {
43  
44      private static final long serialVersionUID = 1L;
45  
46      public static final ColumnIdentifier<SplitSpeciesBatchRowModel> SELECTED = ColumnIdentifier.newId(
47              SplitSpeciesBatchRowModel.PROPERTY_SELECTED,
48              n("tutti.splitSpeciesBatch.table.header.selected"),
49              n("tutti.splitSpeciesBatch.table.header.selected"));
50  
51      public static final ColumnIdentifier<SplitSpeciesBatchRowModel> EDITABLE_CATEGORY_VALUE = ColumnIdentifier.newId(
52              SplitSpeciesBatchRowModel.PROPERTY_CATEGORY_VALUE,
53              n("tutti.splitSpeciesBatch.table.header.category"),
54              n("tutti.splitSpeciesBatch.table.header.category"));
55  
56      public static final ColumnIdentifier<SplitSpeciesBatchRowModel> READ_ONLY_CATEGORY_VALUE = ColumnIdentifier.newId(
57              SplitSpeciesBatchRowModel.PROPERTY_CATEGORY_VALUE,
58              n("tutti.splitSpeciesBatch.table.header.category"),
59              n("tutti.splitSpeciesBatch.table.header.category"));
60  
61      public static final ColumnIdentifier<SplitSpeciesBatchRowModel> WEIGHT = ColumnIdentifier.newId(
62              SplitSpeciesBatchRowModel.PROPERTY_WEIGHT,
63              n("tutti.splitSpeciesBatch.table.header.weight"),
64              n("tutti.splitSpeciesBatch.table.header.weight"));
65  
66      private final SelectedCategoryAble uiModel;
67  
68      /**
69       * Is the table is in split mode?
70       *
71       * @since 2.6
72       */
73      private final boolean splitMode;
74  
75      public SplitSpeciesBatchTableModel(TableColumnModelExt columnModel,
76                                         SelectedCategoryAble uiModel,
77                                         boolean createEmptyRowIsEmpty,
78                                         boolean splitMode) {
79          super(columnModel, createEmptyRowIsEmpty, createEmptyRowIsEmpty);
80          this.uiModel = uiModel;
81          this.splitMode = splitMode;
82          setNoneEditableCols(READ_ONLY_CATEGORY_VALUE);
83      }
84  
85      @Override
86      public SplitSpeciesBatchRowModel createNewRow() {
87          SplitSpeciesBatchRowModel result = new SplitSpeciesBatchRowModel();
88          result.setCategoryType(uiModel.getSelectedCategory());
89          result.setValid(false);
90          result.setEditable(true);
91          return result;
92      }
93  
94      @Override
95      protected boolean isCellEditable(int rowIndex,
96                                       int columnIndex,
97                                       ColumnIdentifier<SplitSpeciesBatchRowModel> propertyName) {
98  
99          boolean result = super.isCellEditable(rowIndex,
100                                               columnIndex,
101                                               propertyName);
102         if (result && !splitMode) {
103             // if editable and on edit mode, use editable property on row
104             SplitSpeciesBatchRowModel entry = getEntry(rowIndex);
105             result = entry.isEditable();
106         }
107         return result;
108     }
109 
110     public Optional<Float> getTotalWeight() {
111 
112         boolean found = false;
113         float result = 0f;
114         List<SplitSpeciesBatchRowModel> rows = getRows();
115         for (SplitSpeciesBatchRowModel row : rows) {
116             if (row.isSelected()) {
117                 Float weight = row.getWeight();
118                 if (weight != null) {
119                     found = true;
120                     result += weight;
121                 }
122             }
123         }
124         return Optional.ofNullable(found ? result : null);
125         
126     }
127 }