View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.protocol.calcifiedpiecessampling.actions;
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 fr.ifremer.tutti.persistence.entities.referential.Species;
28  import fr.ifremer.tutti.service.DecoratorService;
29  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolSpeciesRowModel;
30  import fr.ifremer.tutti.ui.swing.content.protocol.calcifiedpiecessampling.CalcifiedPiecesSamplingEditorRowModel;
31  import fr.ifremer.tutti.ui.swing.content.protocol.calcifiedpiecessampling.CalcifiedPiecesSamplingEditorTableModel;
32  import fr.ifremer.tutti.ui.swing.content.protocol.calcifiedpiecessampling.CalcifiedPiecesSamplingEditorUI;
33  import fr.ifremer.tutti.ui.swing.util.actions.SimpleActionSupport;
34  import org.jdesktop.swingx.JXTable;
35  import org.nuiton.decorator.Decorator;
36  
37  import javax.swing.JOptionPane;
38  import java.util.LinkedHashSet;
39  import java.util.List;
40  import java.util.Set;
41  import java.util.TreeSet;
42  import java.util.stream.Collectors;
43  
44  import static org.nuiton.i18n.I18n.t;
45  
46  /**
47   * @author Kevin Morin (Code Lutin)
48   * @since 4.5
49   */
50  public class DeleteSpeciesAction extends SimpleActionSupport<CalcifiedPiecesSamplingEditorUI> {
51  
52      public DeleteSpeciesAction(CalcifiedPiecesSamplingEditorUI ui) {
53          super(ui);
54      }
55  
56      @Override
57      protected void onActionPerformed(CalcifiedPiecesSamplingEditorUI ui) {
58  
59          JXTable cpsTable = ui.getCpsTable();
60          CalcifiedPiecesSamplingEditorTableModel tableModel = (CalcifiedPiecesSamplingEditorTableModel) cpsTable.getModel();
61  
62          int[] selectedRows = cpsTable.getSelectedRows();
63  
64          List<CalcifiedPiecesSamplingEditorRowModel> cpsRows = ui.getModel().getCpsRows();
65  
66          Set<Species> speciesToDelete = new LinkedHashSet<>();
67          for (int selectedRow : selectedRows) {
68  
69              CalcifiedPiecesSamplingEditorRowModel row = cpsRows.get(selectedRow);
70              EditProtocolSpeciesRowModel speciesRowToDelete = row.getProtocolSpecies();
71              speciesToDelete.add(speciesRowToDelete.getSpecies());
72  
73          }
74  
75          int confirmDeletion;
76  
77          Decorator<Species> speciesDecorator = ui.getHandler().getDecorator(Species.class, DecoratorService.WITH_SURVEY_CODE);
78  
79          if (speciesToDelete.size() == 1) {
80  
81              confirmDeletion = JOptionPane.showConfirmDialog(ui,
82                                                              t("tutti.editCps.deleteOneSpecies.message", speciesDecorator.toString(speciesToDelete.iterator().next())),
83                                                              t("tutti.editCps.deleteOneSpecies.title"),
84                                                              JOptionPane.YES_NO_OPTION,
85                                                              JOptionPane.QUESTION_MESSAGE);
86          } else {
87  
88              StringBuilder builder = new StringBuilder();
89              for (Species species : speciesToDelete) {
90                  builder.append("<li>").append(speciesDecorator.toString(species)).append("</li>");
91              }
92              confirmDeletion = JOptionPane.showConfirmDialog(ui,
93                                                              t("tutti.editCps.deleteMoreThanOneSpecies.message", builder.toString()),
94                                                              t("tutti.editCps.deleteMoreThanOneSpecies.title"),
95                                                              JOptionPane.YES_NO_OPTION,
96                                                              JOptionPane.QUESTION_MESSAGE);
97  
98          }
99  
100         if (confirmDeletion == JOptionPane.YES_OPTION) {
101 
102             for (Species species : speciesToDelete) {
103 
104                 List<CalcifiedPiecesSamplingEditorRowModel> rowsToDelete
105                         = cpsRows
106                         .stream()
107                         .filter(r -> r.getProtocolSpecies().getSpecies().equals(species))
108                         .collect(Collectors.toList());
109 
110                 TreeSet<Integer> indexesToDelete =
111                         new TreeSet<>(rowsToDelete.stream().map(cpsRows::indexOf).collect(Collectors.toSet()));
112 
113                 cpsRows.removeAll(rowsToDelete);
114 
115                 tableModel.fireTableRowsDeleted(indexesToDelete.first(), indexesToDelete.last());
116 
117                 ui.getSpeciesComboBox().addItem(species);
118 
119             }
120 
121         }
122 
123     }
124 
125 }