1 package fr.ifremer.reefdb.ui.swing.content.observation.survey;
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.reefdb.dto.configuration.programStrategy.ProgramDTO;
27 import fr.ifremer.reefdb.dto.data.survey.CampaignDTO;
28 import fr.ifremer.reefdb.dto.data.survey.SurveyDTO;
29 import fr.ifremer.reefdb.dto.enums.FilterTypeValues;
30 import fr.ifremer.reefdb.dto.referential.LocationDTO;
31 import fr.ifremer.reefdb.dto.referential.PersonDTO;
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.tab.TabHandler;
38
39 import javax.swing.AbstractAction;
40 import javax.swing.JButton;
41 import java.awt.event.ActionEvent;
42 import java.util.ArrayList;
43 import java.util.List;
44
45 import static org.nuiton.i18n.I18n.t;
46
47
48
49
50 public class SurveyDetailsTabUIHandler extends AbstractReefDbUIHandler<SurveyDetailsTabUIModel, SurveyDetailsTabUI> implements TabHandler {
51
52
53
54
55 private static final Log LOG = LogFactory.getLog(SurveyDetailsTabUIHandler.class);
56
57
58 public static final String OBSERVERS_DOUBLE_LIST = "observersDoubleList";
59
60 public static final String OBSERVERS_LIST = "observersList";
61
62 private AbstractAction unfilterUsersAction;
63
64
65 @Override
66 public void beforeInit(final SurveyDetailsTabUI ui) {
67 super.beforeInit(ui);
68
69
70 final SurveyDetailsTabUIModel model = new SurveyDetailsTabUIModel();
71 ui.setContextValue(model);
72 }
73
74
75 @Override
76 public void afterInit(final SurveyDetailsTabUI ui) {
77 initUI(ui);
78 SwingUtil.setLayerUI(ui.getDetailsPanel(), ui.getSurveyDetailsBlockLayer());
79
80
81 initComboBox();
82
83
84 initObserversLists();
85
86 initListeners();
87
88
89 listenValidatorValid(getValidator(), getModel());
90 }
91
92 private void initListeners() {
93
94
95 getModel().addPropertyChangeListener(evt -> {
96
97 if (SurveyDetailsTabUIModel.PROPERTY_OBSERVATION_MODEL.equals(evt.getPropertyName())) {
98
99 load(getModel().getObservationModel());
100
101 } else if (SurveyDetailsTabUIModel.PROPERTY_OBSERVERS.equals(evt.getPropertyName())) {
102
103 if (getModel().isAdjusting()) return;
104
105
106 List<PersonDTO> observers = new ArrayList<>(getModel().getObservers());
107
108 getUI().getObserversDoubleList().getHandler().setSelected(observers);
109 getUI().getObserversList().setListData(observers.toArray(new PersonDTO[observers.size()]));
110
111 } else if (SurveyDetailsTabUIModel.PROPERTY_PROGRAM.equals(evt.getPropertyName())) {
112
113 updateLocations(false);
114
115 } else if (SurveyDetailsTabUIModel.PROPERTY_LOCATION.equals(evt.getPropertyName())) {
116
117 getUI().applyDataBinding(SurveyDetailsTabUI.BINDING_LOCATION_MIN_LATITUDE_EDITOR_NUMBER_VALUE);
118 getUI().applyDataBinding(SurveyDetailsTabUI.BINDING_LOCATION_MAX_LATITUDE_EDITOR_NUMBER_VALUE);
119 getUI().applyDataBinding(SurveyDetailsTabUI.BINDING_LOCATION_MIN_LONGITUDE_EDITOR_NUMBER_VALUE);
120 getUI().applyDataBinding(SurveyDetailsTabUI.BINDING_LOCATION_MAX_LONGITUDE_EDITOR_NUMBER_VALUE);
121
122 updatePrograms(false);
123
124 } else if (SurveyDetailsTabUIModel.PROPERTY_DATE.equals(evt.getPropertyName())) {
125
126 updatePrograms(false);
127 updateCampaigns(false);
128
129 } else if (SurveyDetailsTabUIModel.PROPERTY_COORDINATE.equals(evt.getPropertyName())) {
130
131 getUI().applyDataBinding(SurveyDetailsTabUI.BINDING_SURVEY_LATITUDE_EDITOR_NUMBER_VALUE);
132 getUI().applyDataBinding(SurveyDetailsTabUI.BINDING_SURVEY_LONGITUDE_EDITOR_NUMBER_VALUE);
133
134
135 } else if (SurveyDetailsTabUIModel.PROPERTY_LATITUDE.equals(evt.getPropertyName())
136 || SurveyDetailsTabUIModel.PROPERTY_LONGITUDE.equals(evt.getPropertyName())) {
137
138 if (getModel().getLatitude() == null && getModel().getLongitude() == null) {
139 getModel().setPositioning(null);
140 }
141
142
143
144
145
146 } else if (SurveyDetailsTabUIModel.PROPERTY_POSITIONING.equals(evt.getPropertyName())) {
147
148 getUI().getSelectionPrecisionLabel().setText(getModel().getPositioningPrecision());
149
150 } else if (SurveyDetailsTabUIModel.PROPERTY_RECORDER_DEPARTMENT.equals(evt.getPropertyName())) {
151
152 getUI().getInputSaisisseur().setText(decorate(getModel().getRecorderDepartment()));
153
154 } else if (SurveyDetailsTabUIModel.PROPERTY_EDITABLE.equals(evt.getPropertyName())) {
155
156 if (getModel().isEditable()) {
157 getUI().getObserversPanelLayout().setSelected(OBSERVERS_DOUBLE_LIST);
158 } else {
159 getUI().getObserversPanelLayout().setSelected(OBSERVERS_LIST);
160 }
161
162 }
163 });
164
165 }
166
167
168
169
170
171
172 private void load(final SurveyDTO survey) {
173
174
175 getModel().setBean(survey);
176
177
178 getModel().setModify(false);
179 }
180
181
182
183
184 private void initComboBox() {
185
186
187 initBeanFilterableComboBox(
188 getUI().getCampaignCombo(),
189 null,
190 null);
191 getUI().getCampaignCombo().setActionListener(e -> {
192 if (!askBefore(t("reefdb.common.unfilter"), t("reefdb.common.unfilter.confirmation"))) {
193 return;
194 }
195 updateCampaigns(true);
196 });
197 updateCampaigns(false);
198
199
200 initBeanFilterableComboBox(
201 getUI().getProgramCombo(),
202 null,
203 null);
204 getUI().getProgramCombo().setActionListener(e -> {
205 if (!askBefore(t("reefdb.common.unfilter"), t("reefdb.common.unfilter.confirmation"))) {
206 return;
207 }
208 updatePrograms(true);
209 });
210 updatePrograms(false);
211
212
213 initBeanFilterableComboBox(
214 getUI().getLocationCombo(),
215 null,
216 null);
217 getUI().getLocationCombo().setActionListener(e -> {
218 if (!askBefore(t("reefdb.common.unfilter"), t("reefdb.common.unfilter.confirmation"))) {
219 return;
220 }
221 updateLocations(true);
222 });
223 updateLocations(false);
224
225
226 initBeanFilterableComboBox(
227 getUI().getSelectionPositionnementCombo(),
228 getContext().getReferentialService().getPositioningSystems(),
229 null);
230
231
232 initBeanFilterableComboBox(
233 getUI().getProfondeurEditor(),
234 getContext().getReferentialService().getDepths(),
235 null);
236
237 }
238
239 @SuppressWarnings("unchecked")
240 private void initObserversLists() {
241
242
243 getUI().getObserversList().setCellRenderer(newListCellRender(PersonDTO.class));
244
245
246 initBeanList(
247 getUI().getObserversDoubleList(),
248 new ArrayList<>(),
249 new ArrayList<>(),
250 300);
251
252
253 unfilterUsersAction = new AbstractAction("", SwingUtil.createActionIcon("unfilter")){
254 @Override
255 public void actionPerformed(ActionEvent e) {
256 updateUsers(true);
257 }
258 };
259 JButton unfilterUsersButton = new JButton();
260 unfilterUsersButton.setAction(unfilterUsersAction);
261 unfilterUsersButton.setToolTipText(t("reefdb.common.unfilter"));
262 getUI().getObserversDoubleList().getFilterFieldLabel().setVisible(false);
263 getUI().getObserversDoubleList().getToolbarLeft().add(unfilterUsersButton, 0);
264 updateUsers(false);
265 }
266
267 private void updatePrograms(boolean forceNoFilter) {
268
269 if (getModel().isAdjusting() || !getModel().isEditable()) {
270 return;
271 }
272 getModel().setAdjusting(true);
273
274 getUI().getProgramCombo().setActionEnabled(!forceNoFilter
275 && getContext().getDataContext().isContextFiltered(FilterTypeValues.PROGRAM));
276
277 List<ProgramDTO> programs = getContext().getObservationService().getAvailablePrograms(
278 null,
279 getModel().getLocationId(),
280 getModel().getDate(),
281 forceNoFilter);
282
283
284
285 if (!getModel().isLoading() && getModel().getProgram() != null && !programs.contains(getModel().getProgram())) {
286 getModel().setProgram(null);
287 }
288 getUI().getProgramCombo().setData(programs);
289
290 getModel().setAdjusting(false);
291 }
292
293 private void updateCampaigns(boolean forceNoFilter) {
294
295 if (getModel().isAdjusting() || !getModel().isEditable()) {
296 return;
297 }
298 getModel().setAdjusting(true);
299
300 getUI().getCampaignCombo().setActionEnabled(!forceNoFilter
301 && getContext().getDataContext().isContextFiltered(FilterTypeValues.CAMPAIGN));
302
303 List<CampaignDTO> campaigns = getContext().getObservationService().getAvailableCampaigns(getModel().getDate(), forceNoFilter);
304
305
306 if (!getModel().isLoading() && getModel().getCampaign() != null && !campaigns.contains(getModel().getCampaign())) {
307 getModel().setCampaign(null);
308 }
309 getUI().getCampaignCombo().setData(campaigns);
310
311 getModel().setAdjusting(false);
312 }
313
314 private void updateLocations(boolean forceNoFilter) {
315
316 if (getModel().isAdjusting() || !getModel().isEditable()) {
317 return;
318 }
319 getModel().setAdjusting(true);
320
321 getUI().getLocationCombo().setActionEnabled(!forceNoFilter
322 && getContext().getDataContext().isContextFiltered(FilterTypeValues.LOCATION));
323
324 List<LocationDTO> locations = getContext().getObservationService().getAvailableLocations(
325 null,
326 getModel().getProgramCode(),
327 forceNoFilter);
328
329
330
331 if (!getModel().isLoading() && getModel().getLocation() != null && !locations.contains(getModel().getLocation())) {
332 getModel().setLocation(null);
333 }
334 getUI().getLocationCombo().setData(locations);
335
336 getModel().setAdjusting(false);
337 }
338
339 private void updateUsers(boolean forceNoFilter) {
340
341 if (getModel().isAdjusting()) {
342 return;
343 }
344 getModel().setAdjusting(true);
345
346 unfilterUsersAction.setEnabled(!forceNoFilter
347 && getContext().getDataContext().isContextFiltered(FilterTypeValues.USER));
348
349 List<PersonDTO> users = getContext().getObservationService().getAvailableUsers(forceNoFilter);
350
351 getUI().getObserversDoubleList().getModel().setUniverse(users);
352 getUI().getObserversDoubleList().getHandler().sortData();
353
354 getModel().setAdjusting(false);
355 }
356
357
358 @Override
359 public SwingValidator<SurveyDetailsTabUIModel> getValidator() {
360 return getUI().getValidator();
361 }
362
363
364 @Override
365 public boolean onHideTab(int currentIndex, int newIndex) {
366 return true;
367 }
368
369
370 @Override
371 public void onShowTab(int currentIndex, int newIndex) {
372
373 }
374
375
376 @Override
377 public boolean onRemoveTab() {
378 return false;
379 }
380
381
382
383
384 public void save() {
385
386 getModel().setAdjusting(true);
387
388
389 getModel().setObservers(getModel().getSelectedObservers());
390
391 getModel().setAdjusting(false);
392 }
393 }