View Javadoc
1   package fr.ifremer.dali.ui.swing.content.manage.context.contextslist;
2   
3   /*
4    * #%L
5    * Dali :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (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 Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import com.google.common.collect.Lists;
27  import fr.ifremer.dali.dto.configuration.context.ContextDTO;
28  import fr.ifremer.dali.ui.swing.action.AbstractDaliAction;
29  import fr.ifremer.dali.ui.swing.content.manage.context.ManageContextsUI;
30  
31  import java.util.List;
32  
33  import static org.nuiton.i18n.I18n.t;
34  
35  /**
36   * <p>DeleteContextAction class.</p>
37   *
38   */
39  public class DeleteContextAction extends AbstractDaliAction<ManageContextsListTableUIModel, ManageContextsListTableUI, ManageContextsListTableUIHandler> {
40  
41      private List<ContextDTO> contextsToDelete;
42      private boolean activeContextIsDeleted;
43  
44      /**
45       * <p>Constructor for DeleteContextAction.</p>
46       *
47       * @param handler a {@link ManageContextsListTableUIHandler} object.
48       */
49      public DeleteContextAction(final ManageContextsListTableUIHandler handler) {
50          super(handler, false);
51      }
52  
53      /** {@inheritDoc} */
54      @Override
55      public boolean prepareAction() throws Exception {
56          boolean canContinue = super.prepareAction() && getModel().getSelectedRows().size() > 0;
57  
58          if (canContinue) {
59  
60              contextsToDelete = Lists.newArrayList();
61              for (ManageContextsListTableUIRowModel selectedRow: getModel().getSelectedRows()) {
62                  contextsToDelete.add(selectedRow.toBean());
63              }
64  
65              // detect if active context is selected
66              activeContextIsDeleted = false;
67              if (getContext().getSelectedContext() != null) {
68                  activeContextIsDeleted = contextsToDelete.contains(getContext().getSelectedContext());
69              }
70  
71              if (activeContextIsDeleted) {
72                  canContinue = askBeforeDelete(t("dali.action.delete.confirm.title"), t("dali.action.delete.context.active.message"));
73              } else {
74                  canContinue = askBeforeDelete(t("dali.action.delete.confirm.title"), t("dali.action.delete.context.message"));
75              }
76          }
77  
78          return canContinue;
79      }
80  
81      /** {@inheritDoc} */
82      @Override
83      public void doAction() throws Exception {
84  
85          getContext().getContextService().deleteContexts(contextsToDelete);
86  
87      }
88  
89      /** {@inheritDoc} */
90      @Override
91      public void postSuccessAction() {
92  
93          getModel().deleteSelectedRows();
94  
95          // if active context has been deleted, reset it
96          if (activeContextIsDeleted) {
97              getContext().setSelectedContext(null);
98          }
99  
100         ManageContextsUI manageContextsUI = getHandler().getParentContainer(ManageContextsUI.class);
101         if (manageContextsUI != null) {
102             manageContextsUI.getManageContextsListMenuUI().getHandler().reloadComboBox();
103         }
104 
105         super.postSuccessAction();
106     }
107 }