View Javadoc
1   package fr.ifremer.reefdb.ui.swing.content.observation.photo;
2   
3   /*
4    * #%L
5    * Reef DB :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
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   * Import action.
44   */
45  public class ExportAction extends AbstractReefDbAction<PhotosTabUIModel, PhotosTabUI, PhotosTabUIHandler> {
46  
47      private File destDir;
48  
49      /**
50       * Constructor.
51       *
52       * @param handler Controller
53       */
54      public ExportAction(PhotosTabUIHandler handler) {
55          super(handler, false);
56      }
57  
58      /** {@inheritDoc} */
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      /** {@inheritDoc} */
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      /** {@inheritDoc} */
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 }