View Javadoc
1   package fr.ifremer.dali.ui.swing.content.extraction.list;
2   
3   /*
4    * #%L
5    * Dali :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (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 Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import fr.ifremer.dali.dto.DaliBeans;
27  import fr.ifremer.dali.dto.referential.GroupingTypeDTO;
28  import fr.ifremer.dali.dto.system.extraction.ExtractionDTO;
29  import fr.ifremer.dali.ui.swing.util.table.AbstractDaliTableModel;
30  import fr.ifremer.dali.ui.swing.util.table.AbstractDaliTableUIHandler;
31  import fr.ifremer.quadrige3.ui.swing.table.SwingTable;
32  import org.apache.commons.collections4.CollectionUtils;
33  import org.apache.commons.lang3.StringUtils;
34  import org.jdesktop.swingx.table.TableColumnExt;
35  
36  import javax.swing.BorderFactory;
37  import javax.swing.SortOrder;
38  import java.util.List;
39  
40  import static org.nuiton.i18n.I18n.t;
41  
42  /**
43   * Controller pour le tableau des observations.
44   */
45  public class ExtractionsTableUIHandler extends
46          AbstractDaliTableUIHandler<ExtractionsRowModel, ExtractionsTableUIModel, ExtractionsTableUI> {
47  
48      /** {@inheritDoc} */
49      @Override
50      protected String[] getRowPropertiesToIgnore() {
51          return new String[]{
52                  ExtractionsRowModel.PROPERTY_DIRTY,
53                  ExtractionsRowModel.PROPERTY_ERRORS
54          };
55      }
56  
57      /** {@inheritDoc} */
58      @Override
59      public void beforeInit(final ExtractionsTableUI ui) {
60          super.beforeInit(ui);
61  
62          // create model and register to the JAXX context
63          final ExtractionsTableUIModel model = new ExtractionsTableUIModel();
64          ui.setContextValue(model);
65      }
66  
67      /** {@inheritDoc} */
68      @Override
69      public void afterInit(final ExtractionsTableUI ui) {
70  
71          // Initialisation de l ecran
72          initUI(ui);
73  
74          // Initialisation du tableau
75          initTable();
76  
77          // Initialisation de la combobox Extraction
78          initActionComboBox(getUI().getExtractCombo());
79  
80          // button border
81          getUI().getExtractCombo().setBorder(
82                  BorderFactory.createCompoundBorder(
83                          BorderFactory.createMatteBorder(2, 2, 2, 2, getConfig().getColorHighlightButtonBorder()), ui.getExtractCombo().getBorder())
84          );
85  
86      }
87  
88      /**
89       * Initialisation du tableau.
90       */
91      private void initTable() {
92  
93          // Le tableau
94          final SwingTable table = getTable();
95  
96          // name
97          TableColumnExt nameCol = addColumn(ExtractionsTableModel.NAME);
98          nameCol.setSortable(true);
99  
100         // grouping type
101         addFilterableComboDataColumnToModel(
102                 ExtractionsTableModel.GROUPING_TYPE, getContext().getReferentialService().getGroupingTypes(), false);
103 
104         ExtractionsTableModel tableModel = new ExtractionsTableModel(getTable().getColumnModel());
105         table.setModel(tableModel);
106 
107         // Initialisation du tableau
108         initTable(table);
109 
110         table.setVisibleRowCount(8);
111 
112         // Tri par defaut
113         table.setSortOrder(ExtractionsTableModel.NAME, SortOrder.ASCENDING);
114 
115         // border
116         addEditionPanelBorder();
117 
118     }
119 
120     /** {@inheritDoc} */
121     @Override
122     protected boolean isRowValid(ExtractionsRowModel row) {
123         return super.isRowValid(row) && isExtractionValid(row);
124     }
125 
126     private boolean isExtractionValid(ExtractionsRowModel row) {
127 
128         row.getErrors().clear();
129 
130         if (!row.isFiltersValid()) {
131             DaliBeans.addError(row, t("dali.extraction.list.filter.invalid"), ExtractionsRowModel.PROPERTY_NAME);
132         }
133 
134         // search for name duplicates
135         if (StringUtils.isNoneBlank(row.getName()) && row.isDirty()) {
136             boolean duplicateFound = false;
137 
138             for (ExtractionsRowModel otherRow : getModel().getRows()) {
139                 if (row == otherRow) continue;
140                 if (row.getName().equalsIgnoreCase(otherRow.getName())) {
141                     DaliBeans.addError(row, t("dali.error.alreadyExists.label.ui", row.getName()), ExtractionsRowModel.PROPERTY_NAME);
142                     duplicateFound = true;
143                     break;
144                 }
145             }
146 
147             if (!duplicateFound) {
148                 List<ExtractionDTO> allExtractions = getContext().getExtractionService().getAllLightExtractions();
149                 if (CollectionUtils.isNotEmpty(allExtractions)) {
150                     for (ExtractionDTO extraction : allExtractions) {
151                         if (!extraction.getId().equals(row.getId()) && row.getName().equalsIgnoreCase(extraction.getName())) {
152                             DaliBeans.addError(row, t("dali.error.alreadyExists.label.db", row.getName()), ExtractionsRowModel.PROPERTY_NAME);
153                             break;
154                         }
155                     }
156                 }
157             }
158         }
159 
160         return row.isErrorsEmpty();
161     }
162 
163     /** {@inheritDoc} */
164     @Override
165     protected void onRowModified(int rowIndex, ExtractionsRowModel row, String propertyName, Integer propertyIndex, Object oldValue, Object newValue) {
166         super.onRowModified(rowIndex, row, propertyName, propertyIndex, oldValue, newValue);
167 
168         row.setDirty(true);
169         recomputeRowValidState(row);
170     }
171 
172     /** {@inheritDoc} */
173     @Override
174     public AbstractDaliTableModel<ExtractionsRowModel> getTableModel() {
175         return (ExtractionsTableModel) getTable().getModel();
176     }
177 
178     /** {@inheritDoc} */
179     @Override
180     public SwingTable getTable() {
181         return ui.getExtractionsTable();
182     }
183 
184     /** {@inheritDoc} */
185     @Override
186     protected void onRowsAdded(List<ExtractionsRowModel> addedRows) {
187         super.onRowsAdded(addedRows);
188 
189         // should be only one row
190         if (addedRows.size() == 1) {
191             ExtractionsRowModel row = addedRows.get(0);
192 
193             // default order item type only if missing (Mantis #52363)
194             if (row.getGroupingType() == null) {
195                 row.setGroupingType(DaliBeans.findByProperty(
196                     getContext().getReferentialService().getGroupingTypes(),
197                     GroupingTypeDTO.PROPERTY_CODE,
198                     getConfig().getExtractionDefaultOrderItemTypeCode()
199                 ));
200             }
201 
202             // Ajouter le focus sur la cellule de la ligne cree
203             setFocusOnCell(row);
204         }
205     }
206 
207 }