1 package fr.ifremer.tutti.ui.swing.content.operation.catches.marinelitter.actions;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
45
46
47
48
49 public class RemoveMarineLitterBatchAction extends LongActionSupport<MarineLitterBatchUIModel, MarineLitterBatchUI, MarineLitterBatchUIHandler> {
50
51
52 private static final Log log =
53 LogFactory.getLog(RemoveMarineLitterBatchAction.class);
54
55
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
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
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 }