1 package fr.ifremer.tutti.ui.swing.content.home;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import com.google.common.collect.Lists;
26 import fr.ifremer.tutti.persistence.entities.data.Cruise;
27 import fr.ifremer.tutti.persistence.entities.data.Program;
28 import fr.ifremer.tutti.persistence.entities.protocol.TuttiProtocol;
29 import fr.ifremer.tutti.service.PersistenceService;
30 import fr.ifremer.tutti.ui.swing.util.AbstractTuttiUIHandler;
31 import jaxx.runtime.swing.editor.bean.BeanFilterableComboBox;
32 import jaxx.runtime.validator.swing.SwingValidator;
33 import org.apache.commons.collections4.CollectionUtils;
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36
37 import javax.swing.JComboBox;
38 import javax.swing.JComponent;
39 import java.awt.Font;
40 import java.beans.PropertyChangeListener;
41 import java.util.List;
42
43
44
45
46
47
48
49 public class SelectCruiseUIHandler extends AbstractTuttiUIHandler<SelectCruiseUIModel, SelectCruiseUI> {
50
51
52 private static final Log log =
53 LogFactory.getLog(SelectCruiseUIHandler.class);
54
55 @Override
56 public SwingValidator<SelectCruiseUIModel> getValidator() {
57 return ui.getValidator();
58 }
59
60 @Override
61 public void beforeInit(SelectCruiseUI ui) {
62 super.beforeInit(ui);
63
64 PersistenceService persistenceService = getContext().getPersistenceService();
65
66 SelectCruiseUIModel model = new SelectCruiseUIModel();
67
68 List<Program> programs = Lists.newArrayList(persistenceService.getAllProgram());
69 model.setPrograms(programs);
70
71 Program selectedProgram = null;
72
73 if (programs.isEmpty()) {
74
75
76 if (log.isDebugEnabled()) {
77 log.debug("No program found.");
78 }
79 } else {
80
81
82
83
84 List<Cruise> cruises = null;
85 if (getContext().isProgramFilled()) {
86 selectedProgram = getDataContext().getProgram();
87
88 cruises = Lists.newArrayList(persistenceService.getAllCruise(selectedProgram.getId()));
89 }
90
91 model.setProgram(selectedProgram);
92 model.setCruises(cruises);
93
94 if (CollectionUtils.isEmpty(cruises)) {
95
96
97 } else {
98
99 Cruise selectedCruise = null;
100 if (getContext().isCruiseFilled()) {
101
102
103 selectedCruise = getDataContext().reloadCruise();
104 }
105 model.setCruise(selectedCruise);
106 }
107 }
108
109 List<TuttiProtocol> protocols =
110 Lists.newArrayList(persistenceService.getAllProtocol(selectedProgram == null ? null : selectedProgram.getId()));
111 model.setProtocols(protocols);
112
113 if (protocols.isEmpty()) {
114
115
116 if (log.isDebugEnabled()) {
117 log.debug("No protocol found.");
118 }
119 } else {
120
121 model.setProtocol(getDataContext().getProtocol());
122 }
123 ui.setContextValue(model);
124 }
125
126 @Override
127 public void afterInit(SelectCruiseUI ui) {
128
129 initUI(ui);
130
131 Font font = ui.getEditCatchesButton().getFont();
132 ui.getEditCatchesButton().setFont(font.deriveFont(Font.BOLD, 14));
133 ui.getValidateCatchesButton().setFont(font.deriveFont(Font.BOLD, 14));
134
135 SelectCruiseUIModel model = getModel();
136
137 initBeanFilterableComboBox(ui.getProgramCombobox(), model.getPrograms(), model.getProgram());
138
139 initBeanFilterableComboBox(ui.getCruiseCombobox(), model.getCruises(), model.getCruise());
140
141 initBeanFilterableComboBox(ui.getProtocolCombobox(), model.getProtocols(), model.getProtocol());
142
143 model.addPropertyChangeListener(SelectCruiseUIModel.PROPERTY_PROTOCOLS, evt -> {
144
145 BeanFilterableComboBox<TuttiProtocol> combobox = SelectCruiseUIHandler.this.ui.getProtocolCombobox();
146 List<TuttiProtocol> protocols = (List<TuttiProtocol>) evt.getNewValue();
147
148 combobox.setData(null);
149 if (protocols != null) {
150 combobox.setData(protocols);
151 }
152
153 SelectCruiseUIHandler.this.ui.applyDataBinding(SelectCruiseUI.BINDING_PROTOCOL_COMBOBOX_ENABLED);
154 });
155
156 model.addPropertyChangeListener(SelectCruiseUIModel.PROPERTY_PROGRAM, evt -> {
157 Program newValue = (Program) evt.getNewValue();
158 boolean noProgram = newValue == null;
159 getContext().setProgramId(noProgram ? null : newValue.getId());
160 if (log.isInfoEnabled()) {
161 log.info("Selected program: " + newValue);
162 }
163 List<Cruise> cruises;
164 List<TuttiProtocol> protocols;
165 if (noProgram) {
166 cruises = Lists.newArrayList();
167 protocols = Lists.newArrayList(getPersistenceService().getAllProtocol(null));
168
169 } else {
170 cruises = Lists.newArrayList(getPersistenceService().getAllCruise(newValue.getId()));
171 protocols = Lists.newArrayList(getPersistenceService().getAllProtocol(newValue.getId()));
172 }
173 SelectCruiseUIModel source = (SelectCruiseUIModel) evt.getSource();
174 source.setCruises(cruises);
175 source.setCruise(null);
176 source.setProtocols(protocols);
177 source.setProtocol(null);
178 });
179
180 model.addPropertyChangeListener(SelectCruiseUIModel.PROPERTY_CRUISES, evt -> {
181
182 BeanFilterableComboBox<Cruise> combobox = SelectCruiseUIHandler.this.ui.getCruiseCombobox();
183 List<Cruise> campaigns = (List<Cruise>) evt.getNewValue();
184 combobox.setData(null);
185 if (campaigns != null) {
186 combobox.setData(campaigns);
187 }
188 });
189
190 model.addPropertyChangeListener(SelectCruiseUIModel.PROPERTY_CRUISE, evt -> {
191 Cruise newValue = (Cruise) evt.getNewValue();
192 getContext().setCruiseId(newValue == null ? null : newValue.getIdAsInt());
193 });
194
195 model.addPropertyChangeListener(SelectCruiseUIModel.PROPERTY_PROTOCOLS, evt -> {
196
197 BeanFilterableComboBox<TuttiProtocol> combobox = SelectCruiseUIHandler.this.ui.getProtocolCombobox();
198 List<TuttiProtocol> protocols = (List<TuttiProtocol>) evt.getNewValue();
199 combobox.setData(null);
200 if (protocols != null) {
201 combobox.setData(protocols);
202 }
203 });
204
205 model.addPropertyChangeListener(SelectCruiseUIModel.PROPERTY_PROTOCOL, evt -> {
206 TuttiProtocol newValue = (TuttiProtocol) evt.getNewValue();
207 getContext().setProtocolId(newValue == null ? null : newValue.getId());
208
209 JComboBox editProtocolComboBox = SelectCruiseUIHandler.this.ui.getEditProtocolComboBox();
210
211
212 editProtocolComboBox.putClientProperty(CAN_EDIT, false);
213
214 try {
215
216 if (model.isProgramFound()) {
217
218
219
220 if (editProtocolComboBox.getItemCount() == 2) {
221
222 editProtocolComboBox.removeAllItems();
223 editProtocolComboBox.addItem(getUI().getEditProtocolButton());
224 editProtocolComboBox.addItem(getUI().getCloneProtocolButton());
225 editProtocolComboBox.addItem(getUI().getExportProtocolButton());
226 editProtocolComboBox.addItem(getUI().getDeleteProtocolButton());
227
228 }
229
230 } else {
231
232
233 if (editProtocolComboBox.getItemCount() == 4) {
234
235 editProtocolComboBox.removeItem(getUI().getEditProtocolButton());
236 editProtocolComboBox.removeItem(getUI().getCloneProtocolButton());
237
238 }
239
240 }
241
242 } finally {
243
244
245 editProtocolComboBox.putClientProperty(CAN_EDIT, true);
246 }
247
248 });
249
250 registerValidators(getValidator());
251
252 listenValidatorValid(getValidator(), model);
253
254 getValidator().setBean(model);
255
256 ui.applyDataBinding(SelectCruiseUI.BINDING_NEW_CRUISE_BUTTON_ENABLED);
257 ui.applyDataBinding(SelectCruiseUI.BINDING_EDIT_CATCHES_BUTTON_ENABLED);
258 ui.applyDataBinding(SelectCruiseUI.BINDING_VALIDATE_CATCHES_BUTTON_ENABLED);
259 }
260
261 @Override
262 protected JComponent getComponentToFocus() {
263 return getUI().getEditCatchesButton();
264 }
265
266 @Override
267 public void onCloseUI() {
268 if (log.isDebugEnabled()) {
269 log.debug("closing: " + ui);
270 }
271 PropertyChangeListener[] listeners = getModel().getPropertyChangeListeners();
272 for (PropertyChangeListener listener : listeners) {
273 getModel().removePropertyChangeListener(listener);
274 }
275 clearValidators();
276 }
277
278 }