View Javadoc
1   package fr.ifremer.tutti.ui.swing.util.attachment.actions;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (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 General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import fr.ifremer.tutti.ui.swing.util.actions.SimpleActionSupport;
28  import fr.ifremer.tutti.ui.swing.util.TuttiUIUtil;
29  import fr.ifremer.tutti.ui.swing.util.attachment.AttachmentItem;
30  import fr.ifremer.tutti.ui.swing.util.attachment.AttachmentItemModel;
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  import org.nuiton.jaxx.application.ApplicationTechnicalException;
34  
35  import java.io.File;
36  
37  import static org.nuiton.i18n.I18n.t;
38  
39  /**
40   * Created on 3/7/15.
41   *
42   * @author Tony Chemit - chemit@codelutin.com
43   * @since 3.15
44   */
45  public class OpenAttachmentAction extends SimpleActionSupport<AttachmentItem> {
46  
47      /** Logger. */
48      private static final Log log = LogFactory.getLog(OpenAttachmentAction.class);
49  
50      private static final long serialVersionUID = 1L;
51  
52      public OpenAttachmentAction(AttachmentItem ui) {
53          super(ui);
54      }
55  
56      @Override
57      protected void onActionPerformed(AttachmentItem ui) {
58  
59          AttachmentItemModel model = ui.getModel();
60  
61          File file;
62  
63          if (model.isCreate()) {
64              file = new File(model.getPath());
65  
66          } else {
67              file = ui.getHandler().getPersistenceService().getAttachmentFile(model.getId());
68          }
69  
70          if (!file.exists()) {
71              throw new ApplicationTechnicalException(t("tutti.attachmentEditor.fileNotFound", file.getAbsolutePath()));
72          }
73  
74          if (log.isInfoEnabled()) {
75              log.info("Open attachment: " + file);
76          }
77          TuttiUIUtil.openResource(file);
78  
79      }
80  
81  }