View Javadoc
1   package fr.ifremer.tutti.ui.swing.util.attachment.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 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   * To persist a attachment.
38   *
39   * @author Tony Chemit - chemit@codelutin.com
40   * @since 2.4
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 }