1 package fr.ifremer.reefdb.ui.swing.content.synchro.program;
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 com.google.common.base.Predicate;
27 import com.google.common.collect.Lists;
28 import fr.ifremer.reefdb.dto.ReefDbBeans;
29 import fr.ifremer.reefdb.dto.configuration.programStrategy.ProgramDTO;
30 import fr.ifremer.reefdb.dto.enums.SearchDateValues;
31 import fr.ifremer.reefdb.service.StatusFilter;
32 import fr.ifremer.reefdb.ui.swing.util.AbstractReefDbUIHandler;
33 import fr.ifremer.reefdb.ui.swing.util.ReefDbUIs;
34 import org.apache.commons.collections4.CollectionUtils;
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.nuiton.jaxx.application.swing.util.Cancelable;
38
39 import java.util.List;
40
41
42
43
44 public class ProgramSelectUIHandler extends AbstractReefDbUIHandler<ProgramSelectUIModel, ProgramSelectUI> implements Cancelable {
45
46
47
48
49 private static final Log LOG = LogFactory.getLog(ProgramSelectUIHandler.class);
50
51
52 @Override
53 public void beforeInit(final ProgramSelectUI ui) {
54 super.beforeInit(ui);
55
56
57 final ProgramSelectUIModel model = new ProgramSelectUIModel();
58 ui.setContextValue(model);
59 }
60
61
62 @Override
63 public void afterInit(final ProgramSelectUI ui) {
64
65
66 initUI(ui);
67
68 initOptions(ui);
69
70
71 StatusFilter programStatusFilter = ui.getProgramStatusFilter();
72
73 List<ProgramDTO> programs;
74 if (getContext().isSynchroEnabled()
75 && (programStatusFilter == StatusFilter.NATIONAL || programStatusFilter == StatusFilter.NATIONAL_ACTIVE)) {
76
77 programs = getContext().getProgramStrategyService().getRemoteProgramsByUser(getContext().getAuthenticationInfo());
78 } else {
79 if (ui.getUserIdFilter() != null) {
80 programs = getContext().getProgramStrategyService().getWritableProgramsByUserAndStatus(ui.getUserIdFilter(), programStatusFilter);
81 } else {
82
83 programs = getContext().getProgramStrategyService().getProgramsByStatus(programStatusFilter);
84 }
85 }
86
87 List<ProgramDTO> selectedPrograms = Lists.newArrayList();
88
89 if (CollectionUtils.isNotEmpty(programs) && CollectionUtils.isNotEmpty(ui.getInitialProgramCodes())) {
90 selectedPrograms = ReefDbBeans.filterCollection(programs, (Predicate<ProgramDTO>) input -> input != null && ui.getInitialProgramCodes().contains(input.getCode()));
91 }
92
93
94 getModel().setSelectedPrograms(selectedPrograms);
95 initBeanList(ui.getProgramDoubleList(), programs, selectedPrograms);
96
97 }
98
99 private void initOptions(ProgramSelectUI ui) {
100
101 if (Boolean.TRUE.equals(ui.getFileSynchro())) {
102
103
104 ui.getOptionsPanel().setVisible(true);
105 ui.getOnlyDirtyCheckBox().setVisible(true);
106 ui.getDatesPanel().setVisible(true);
107
108
109 getModel().setDirtyOnly(true);
110
111
112 ui.getStartDateEditor().setEnabled(false);
113 ui.getEndDateEditor().setEnabled(false);
114 ui.getAndLabel().setVisible(false);
115 ui.getEndDateEditor().setVisible(false);
116
117 ReefDbUIs.forceComponentSize(ui.getSearchDateCombo(), 82);
118 ReefDbUIs.forceComponentSize(ui.getStartDateEditor(), 120);
119 ReefDbUIs.forceComponentSize(ui.getEndDateEditor(), 120);
120
121
122 initBeanFilterableComboBox(
123 ui.getSearchDateCombo(),
124 getContext().getSystemService().getSearchDates(),
125 null);
126
127
128 getModel().addPropertyChangeListener(ProgramSelectUIModel.PROPERTY_SEARCH_DATE, evt -> {
129
130 if (getModel().getSearchDateId() != null) {
131
132
133 ui.getStartDateEditor().setEnabled(true);
134 ui.getEndDateEditor().setEnabled(true);
135
136 final SearchDateValues searchDateValue = SearchDateValues.values()[getModel().getSearchDateId()];
137 if (searchDateValue == SearchDateValues.BETWEEN) {
138
139 ui.getAndLabel().setVisible(true);
140 ui.getEndDateEditor().setVisible(true);
141 } else {
142
143 ui.getEndDateEditor().setVisible(false);
144 ui.getAndLabel().setVisible(false);
145 getModel().setEndDate(null);
146 }
147 } else {
148
149
150 ui.getStartDateEditor().setEnabled(false);
151 ui.getEndDateEditor().setEnabled(false);
152 ui.getEndDateEditor().setVisible(false);
153 ui.getAndLabel().setVisible(false);
154 getModel().setStartDate(null);
155 getModel().setEndDate(null);
156 }
157 });
158
159
160 } else {
161
162
163 ui.getOnlyDirtyCheckBox().setVisible(false);
164 ui.getDatesPanel().setVisible(false);
165
166 getModel().setDirtyOnly(false);
167 getModel().setSearchDate(null);
168 getModel().setStartDate(null);
169 getModel().setEndDate(null);
170 }
171
172 if (Boolean.TRUE.equals(ui.getPhotoSynchro())) {
173
174
175 ui.getOptionsPanel().setVisible(true);
176 ui.getEnablePhotoCheckBox().setVisible(true);
177
178 getModel().setEnablePhoto(getConfig().isSynchroPhotoEnabledByDefault());
179
180 } else {
181
182
183 ui.getEnablePhotoCheckBox().setVisible(false);
184 }
185
186
187 if (Boolean.FALSE.equals(ui.getFileSynchro()) && Boolean.FALSE.equals(ui.getPhotoSynchro())) {
188 ui.getOptionsPanel().setVisible(false);
189 }
190 }
191
192
193 @Override
194 public void cancel() {
195
196
197 getModel().setSelectedPrograms(null);
198
199 closeDialog();
200 }
201
202
203
204
205 public void select() {
206 closeDialog();
207 }
208 }