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 import fr.ifremer.tutti.ui.swing.util.actions.LongActionSupport;
26 import fr.ifremer.tutti.ui.swing.util.attachment.AttachmentEditorUI;
27 import fr.ifremer.tutti.ui.swing.util.attachment.AttachmentItem;
28 import fr.ifremer.tutti.ui.swing.util.attachment.AttachmentItemHandler;
29 import fr.ifremer.tutti.ui.swing.util.attachment.AttachmentItemModel;
30 import org.nuiton.jaxx.application.ApplicationIOUtil;
31
32 import java.io.File;
33
34 import static org.nuiton.i18n.I18n.t;
35
36
37
38
39
40
41
42 public class SaveAttachmentAction extends LongActionSupport<AttachmentItemModel, AttachmentItem, AttachmentItemHandler> {
43
44 protected File file;
45
46 protected File attachmentFile;
47
48 public SaveAttachmentAction(AttachmentItemHandler handler) {
49 super(handler, false);
50 }
51
52 @Override
53 public boolean prepareAction() throws Exception {
54 boolean doAction = super.prepareAction();
55 if (doAction) {
56
57 AttachmentItemModel model = getModel();
58
59 if (model.isCreate()) {
60 attachmentFile = new File(model.getPath());
61 } else {
62 attachmentFile = getContext().getPersistenceService().getAttachmentFile(model.getId());
63 }
64
65 file = saveFile(
66 ApplicationIOUtil.getBaseName(model.getName()),
67 ApplicationIOUtil.getExtension(attachmentFile.getName()),
68 t("tutti.attachmentEditor.saveAttachment.title"),
69 t("tutti.attachmentEditor.saveAttachment.button"));
70 doAction = file != null;
71 }
72 return doAction;
73 }
74
75 @Override
76 public void doAction() throws Exception {
77
78 AttachmentEditorUI upperUI = getUI().getParentContainer(AttachmentEditorUI.class);
79
80 boolean hackDialog = upperUI.isAlwaysOnTop();
81 if (hackDialog) {
82 upperUI.setAlwaysOnTop(false);
83 }
84 try {
85
86 ApplicationIOUtil.copyFile(attachmentFile, file,
87 t("tutti.attachmentEditor.saveAttachment.error.message", attachmentFile, file.getName()));
88 sendMessage(
89 t("tutti.attachmentEditor.saveAttachment.success.message", file.getName()));
90
91 } finally {
92 if (hackDialog) {
93 upperUI.setAlwaysOnTop(true);
94 }
95 }
96 }
97
98 @Override
99 public void releaseAction() {
100 super.releaseAction();
101 attachmentFile = null;
102 file = null;
103 }
104 }