1 package fr.ifremer.dali.ui.swing.content.manage.rule.rulelist;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
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
46
47
48
49 public DuplicateRuleListAction(RuleListUIHandler handler) {
50 super(handler, false);
51 }
52
53
54
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
79 return getHandler().checkCodeDuplicates(newCode);
80 }
81
82
83
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
98
99 @Override
100 public void postSuccessAction() {
101 super.postSuccessAction();
102
103 getModel().setModify(true);
104
105 getHandler().setFocusOnCell(getModel().addNewRow(duplicatedRuleList));
106 }
107 }