1 package fr.ifremer.reefdb.ui.swing.content.manage.campaign;
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.quadrige3.synchro.vo.SynchroProgressionStatus;
27 import fr.ifremer.quadrige3.ui.swing.synchro.action.ImportSynchroCheckAction;
28 import fr.ifremer.reefdb.dto.data.survey.CampaignDTO;
29 import fr.ifremer.reefdb.ui.swing.action.QuitScreenAction;
30 import fr.ifremer.reefdb.ui.swing.content.manage.campaign.menu.CampaignsMenuUIModel;
31 import fr.ifremer.reefdb.ui.swing.util.AbstractReefDbBeanUIModel;
32 import fr.ifremer.reefdb.ui.swing.util.AbstractReefDbUIHandler;
33 import jaxx.runtime.SwingUtil;
34 import jaxx.runtime.validator.swing.SwingValidator;
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.nuiton.jaxx.application.swing.util.CloseableUI;
38
39 import javax.swing.SwingUtilities;
40 import java.util.Collection;
41
42 import static org.nuiton.i18n.I18n.t;
43
44
45
46
47 public class CampaignsUIHandler extends AbstractReefDbUIHandler<CampaignsUIModel, CampaignsUI> implements CloseableUI {
48
49
50
51
52 private static final Log LOG = LogFactory.getLog(CampaignsUIHandler.class);
53 private ImportSynchroCheckAction importSynchroCheckAction;
54
55
56
57
58 @Override
59 public void beforeInit(final CampaignsUI ui) {
60 super.beforeInit(ui);
61
62
63 final CampaignsUIModel model = new CampaignsUIModel();
64 ui.setContextValue(model);
65
66 ui.setContextValue(SwingUtil.createActionIcon("campaign"));
67
68 }
69
70
71
72
73 @Override
74 public void afterInit(final CampaignsUI ui) {
75 initUI(ui);
76
77 ui.getMenuUI().getHandler().enableContextFilter(false);
78
79
80 getModel().setCampaignsTableUIModel(ui.getCampaignsTableUI().getModel());
81
82
83 SwingUtil.setComponentWidth(ui.getLeftImage(), ui.getMenu().getPreferredSize().width * 9 / 10);
84 ui.getLeftImage().setScaled(true);
85
86 initListeners();
87
88 ui.applyDataBinding(CampaignsUI.BINDING_SAVE_BUTTON_ENABLED);
89
90
91 SwingUtilities.invokeLater(this::checkForReferentialUpdates);
92 }
93
94 @SuppressWarnings("unchecked")
95 private void initListeners() {
96
97
98 getUI().getMenuUI().getModel().addPropertyChangeListener(CampaignsMenuUIModel.PROPERTY_RESULTS, evt -> {
99
100
101 getModel().getCampaignsTableUIModel().setBeans((Collection<CampaignDTO>) evt.getNewValue());
102
103 getModel().getCampaignsTableUIModel().getRows().forEach(campaign -> {
104
105
106 campaign.setEditable(getModel().isSaveEnabled());
107
108
109 campaign.setReadOnly(!(campaign.getManager().getId().equals(getContext().getDataContext().getRecorderPersonId())
110 || campaign.getRecorderDepartment().getId().equals(getContext().getDataContext().getRecorderDepartmentId())));
111
112 });
113
114 });
115
116
117 getModel().getCampaignsTableUIModel().addPropertyChangeListener(AbstractReefDbBeanUIModel.PROPERTY_VALID, evt -> getValidator().doValidate());
118 listenModelModify(getModel().getCampaignsTableUIModel());
119
120
121 getModel().addPropertyChangeListener(CampaignsUIModel.PROPERTY_SAVE_ENABLED, evt -> getModel().getCampaignsTableUIModel().setSaveEnabled(getModel().isSaveEnabled()));
122
123
124 registerValidators(getValidator());
125 listenValidatorValid(getValidator(), getModel());
126
127 }
128
129
130
131
132 @Override
133 public SwingValidator<CampaignsUIModel> getValidator() {
134 return getUI().getValidator();
135 }
136
137
138
139
140 @Override
141 @SuppressWarnings("unchecked")
142 public boolean quitUI() {
143 try {
144 QuitScreenAction action = new QuitScreenAction(this, false, SaveAction.class);
145 if (action.prepareAction()) {
146 return true;
147 }
148 } catch (Exception e) {
149 LOG.error(e.getLocalizedMessage(), e);
150 }
151 return false;
152 }
153
154 private void checkForReferentialUpdates() {
155 if (getContext().isNextImportSynchroCheckActionPrevented()) {
156 return;
157 }
158 if (getContext().getSynchroContext().isRunningStatus()) {
159 return;
160 }
161 getImportSynchroCheckAction().execute();
162 if (LOG.isDebugEnabled())
163 LOG.debug("checkForReferentialUpdates executed");
164 }
165
166 private ImportSynchroCheckAction getImportSynchroCheckAction() {
167 if (importSynchroCheckAction == null) {
168 importSynchroCheckAction = getContext().getActionFactory().createNonBlockingUIAction(getContext().getSynchroHandler(), ImportSynchroCheckAction.class);
169 }
170 if (!importSynchroCheckAction.isConsumerSet()) {
171 importSynchroCheckAction.setConsumer(synchroUIContext -> {
172
173 if (LOG.isDebugEnabled())
174 LOG.debug("check result: " + synchroUIContext.isImportReferential());
175
176
177 if (synchroUIContext.getStatus() == SynchroProgressionStatus.FAILED) {
178 getModel().setSaveEnabled(false);
179 getContext().getDialogHelper().showWarningDialog(t("reefdb.error.synchro.serverNotYetAvailable"));
180 } else {
181 getModel().setSaveEnabled(true);
182
183 if (synchroUIContext.isImportReferential()) {
184 UpdateCampaignsAction updateCampaignsAction = getContext().getActionFactory().createLogicAction(this, UpdateCampaignsAction.class);
185 getContext().getActionEngine().runAction(updateCampaignsAction);
186 }
187 }
188
189
190 getContext().getSynchroHandler().report(t("quadrige3.synchro.report.idle"), false);
191 getContext().getSynchroContext().resetImportContext();
192 getContext().getSynchroContext().saveImportContext(true, true);
193
194 });
195 }
196 return importSynchroCheckAction;
197 }
198 }