View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.operation.catches.marinelitter.actions;
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 com.google.common.base.Preconditions;
26  import fr.ifremer.tutti.persistence.TuttiPersistence;
27  import fr.ifremer.tutti.persistence.entities.TuttiEntities;
28  import fr.ifremer.tutti.ui.swing.content.operation.catches.marinelitter.MarineLitterBatchRowModel;
29  import fr.ifremer.tutti.ui.swing.content.operation.catches.marinelitter.MarineLitterBatchTableModel;
30  import fr.ifremer.tutti.ui.swing.content.operation.catches.marinelitter.MarineLitterBatchUI;
31  import fr.ifremer.tutti.ui.swing.content.operation.catches.marinelitter.MarineLitterBatchUIHandler;
32  import fr.ifremer.tutti.ui.swing.content.operation.catches.marinelitter.MarineLitterBatchUIModel;
33  import fr.ifremer.tutti.ui.swing.util.TuttiUIUtil;
34  import fr.ifremer.tutti.ui.swing.util.actions.LongActionSupport;
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  import org.jdesktop.swingx.JXTable;
38  
39  import javax.swing.JOptionPane;
40  
41  import static org.nuiton.i18n.I18n.t;
42  
43  /**
44   * To remove a selected marine litter batch in the table.
45   *
46   * @author Tony Chemit - chemit@codelutin.com
47   * @since 1.3
48   */
49  public class RemoveMarineLitterBatchAction extends LongActionSupport<MarineLitterBatchUIModel, MarineLitterBatchUI, MarineLitterBatchUIHandler> {
50  
51      /** Logger. */
52      private static final Log log =
53              LogFactory.getLog(RemoveMarineLitterBatchAction.class);
54  
55  //    protected RemoveSpeciesSubBatchAction removeSpeciesSubBatchAction;
56  
57      public RemoveMarineLitterBatchAction(MarineLitterBatchUIHandler handler) {
58          super(handler, false);
59      }
60  
61      int rowIndex;
62  
63      @Override
64      public boolean prepareAction() throws Exception {
65          boolean result = super.prepareAction();
66  
67          if (result) {
68              int answer = JOptionPane.showConfirmDialog(getContext().getActionUI(),
69                                                         t("tutti.editMarineLitterBatch.action.removeBatch.confirm.message"),
70                                                         t("tutti.editMarineLitterBatch.action.removeBatch.confirm.title"),
71                                                         JOptionPane.YES_NO_OPTION);
72              result = answer == JOptionPane.YES_OPTION;
73          }
74  
75          return result;
76      }
77  
78      @Override
79      public void doAction() throws Exception {
80  
81          JXTable table = handler.getTable();
82  
83          rowIndex = table.getSelectedRow();
84  
85          Preconditions.checkState(rowIndex != -1,
86                                   "Cant remove batch if none is selected");
87  
88          MarineLitterBatchTableModel tableModel = handler.getTableModel();
89          MarineLitterBatchRowModel selectedBatch = tableModel.getEntry(rowIndex);
90  
91          boolean persisted = !TuttiEntities.isNew(selectedBatch);
92  
93          if (persisted) {
94  
95              // remove it from db
96  
97              Integer id = selectedBatch.getIdAsInt();
98  
99              if (log.isInfoEnabled()) {
100                 log.info("Remove marineLitter with id: " + id);
101             }
102 
103             TuttiPersistence persistenceService =
104                     getContext().getPersistenceService();
105 
106             persistenceService.deleteMarineLitterBatch(id);
107         }
108 
109         // update speciesUsed
110         handler.removeFromMarineLitterCategoriesUsed(selectedBatch);
111     }
112 
113     @Override
114     public void postSuccessAction() {
115         super.postSuccessAction();
116 
117         JXTable table = handler.getTable();
118 
119         MarineLitterBatchTableModel tableModel = handler.getTableModel();
120 
121         tableModel.removeRow(rowIndex);
122 
123         TuttiUIUtil.selectFirstCellOnFirstRowAndStopEditing(table);
124     }
125 }