View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.home.actions;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (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 General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import com.google.common.base.Preconditions;
26  import fr.ifremer.tutti.persistence.entities.protocol.TuttiProtocol;
27  import fr.ifremer.tutti.service.protocol.ProtocolImportExportService;
28  import fr.ifremer.tutti.ui.swing.util.actions.LongActionSupport;
29  import fr.ifremer.tutti.ui.swing.content.home.SelectCruiseUI;
30  import fr.ifremer.tutti.ui.swing.content.home.SelectCruiseUIHandler;
31  import fr.ifremer.tutti.ui.swing.content.home.SelectCruiseUIModel;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  
35  import java.io.File;
36  
37  import static org.nuiton.i18n.I18n.t;
38  
39  /**
40   * Opens a file chooser and exports the protocol into the selected file.
41   *
42   * @author Tony Chemit - chemit@codelutin.com
43   * @since 1.0
44   */
45  public class ExportProtocolAction extends LongActionSupport<SelectCruiseUIModel, SelectCruiseUI, SelectCruiseUIHandler> {
46  
47      /** Logger. */
48      private static final Log log =
49              LogFactory.getLog(ExportProtocolAction.class);
50  
51      protected File file;
52  
53      public ExportProtocolAction(SelectCruiseUIHandler handler) {
54          super(handler, true);
55      }
56  
57      @Override
58      public boolean prepareAction() throws Exception {
59  
60          boolean doAction = super.prepareAction();
61  
62          if (doAction) {
63  
64              TuttiProtocol protocol = getModel().getProtocol();
65              // choose file to export
66              file = saveFile(
67                      "protocol-" + protocol.getName().replaceAll(" ", "_"),
68                      "tuttiProtocol",
69                      t("tutti.selectCruise.title.choose.exportProtocolFile"),
70                      t("tutti.selectCruise.action.exportProtocol"),
71                      "^.+\\.tuttiProtocol$", t("tutti.common.file.protocol")
72              );
73              doAction = file != null;
74          }
75          return doAction;
76      }
77  
78      @Override
79      public void releaseAction() {
80          file = null;
81          super.releaseAction();
82      }
83  
84      @Override
85      public void doAction() throws Exception {
86          TuttiProtocol protocol = getModel().getProtocol();
87          Preconditions.checkNotNull(protocol);
88          Preconditions.checkNotNull(file);
89  
90          if (log.isInfoEnabled()) {
91              log.info("Will save protocol " + protocol.getId() +
92                       " to file: " + file);
93          }
94  
95          // export protocol
96          ProtocolImportExportService service =
97                  getContext().getTuttiProtocolImportExportService();
98          service.exportProtocol(protocol, file);
99  
100     }
101 
102     @Override
103     public void postSuccessAction() {
104 
105         getHandler().resetComboBoxAction(getUI().getEditProtocolComboBox());
106 
107         super.postSuccessAction();
108         TuttiProtocol protocol = getModel().getProtocol();
109         sendMessage(t("tutti.exportProtocol.action.success", protocol.getName(), file.getName()));
110 
111     }
112 }