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  
36  import javax.swing.JOptionPane;
37  import java.util.List;
38  
39  import static org.nuiton.i18n.I18n.t;
40  
41  /**
42   * @author Kevin Morin (Code Lutin)
43   * @since 4.5
44   */
45  public class DeleteRowAction extends SimpleActionSupport<CalcifiedPiecesSamplingEditorUI> {
46  
47      public DeleteRowAction(CalcifiedPiecesSamplingEditorUI ui) {
48          super(ui);
49      }
50  
51      @Override
52      protected void onActionPerformed(CalcifiedPiecesSamplingEditorUI ui) {
53  
54          JXTable cpsTable = ui.getCpsTable();
55          CalcifiedPiecesSamplingEditorTableModel tableModel = (CalcifiedPiecesSamplingEditorTableModel) cpsTable.getModel();
56  
57          int selectedRow = cpsTable.getSelectedRow();
58  
59          List<CalcifiedPiecesSamplingEditorRowModel> cspRows = ui.getModel().getCpsRows();
60  
61          CalcifiedPiecesSamplingEditorRowModel row = cspRows.get(selectedRow);
62  
63          if (row.getMinSize() > 0) {
64              EditProtocolSpeciesRowModel speciesToDelete = row.getProtocolSpecies();
65  
66              String decoratedRow = ui.getHandler().getDecorator(Species.class, null).toString(speciesToDelete.getSpecies());
67              if (row.getMaturity() != null) {
68                  decoratedRow += " (" + ui.getHandler().getDecorator(Boolean.class, DecoratorService.MATURITY).toString(row.getMaturity()) + ")";
69              }
70              decoratedRow += " [" + row.getMinSize() + ", "
71                              + ui.getHandler().getDecorator(Integer.class, DecoratorService.NULL_INFINITE).toString(row.getMaxSize()) + "]";
72  
73              int confirmDeletion = JOptionPane.showConfirmDialog(ui,
74                                                                  t("tutti.editCps.deleteRow.message", decoratedRow),
75                                                                  t("tutti.editCps.deleteRow.title"),
76                                                                  JOptionPane.YES_NO_OPTION,
77                                                                  JOptionPane.QUESTION_MESSAGE);
78  
79              if (confirmDeletion == JOptionPane.YES_OPTION) {
80  
81                  // merge the row with the previous one
82                  CalcifiedPiecesSamplingEditorRowModel previousRow = cspRows.get(selectedRow - 1);
83                  previousRow.setMaxSize(row.getMaxSize());
84  
85                  cspRows.remove(row);
86                  tableModel.fireTableRowsDeleted(selectedRow, selectedRow);
87              }
88          }
89      }
90  
91  }