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.PersistenceService;
28  import fr.ifremer.tutti.ui.swing.content.home.SelectCruiseUI;
29  import fr.ifremer.tutti.ui.swing.content.home.SelectCruiseUIHandler;
30  import fr.ifremer.tutti.ui.swing.content.home.SelectCruiseUIModel;
31  import fr.ifremer.tutti.ui.swing.util.actions.LongActionSupport;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  
35  import java.util.List;
36  
37  import static org.nuiton.i18n.I18n.t;
38  
39  /**
40   * To delete the selected protocol.
41   *
42   * @author Tony Chemit - chemit@codelutin.com
43   * @since 1.0
44   */
45  public class DeleteProtocolAction extends LongActionSupport<SelectCruiseUIModel, SelectCruiseUI, SelectCruiseUIHandler> {
46  
47      /** Logger. */
48      private static final Log log = LogFactory.getLog(EditProtocolAction.class);
49  
50      public DeleteProtocolAction(SelectCruiseUIHandler handler) {
51          super(handler, false);
52      }
53  
54      @Override
55      public boolean prepareAction() throws Exception {
56          boolean result = super.prepareAction();
57  
58          TuttiProtocol protocol = getHandler().getModel().getProtocol();
59  
60          result &=
61                  askBeforeDelete(
62                          t("tutti.selectCruise.action.deleteProtocol.title"),
63                          t("tutti.selectCruise.action.deleteProtocol.message", protocol.getName())
64                  );
65  
66          return result;
67      }
68  
69      @Override
70      public void doAction() {
71          TuttiProtocol protocol = getModel().getProtocol();
72          Preconditions.checkNotNull(protocol);
73          String id = protocol.getId();
74          Preconditions.checkNotNull(id);
75          if (log.isInfoEnabled()) {
76              log.info("Delete protocol: " + id);
77          }
78  
79          PersistenceService service = getContext().getPersistenceService();
80          service.deleteProtocol(id);
81  
82          getModel().setProtocol(null);
83  
84          List<TuttiProtocol> protocols = getModel().getProtocols();
85          protocols.remove(protocol);
86  
87          if (log.isInfoEnabled()) {
88              log.info("nb protocols: " + protocols.size());
89          }
90          // reset (will clear combo-box)
91          getModel().setProtocols(null);
92  
93          // set new list
94          getModel().setProtocols(protocols);
95  
96      }
97  
98      @Override
99      public void postSuccessAction() {
100 
101         getHandler().resetComboBoxAction(getUI().getEditProtocolComboBox());
102         super.postSuccessAction();
103 
104     }
105 }