View Javadoc
1   package fr.ifremer.dali.ui.swing.content.extraction.list;
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 fr.ifremer.dali.dto.system.extraction.ExtractionDTO;
27  import fr.ifremer.dali.ui.swing.action.AbstractDaliAction;
28  
29  import javax.swing.*;
30  
31  import static org.nuiton.i18n.I18n.t;
32  
33  /**
34   * Duplicate Extraction Action
35   */
36  public class DuplicateExtractionAction extends AbstractDaliAction<ExtractionsTableUIModel, ExtractionsTableUI, ExtractionsTableUIHandler> {
37  
38      private ExtractionDTO duplicateExtraction;
39  
40      /**
41       * Constructor.
42       *
43       * @param handler the handler
44       */
45      public DuplicateExtractionAction(ExtractionsTableUIHandler handler) {
46          super(handler, false);
47      }
48  
49      /**
50       * {@inheritDoc}
51       */
52      @Override
53      public boolean prepareAction() throws Exception {
54          if (!super.prepareAction() || getModel().getSelectedRows().size() != 1) {
55              return false;
56          }
57  
58          return getContext().getDialogHelper().showConfirmDialog(
59                  getUI(),
60                  t("dali.action.duplicate.extraction.message"),
61                  t("dali.action.duplicate.extraction.title"),
62                  JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION;
63      }
64  
65      /**
66       * {@inheritDoc}
67       */
68      @Override
69      public void doAction() throws Exception {
70  
71          // Selected observation
72          final ExtractionsRowModel extractionsRowModel = getModel().getSelectedRows().iterator().next();
73          if (extractionsRowModel != null) {
74  
75              // Duplicate observation
76              duplicateExtraction = getContext().getExtractionService().duplicateExtraction(extractionsRowModel.toBean());
77          }
78      }
79  
80      /**
81       * {@inheritDoc}
82       */
83      @Override
84      public void postSuccessAction() {
85          super.postSuccessAction();
86  
87          getModel().setModify(true);
88  
89          // Add focus on duplicate row
90          getHandler().setFocusOnCell(getModel().addNewRow(duplicateExtraction));
91      }
92  }