1 package fr.ifremer.tutti.ui.swing.util.attachment.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
26
27 import fr.ifremer.tutti.ui.swing.util.actions.SimpleActionSupport;
28 import fr.ifremer.tutti.ui.swing.util.attachment.AttachmentEditorUI;
29 import fr.ifremer.tutti.ui.swing.util.attachment.AttachmentItem;
30 import fr.ifremer.tutti.ui.swing.util.attachment.AttachmentItemModel;
31 import fr.ifremer.tutti.ui.swing.util.attachment.AttachmentModelAware;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35 import javax.swing.JOptionPane;
36
37 import static org.nuiton.i18n.I18n.t;
38
39
40
41
42
43
44
45 public class RemoveAttachmentAction extends SimpleActionSupport<AttachmentItem> {
46
47
48 private static final Log log = LogFactory.getLog(RemoveAttachmentAction.class);
49
50 private static final long serialVersionUID = 1L;
51
52 public RemoveAttachmentAction(AttachmentItem ui) {
53 super(ui);
54 }
55
56 @Override
57 protected void onActionPerformed(AttachmentItem ui) {
58
59 AttachmentEditorUI upperUI = ui.getParentContainer(AttachmentEditorUI.class);
60
61 AttachmentItemModel model = ui.getModel();
62 boolean hackDialog = upperUI.isAlwaysOnTop();
63 if (hackDialog) {
64 upperUI.setAlwaysOnTop(false);
65 }
66 int answer = JOptionPane.showConfirmDialog(upperUI,
67 t("tutti.attachmentEditor.deleteAttachment.message", model.getName()),
68 t("tutti.attachmentEditor.deleteAttachment.title"),
69 JOptionPane.YES_NO_OPTION);
70 if (hackDialog) {
71 upperUI.setAlwaysOnTop(true);
72 }
73
74 if (answer == JOptionPane.YES_OPTION) {
75 AttachmentModelAware bean = upperUI.getBean();
76
77 if (log.isInfoEnabled()) {
78 log.info("Remove attachment: " + model.getName());
79 }
80
81
82 if (!model.isCreate()) {
83 ui.getHandler().getPersistenceService().deleteAttachment(model.getId());
84 }
85 bean.removeAttachment(model.toEntity());
86 upperUI.getAttachments().remove(ui);
87
88 upperUI.pack();
89 }
90
91 }
92
93 }