View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.protocol.actions;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2014 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.base.Preconditions;
28  import com.google.common.collect.Sets;
29  import fr.ifremer.tutti.persistence.entities.referential.Caracteristic;
30  import fr.ifremer.tutti.ui.swing.util.actions.LongActionSupport;
31  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolCaracteristicsRowModel;
32  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolCaracteristicsTableModel;
33  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolUI;
34  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolUIHandler;
35  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolUIModel;
36  import jaxx.runtime.SwingUtil;
37  
38  import javax.swing.JTable;
39  import java.util.ArrayList;
40  import java.util.Collections;
41  import java.util.List;
42  import java.util.Set;
43  
44  import static org.nuiton.i18n.I18n.t;
45  
46  /**
47   * @author Kevin Morin (Code Lutin)
48   * @since 3.10
49   */
50  public class RemoveCaracteristicMappingAction extends LongActionSupport<EditProtocolUIModel, EditProtocolUI, EditProtocolUIHandler> {
51  
52  
53      /**
54       * Set of removed caracteristics.
55       */
56      protected Set<Caracteristic> removedCarateristics;
57  
58      /**
59       * Set of removed rows.
60       */
61      protected Set<EditProtocolCaracteristicsRowModel> removedRows;
62  
63      protected List<Integer> removedRowIndexes;
64  
65  
66      public RemoveCaracteristicMappingAction(EditProtocolUIHandler handler) {
67          super(handler, false);
68      }
69  
70      @Override
71      public void doAction() throws Exception {
72  
73          JTable table = handler.getCaracteristicsMappingTable();
74  
75          // need to have a selection
76          Preconditions.checkState(!table.getSelectionModel().isSelectionEmpty());
77  
78          EditProtocolCaracteristicsTableModel tableModel =
79                  (EditProtocolCaracteristicsTableModel) table.getModel();
80  
81          removedCarateristics = Sets.newHashSet();
82          removedRows = Sets.newHashSet();
83          removedRowIndexes = new ArrayList<>();
84  
85          for (Integer rowIndex : SwingUtil.getSelectedModelRows(table)) {
86  
87              removedRowIndexes.add(rowIndex);
88  
89              // get row to remove
90              EditProtocolCaracteristicsRowModel selectedRow =
91                      tableModel.getEntry(rowIndex);
92  
93              // re-add all synonym of this taxon to the species / benthos combobox
94              Caracteristic caracteristic = selectedRow.getPsfm();
95              removedCarateristics.add(caracteristic);
96  
97              // mark row to be removed at the very last moment
98              removedRows.add(selectedRow);
99          }
100 
101         Collections.sort(removedRowIndexes, Collections.reverseOrder());
102     }
103 
104     @Override
105     public void postSuccessAction() {
106         super.postSuccessAction();
107 
108         // update comboboxes
109         getUI().getCaracteristicMappingComboBox().addItems(removedCarateristics);
110 
111         // remove all rows from model
112         getModel().removeCaracteristicMappingRows(removedRows);
113 
114         // fire table data changed
115         JTable table = handler.getCaracteristicsMappingTable();
116         EditProtocolCaracteristicsTableModel tableModel =
117                 (EditProtocolCaracteristicsTableModel) table.getModel();
118 
119         removedRowIndexes.forEach(tableModel::removeRow);
120 
121         // clear table selection
122         table.clearSelection();
123 
124         // notify user
125         sendMessage(t("tutti.flash.info.caracteristicMapping.remove.from.protocol"));
126     }
127 }