View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.operation.fishing.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.persistence.entities.TuttiEntities;
26  import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
27  import fr.ifremer.tutti.ui.swing.util.actions.LongActionSupport;
28  import fr.ifremer.tutti.ui.swing.content.operation.FishingOperationsUI;
29  import fr.ifremer.tutti.ui.swing.content.operation.FishingOperationsUIHandler;
30  import fr.ifremer.tutti.ui.swing.content.operation.FishingOperationsUIModel;
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  
34  import static org.nuiton.i18n.I18n.t;
35  
36  /**
37   * To delete a fishing operation.
38   *
39   * @author Tony Chemit - chemit@codelutin.com
40   * @since 2.2
41   */
42  public class DeleteFishingOperationAction extends LongActionSupport<FishingOperationsUIModel, FishingOperationsUI, FishingOperationsUIHandler> {
43  
44      /** Logger. */
45      private static final Log log =
46              LogFactory.getLog(DeleteFishingOperationAction.class);
47  
48      public DeleteFishingOperationAction(FishingOperationsUIHandler handler) {
49          super(handler, true);
50          setActionDescription(
51                  t("tutti.editFishingOperation.action.deleteFishingOperation.tip"));
52      }
53  
54      @Override
55      public boolean prepareAction() throws Exception {
56          boolean canContinue = super.prepareAction();
57          FishingOperation fishingOperation = getModel().getSelectedFishingOperation();
58          canContinue &= fishingOperation != null &&
59                         !TuttiEntities.isNew(fishingOperation) &&
60                         askBeforeDelete(
61                                 t("tutti.editFishingOperation.action.deleteFishingOperation.title"),
62                                 t("tutti.editFishingOperation.action.deleteFishingOperation.message", decorate(fishingOperation))
63                         );
64          return canContinue;
65      }
66  
67      @Override
68      public void doAction() throws Exception {
69  
70          FishingOperation fishingOperation =
71                  getModel().getSelectedFishingOperation();
72  
73          if (log.isInfoEnabled()) {
74              log.info("Will delete fishingOperation: " + fishingOperation);
75          }
76  
77          getContext().getPersistenceService().deleteFishingOperation(fishingOperation.getIdAsInt());
78  
79          FishingOperationsUIModel model = getModel();
80  
81          // unselect removed fishing operation
82          model.setSelectedFishingOperation(null);
83  
84          // remove it from selection model
85          model.removeFishingOperation(fishingOperation);
86  
87          sendMessage(t("tutti.editFishingOperation.message.operation.deleted", decorate(fishingOperation)));
88      }
89  }