View Javadoc
1   package fr.ifremer.reefdb.ui.swing.content.manage.referential.unit.local;
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 com.google.common.collect.Lists;
27  import fr.ifremer.reefdb.decorator.DecoratorService;
28  import fr.ifremer.reefdb.dto.referential.UnitDTO;
29  import fr.ifremer.reefdb.service.StatusFilter;
30  import fr.ifremer.reefdb.service.referential.ReferentialService;
31  import fr.ifremer.reefdb.ui.swing.action.AbstractReefDbAction;
32  import fr.ifremer.reefdb.ui.swing.content.manage.referential.replace.AbstractOpenReplaceUIAction;
33  import fr.ifremer.reefdb.ui.swing.content.manage.referential.unit.ReferentialUnitsUI;
34  import fr.ifremer.reefdb.ui.swing.content.manage.referential.unit.local.replace.ReplaceUnitUI;
35  import fr.ifremer.reefdb.ui.swing.content.manage.referential.unit.local.replace.ReplaceUnitUIModel;
36  import jaxx.runtime.context.JAXXInitialContext;
37  
38  import java.util.List;
39  
40  import static org.nuiton.i18n.I18n.t;
41  
42  /**
43   * Created on 7/6/14.
44   */
45  public class OpenReplaceUnitAction extends AbstractReefDbAction<ReferentialUnitsLocalUIModel, ReferentialUnitsLocalUI, ReferentialUnitsLocalUIHandler> {
46  
47      /**
48       * <p>Constructor for OpenReplaceUnitAction.</p>
49       *
50       * @param handler a {@link fr.ifremer.reefdb.ui.swing.content.manage.referential.unit.local.ReferentialUnitsLocalUIHandler} object.
51       */
52      public OpenReplaceUnitAction(ReferentialUnitsLocalUIHandler handler) {
53          super(handler, false);
54      }
55  
56      /** {@inheritDoc} */
57      @Override
58      public void doAction() {
59  
60          AbstractOpenReplaceUIAction<UnitDTO, ReplaceUnitUIModel, ReplaceUnitUI> openAction =
61                  new AbstractOpenReplaceUIAction<UnitDTO, ReplaceUnitUIModel, ReplaceUnitUI>(getContext().getMainUI().getHandler()) {
62  
63                      @Override
64                      protected String getEntityLabel() {
65                          return t("reefdb.property.pmfm.unit");
66                      }
67  
68                      @Override
69                      protected ReplaceUnitUIModel createNewModel() {
70                          ReplaceUnitUIModel model = new ReplaceUnitUIModel();
71                          model.setDecoratorContext(DecoratorService.WITH_SYMBOL);
72                          return model;
73                      }
74  
75                      @Override
76                      protected ReplaceUnitUI createUI(JAXXInitialContext ctx) {
77                          return new ReplaceUnitUI(ctx);
78                      }
79  
80                      @Override
81                      protected List<UnitDTO> getReferentialList(ReferentialService referentialService) {
82                          return Lists.newArrayList(referentialService.getUnits(StatusFilter.ACTIVE));
83                      }
84  
85                      @Override
86                      protected UnitDTO getSelectedSource() {
87                          List<UnitDTO> selectedBeans = OpenReplaceUnitAction.this.getModel().getSelectedBeans();
88                          return selectedBeans.size() == 1 ? selectedBeans.get(0) : null;
89                      }
90  
91                      @Override
92                      protected UnitDTO getSelectedTarget() {
93                          ReferentialUnitsUI ui = OpenReplaceUnitAction.this.getUI().getParentContainer(ReferentialUnitsUI.class);
94                          List<UnitDTO> selectedBeans = ui.getReferentialUnitsNationalUI().getModel().getSelectedBeans();
95                          return selectedBeans.size() == 1 ? selectedBeans.get(0) : null;
96                      }
97                  };
98  
99          getActionEngine().runFullInternalAction(openAction);
100     }
101 }