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.attachment.AttachmentEditorUI;
29  import fr.ifremer.tutti.ui.swing.util.attachment.AttachmentItem;
30  import fr.ifremer.tutti.ui.swing.util.attachment.AttachmentItemModel;
31  import fr.ifremer.tutti.ui.swing.util.attachment.AttachmentModelAware;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  
35  import javax.swing.JOptionPane;
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 RemoveAttachmentAction extends SimpleActionSupport<AttachmentItem> {
46  
47      /** Logger. */
48      private static final Log log = LogFactory.getLog(RemoveAttachmentAction.class);
49  
50      private static final long serialVersionUID = 1L;
51  
52      public RemoveAttachmentAction(AttachmentItem ui) {
53          super(ui);
54      }
55  
56      @Override
57      protected void onActionPerformed(AttachmentItem ui) {
58  
59          AttachmentEditorUI upperUI = ui.getParentContainer(AttachmentEditorUI.class);
60  
61          AttachmentItemModel model = ui.getModel();
62          boolean hackDialog = upperUI.isAlwaysOnTop();
63          if (hackDialog) {
64              upperUI.setAlwaysOnTop(false);
65          }
66          int answer = JOptionPane.showConfirmDialog(upperUI,
67                                                     t("tutti.attachmentEditor.deleteAttachment.message", model.getName()),
68                                                     t("tutti.attachmentEditor.deleteAttachment.title"),
69                                                     JOptionPane.YES_NO_OPTION);
70          if (hackDialog) {
71              upperUI.setAlwaysOnTop(true);
72          }
73  
74          if (answer == JOptionPane.YES_OPTION) {
75              AttachmentModelAware bean = upperUI.getBean();
76  
77              if (log.isInfoEnabled()) {
78                  log.info("Remove attachment: " + model.getName());
79              }
80  
81              // si la pj n'est pas persisté, pas besoin de la supprimer, juste l'enlever des pj à enregistrer
82              if (!model.isCreate()) {
83                  ui.getHandler().getPersistenceService().deleteAttachment(model.getId());
84              }
85              bean.removeAttachment(model.toEntity());
86              upperUI.getAttachments().remove(ui);
87  
88              upperUI.pack();
89          }
90  
91      }
92  
93  }