View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.operation.catches.accidental.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.util.actions.LongActionSupport;
29  import fr.ifremer.tutti.ui.swing.content.operation.catches.accidental.AccidentalBatchRowModel;
30  import fr.ifremer.tutti.ui.swing.content.operation.catches.accidental.AccidentalBatchTableModel;
31  import fr.ifremer.tutti.ui.swing.content.operation.catches.accidental.AccidentalBatchUI;
32  import fr.ifremer.tutti.ui.swing.content.operation.catches.accidental.AccidentalBatchUIHandler;
33  import fr.ifremer.tutti.ui.swing.content.operation.catches.accidental.AccidentalBatchUIModel;
34  import fr.ifremer.tutti.ui.swing.util.TuttiUIUtil;
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   * @author Kevin Morin - kmorin@codelutin.com
45   * @since 1.4
46   */
47  public class RemoveAccidentalBatchAction extends LongActionSupport<AccidentalBatchUIModel, AccidentalBatchUI, AccidentalBatchUIHandler> {
48  
49      private static final Log log =
50              LogFactory.getLog(RemoveAccidentalBatchAction.class);
51  
52      public RemoveAccidentalBatchAction(AccidentalBatchUIHandler handler) {
53          super(handler, false);
54      }
55  
56      int rowIndex;
57  
58      @Override
59      public boolean prepareAction() throws Exception {
60          boolean result = super.prepareAction();
61  
62          if (result) {
63              int answer = JOptionPane.showConfirmDialog(getContext().getActionUI(),
64                                                         t("tutti.editAccidentalBatch.action.removeBatch.confirm.message"),
65                                                         t("tutti.editAccidentalBatch.action.removeBatch.confirm.title"),
66                                                         JOptionPane.YES_NO_OPTION);
67              result = answer == JOptionPane.YES_OPTION;
68          }
69  
70          return result;
71      }
72  
73      @Override
74      public void doAction() throws Exception {
75  
76          JXTable table = handler.getTable();
77  
78          rowIndex = table.getSelectedRow();
79  
80          Preconditions.checkState(rowIndex != -1,
81                                   "Cant remove batch if none is selected");
82  
83          AccidentalBatchTableModel tableModel = handler.getTableModel();
84          AccidentalBatchRowModel selectedBatch = tableModel.getEntry(rowIndex);
85  
86          boolean persisted = !TuttiEntities.isNew(selectedBatch);
87  
88          if (persisted) {
89  
90              // remove it from db
91  
92              String id = selectedBatch.getId();
93  
94              if (log.isInfoEnabled()) {
95                  log.info("Remove accidental with id: " + id);
96              }
97  
98              TuttiPersistence persistenceService =
99                      getContext().getPersistenceService();
100 
101             persistenceService.deleteAccidentalBatch(id);
102         }
103     }
104 
105     @Override
106     public void postSuccessAction() {
107         super.postSuccessAction();
108 
109         JXTable table = handler.getTable();
110 
111         AccidentalBatchTableModel tableModel = handler.getTableModel();
112 
113         tableModel.removeRow(rowIndex);
114 
115         TuttiUIUtil.selectFirstCellOnFirstRowAndStopEditing(table);
116     }
117 }