View Javadoc
1   package fr.ifremer.reefdb.ui.swing.content.manage.referential.location.local.replace;
2   
3   /*
4    * #%L
5    * Reef DB :: 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 fr.ifremer.reefdb.dto.referential.LocationDTO;
27  import fr.ifremer.reefdb.service.referential.ReferentialService;
28  import fr.ifremer.reefdb.ui.swing.content.manage.referential.location.ManageLocationUI;
29  import fr.ifremer.reefdb.ui.swing.content.manage.referential.replace.AbstractReplaceAction;
30  
31  import javax.swing.*;
32  
33  import static org.nuiton.i18n.I18n.t;
34  
35  /**
36   * Created on 7/6/14.
37   *
38   * @since 3.6
39   */
40  public class ReplaceLocationAction extends AbstractReplaceAction<LocationDTO, ReplaceLocationUIModel, ReplaceLocationUI, ReplaceLocationUIHandler> {
41  
42      /**
43       * <p>Constructor for ReplaceLocationAction.</p>
44       *
45       * @param handler a {@link fr.ifremer.reefdb.ui.swing.content.manage.referential.location.local.replace.ReplaceLocationUIHandler} object.
46       */
47      public ReplaceLocationAction(ReplaceLocationUIHandler handler) {
48          super(handler);
49      }
50  
51      /** {@inheritDoc} */
52      @Override
53      protected String getReferentialLabel() {
54          return t("reefdb.property.location");
55      }
56  
57      /** {@inheritDoc} */
58      @Override
59      protected boolean prepareReplaceReferential(ReferentialService service, LocationDTO source, LocationDTO target) {
60  
61          if (service.isLocationUsedInProgram(source.getId())) {
62              String decoratedSource = decorate(source);
63              if (getContext().getDialogHelper().showConfirmDialog(getReplaceUI(), t("reefdb.replaceReferential.used.programStrategy.message", decoratedSource, decoratedSource),
64                      getReplaceUI().getTitle(), JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
65                  return false;
66              }
67              setDelete(false);
68          }
69  
70          if (service.isLocationUsedInValidatedData(source.getId())) {
71              String decoratedSource = decorate(source);
72              if (getContext().getDialogHelper().showConfirmDialog(getReplaceUI(), t("reefdb.replaceReferential.used.validatedData.message", decoratedSource, decoratedSource),
73                      getReplaceUI().getTitle(), JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
74                  return false;
75              }
76              setDelete(false);
77          }
78  
79          return true;
80      }
81  
82      /** {@inheritDoc} */
83      @Override
84      protected void replaceReferential(ReferentialService service, LocationDTO source, LocationDTO target, boolean delete) {
85  
86          service.replaceLocation(source, target, delete);
87      }
88  
89      /** {@inheritDoc} */
90      @Override
91      protected void resetCaches() {
92      }
93  
94      /** {@inheritDoc} */
95      @Override
96      public void postSuccessAction() {
97          super.postSuccessAction();
98  
99          final ManageLocationUI ui = (ManageLocationUI) getContext().getMainUI().getHandler().getCurrentBody();
100         // reload table
101         if (ui != null) {
102             SwingUtilities.invokeLater(() -> getActionEngine().runAction(ui.getManageLocationLocalUI().getLocationLocalMenuUI().getSearchButton()));
103         }
104 
105     }
106 }