1 package fr.ifremer.reefdb.ui.swing.content.observation.photo;
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 import fr.ifremer.quadrige3.core.dao.technical.Files;
27 import fr.ifremer.quadrige3.ui.swing.ApplicationUIUtil;
28 import fr.ifremer.reefdb.service.ReefDbBusinessException;
29 import fr.ifremer.reefdb.ui.swing.action.AbstractReefDbAction;
30
31 import javax.swing.JEditorPane;
32 import javax.swing.JOptionPane;
33 import javax.swing.event.HyperlinkEvent;
34 import java.io.File;
35 import java.net.URL;
36 import java.nio.file.Path;
37 import java.nio.file.Paths;
38 import java.util.stream.Collectors;
39
40 import static org.nuiton.i18n.I18n.t;
41
42
43
44
45 public class ExportAction extends AbstractReefDbAction<PhotosTabUIModel, PhotosTabUI, PhotosTabUIHandler> {
46
47 private File destDir;
48
49
50
51
52
53
54 public ExportAction(PhotosTabUIHandler handler) {
55 super(handler, false);
56 }
57
58
59 @Override
60 public boolean prepareAction() throws Exception {
61 if (!super.prepareAction() || getModel().getSelectedRows().isEmpty()) {
62 return false;
63 }
64
65 destDir = chooseDirectory(t("reefdb.action.photo.export.chooseDirectory.title"),
66 t("reefdb.action.photo.export.chooseDirectory.buttonLabel"));
67
68 return destDir != null;
69
70 }
71
72
73 @Override
74 public void doAction() throws Exception {
75
76 for (PhotosTableRowModel photo : getModel().getSelectedRows().stream().filter(PhotosTableRowModel::isFileExists).collect(Collectors.toList())) {
77 Path fileSrc = Paths.get(photo.getFullPath());
78 if (!java.nio.file.Files.isRegularFile(fileSrc)) {
79 throw new ReefDbBusinessException(t("quadrige3.error.file.not.exists"));
80 }
81 Path fileDest = destDir.toPath().resolve(fileSrc.getFileName());
82 Files.copyFile(fileSrc, fileDest);
83 }
84
85 }
86
87
88 @Override
89 public void postSuccessAction() {
90
91 String text = t("reefdb.action.photo.export.done",
92 destDir.getAbsoluteFile().toURI(),
93 destDir.getAbsolutePath()
94 );
95
96 JEditorPane editorPane = new JEditorPane("text/html", text);
97 editorPane.setEditable(false);
98 editorPane.addHyperlinkListener(e -> {
99 if (HyperlinkEvent.EventType.ACTIVATED == e.getEventType()) {
100 URL url = e.getURL();
101 ApplicationUIUtil.openLink(url);
102 }
103 });
104
105 getContext().getDialogHelper().showOptionDialog(
106 getUI(),
107 editorPane,
108 t("reefdb.action.photo.export.title"),
109 JOptionPane.INFORMATION_MESSAGE,
110 JOptionPane.DEFAULT_OPTION
111 );
112
113 super.postSuccessAction();
114 }
115 }