1 package fr.ifremer.tutti.ui.swing.content.operation.catches.accidental.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.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
45
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
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 }