View Javadoc
1   package fr.ifremer.dali.ui.swing.content.manage.rule.rulelist;
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.configuration.control.RuleListDTO;
27  import fr.ifremer.dali.ui.swing.action.AbstractDaliAction;
28  
29  import javax.swing.JCheckBox;
30  import javax.swing.JOptionPane;
31  import javax.swing.JTextField;
32  
33  import static org.nuiton.i18n.I18n.t;
34  
35  /**
36   * Rule list duplicate action
37   */
38  public class DuplicateRuleListAction extends AbstractDaliAction<RuleListUIModel, RuleListUI, RuleListUIHandler> {
39  
40      private String newCode;
41      private boolean duplicateControlRules;
42      private RuleListDTO duplicatedRuleList;
43  
44      /**
45       * Constructor.
46       *
47       * @param handler the handler
48       */
49      public DuplicateRuleListAction(RuleListUIHandler handler) {
50          super(handler, false);
51      }
52  
53      /**
54       * {@inheritDoc}
55       */
56      @Override
57      public boolean prepareAction() throws Exception {
58          if (!super.prepareAction() || getModel().getSelectedRows().size() != 1) {
59              return false;
60          }
61  
62          String title = t("dali.action.duplicate.ruleList.title");
63          String message = t("dali.action.duplicate.ruleList.message");
64          JTextField codeField = new JTextField();
65  
66          JCheckBox checkBox = new JCheckBox(t("dali.action.duplicate.ruleList.controlRules"));
67          Object[] messageObjects = {message, codeField, checkBox};
68  
69          int result = getContext().getDialogHelper().showOptionDialog(getUI(), messageObjects, title, JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
70  
71          if (result == JOptionPane.NO_OPTION || result == JOptionPane.CLOSED_OPTION) {
72              return false;
73          }
74  
75          duplicateControlRules = checkBox.isSelected();
76          newCode = codeField.getText();
77  
78          // check code duplicates
79          return getHandler().checkCodeDuplicates(newCode);
80      }
81  
82      /**
83       * {@inheritDoc}
84       */
85      @Override
86      public void doAction() throws Exception {
87  
88          RuleListRowModel selectedRuleList = getModel().getSelectedRows().iterator().next();
89          if (selectedRuleList != null) {
90  
91              duplicatedRuleList = getContext().getRuleListService().duplicateRuleList(selectedRuleList.toBean(), newCode, duplicateControlRules);
92              duplicatedRuleList.setDirty(true);
93          }
94      }
95  
96      /**
97       * {@inheritDoc}
98       */
99      @Override
100     public void postSuccessAction() {
101         super.postSuccessAction();
102 
103         getModel().setModify(true);
104 
105         getHandler().setFocusOnCell(getModel().addNewRow(duplicatedRuleList));
106     }
107 }