View Javadoc
1   package fr.ifremer.dali.ui.swing.content.extraction.list;
2   
3   /*-
4    * #%L
5    * Dali :: UI
6    * %%
7    * Copyright (C) 2017 - 2018 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Affero General Public License as published by
11   * the Free Software Foundation, either version 3 of the License, or
12   * (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU Affero General Public License
20   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21   * #L%
22   */
23  
24  import fr.ifremer.dali.ui.swing.action.AbstractDaliAction;
25  
26  import java.io.File;
27  
28  import static org.nuiton.i18n.I18n.t;
29  
30  /**
31   * @author peck7 on 22/01/2018.
32   */
33  public class ExportExtractionAction extends AbstractDaliAction<ExtractionsTableUIModel, ExtractionsTableUI, ExtractionsTableUIHandler> {
34  
35      private File exportFile;
36  
37      /**
38       * <p>Constructor for ExportExtractionAction.</p>
39       *
40       * @param handler a H object.
41       */
42      public ExportExtractionAction(ExtractionsTableUIHandler handler) {
43          super(handler, false);
44      }
45  
46      @Override
47      public boolean prepareAction() throws Exception {
48          if (!super.prepareAction()) return false;
49  
50          if (getModel().getSingleSelectedRow() == null) {
51              return false;
52          }
53  
54          if (getModel().getSingleSelectedRow().isDirty()) {
55              getContext().getDialogHelper().showWarningDialog(
56                      t("dali.extraction.list.export.dirty.message"),
57                      t("dali.extraction.list.export.title")
58              );
59              return false;
60          }
61  
62          String fileName = String.format("%s-%s", t("dali.service.extraction.file.prefix"), getModel().getSingleSelectedRow().getName());
63          String fileExtension = getConfig().getExtractionFileExtension();
64          exportFile = saveFile(
65                  fileName,
66                  fileExtension,
67                  t("dali.extraction.list.export.title"),
68                  t("dali.common.export"),
69                  "^.*\\." + fileExtension,
70                  t("dali.common.file." + fileExtension)
71          );
72  
73          return exportFile != null;
74      }
75  
76      @Override
77      public void doAction() throws Exception {
78  
79          getContext().getExtractionService().exportExtraction(getModel().getSingleSelectedRow(), exportFile);
80      }
81  
82      @Override
83      protected void releaseAction() {
84          super.releaseAction();
85  
86          exportFile = null;
87      }
88  }