cleanup
[cdm-vaadin.git] / src / main / java / eu / etaxonomy / cdm / vaadin / component / taxon / NewTaxonBaseComposite.java
1 /**
2 * Copyright (C) 2015 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.cdm.vaadin.component.taxon;
10
11 import java.util.Arrays;
12
13 import org.apache.logging.log4j.LogManager;
14 import org.apache.logging.log4j.Logger;
15
16 import com.vaadin.annotations.AutoGenerated;
17 import com.vaadin.data.Validator.EmptyValueException;
18 import com.vaadin.data.util.sqlcontainer.RowId;
19 import com.vaadin.server.Page;
20 import com.vaadin.ui.Alignment;
21 import com.vaadin.ui.Button;
22 import com.vaadin.ui.Button.ClickEvent;
23 import com.vaadin.ui.ComboBox;
24 import com.vaadin.ui.CustomComponent;
25 import com.vaadin.ui.GridLayout;
26 import com.vaadin.ui.HorizontalLayout;
27 import com.vaadin.ui.Label;
28 import com.vaadin.ui.Notification;
29 import com.vaadin.ui.Notification.Type;
30 import com.vaadin.ui.TextField;
31 import com.vaadin.ui.UI;
32 import com.vaadin.ui.VerticalLayout;
33 import com.vaadin.ui.Window;
34
35 import eu.etaxonomy.cdm.vaadin.component.CdmProgressComponent;
36 import eu.etaxonomy.cdm.vaadin.component.TextFieldNFix;
37 import eu.etaxonomy.cdm.vaadin.container.IdUuidName;
38 import eu.etaxonomy.cdm.vaadin.session.CdmChangeEvent;
39 import eu.etaxonomy.cdm.vaadin.session.CdmChangeEvent.Action;
40 import eu.etaxonomy.cdm.vaadin.util.CdmVaadinOperation;
41 import eu.etaxonomy.cdm.vaadin.util.CdmVaadinUtilities;
42
43 /**
44 * @author cmathew
45 * @since 2 Apr 2015
46 */
47 public class NewTaxonBaseComposite extends CustomComponent implements INewTaxonBaseComposite {
48
49 @SuppressWarnings("unused")
50 private static final Logger logger = LogManager.getLogger();
51
52 /*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */
53
54 @AutoGenerated
55 private VerticalLayout mainLayout;
56 @AutoGenerated
57 private CdmProgressComponent cdmProgressComponent;
58 @AutoGenerated
59 private HorizontalLayout horizontalLayout;
60 @AutoGenerated
61 private Button cancelButton;
62 @AutoGenerated
63 private Button saveButton;
64 @AutoGenerated
65 private GridLayout gridLayout;
66 @AutoGenerated
67 private ComboBox synComboBox;
68 @AutoGenerated
69 private Label synSecLabel;
70 @AutoGenerated
71 private ComboBox accTaxonSecComboBox;
72 @AutoGenerated
73 private Label accTaxonSecLabel;
74 @AutoGenerated
75 private TextField nameTextField;
76 @AutoGenerated
77 private Label nameLabel;
78 @AutoGenerated
79 private Label accTaxonNameValue;
80 @AutoGenerated
81 private Label accTaxonLabel;
82 private INewTaxonBaseComponentListener listener;
83
84
85 private final Window dialog;
86 private final IdUuidName accTaxonIun;
87 private final IdUuidName classificationIun;
88 private static final String CHOOSE_SECUNDUM_PROMPT = "Choose Secundum ....";
89
90 /**
91 * The constructor should first build the main layout, set the
92 * composition root and then do any custom initialization.
93 *
94 * The constructor will not be automatically regenerated by the
95 * visual editor.
96 */
97 public NewTaxonBaseComposite(Window dialog,
98 INewTaxonBaseComponentListener listener,
99 IdUuidName accTaxonIun,
100 String accTaxonName,
101 IdUuidName classificationIun) {
102 buildMainLayout();
103 setCompositionRoot(mainLayout);
104
105 this.listener = listener;
106 this.dialog = dialog;
107 this.accTaxonIun = accTaxonIun;
108 this.classificationIun = classificationIun;
109
110 addUIListeners();
111
112 if(accTaxonName == null || accTaxonName.isEmpty()) {
113 // this is the case where we create a new taxon
114 accTaxonLabel.setVisible(false);
115 accTaxonNameValue.setVisible(false);
116 synSecLabel.setVisible(false);
117 synComboBox.setVisible(false);
118 } else {
119 // this is the case where we create a new synonym
120 accTaxonNameValue.setValue(accTaxonName);
121 }
122 init();
123 }
124
125 public void init() {
126 initAccTaxonSecComboBox();
127 if(accTaxonIun != null) {
128 initSynSecComboBox();
129 }
130 }
131
132 private void initAccTaxonSecComboBox() {
133 accTaxonSecComboBox.setNullSelectionAllowed(false);
134 accTaxonSecComboBox.setItemCaptionPropertyId("titleCache");
135 accTaxonSecComboBox.setImmediate(true);
136 if(listener != null) {
137 accTaxonSecComboBox.setContainerDataSource(listener.getAccTaxonSecRefContainer());
138 Object selectedSecItemId = listener.getClassificationRefId(classificationIun.getUuid());
139 if(selectedSecItemId != null) {
140 accTaxonSecComboBox.setValue(selectedSecItemId);
141 } else {
142 accTaxonSecComboBox.setInputPrompt(CHOOSE_SECUNDUM_PROMPT);
143 }
144 }
145 }
146
147 private void initSynSecComboBox() {
148 synComboBox.setNullSelectionAllowed(false);
149 synComboBox.setItemCaptionPropertyId("titleCache");
150 synComboBox.setImmediate(true);
151 if(listener != null) {
152 synComboBox.setContainerDataSource(listener.getSynSecRefContainer());
153 Object selectedSecItemId = listener.getClassificationRefId(classificationIun.getUuid());
154 if(selectedSecItemId != null) {
155 synComboBox.setValue(selectedSecItemId);
156 } else {
157 synComboBox.setInputPrompt(CHOOSE_SECUNDUM_PROMPT);
158 }
159 }
160 }
161
162
163
164 private void addUIListeners() {
165 addSaveButtonListener();
166 addCancelButtonListener();
167 }
168
169 private void addSaveButtonListener() {
170 saveButton.addClickListener(new Button.ClickListener() {
171
172 @Override
173 public void buttonClick(ClickEvent event) {
174 try {
175 nameTextField.validate();
176 accTaxonSecComboBox.validate();
177 if(accTaxonIun != null) {
178 synComboBox.validate();
179 }
180 } catch (EmptyValueException e) {
181 Notification notification = new Notification("Invalid input", "Neither Name or Secundum can be empty", Type.WARNING_MESSAGE);
182 notification.show(Page.getCurrent());
183 return;
184 }
185
186 CdmVaadinUtilities.setEnabled(mainLayout, false, null);
187
188 CdmVaadinUtilities.exec(new CdmVaadinOperation(500, cdmProgressComponent) {
189 @Override
190 public boolean execute() {
191 setProgress("Saving Taxon " + nameTextField.getValue());
192 IdUuidName taxonBaseIdUuid;
193 boolean newTaxon = false;
194 try {
195 if(accTaxonIun == null) {
196 taxonBaseIdUuid = listener.newTaxon(nameTextField.getValue(),accTaxonSecComboBox.getValue(), classificationIun.getUuid());
197 newTaxon = true;
198 } else {
199 taxonBaseIdUuid = listener.newSynonym(nameTextField.getValue(),
200 accTaxonSecComboBox.getValue(),
201 accTaxonSecComboBox.getValue(),
202 accTaxonIun.getUuid());
203 newTaxon = false;
204 }
205 Object rowId = new RowId(taxonBaseIdUuid.getId());
206 registerDelayedEvent(new CdmChangeEvent(Action.Create, Arrays.asList(rowId, newTaxon), NewTaxonBaseComposite.this));
207 } catch (IllegalArgumentException iae) {
208 setException(iae);
209 return false;
210 }
211 return true;
212 }
213
214 @Override
215 public void postOpUIUpdate(boolean success) {
216 if(success) {
217 UI.getCurrent().removeWindow(dialog);
218 } else {
219 CdmVaadinUtilities.setEnabled(mainLayout, true, null);
220 }
221 }
222 });
223 }
224 });
225 }
226
227 private void addCancelButtonListener() {
228 cancelButton.addClickListener(new Button.ClickListener() {
229
230 @Override
231 public void buttonClick(ClickEvent event) {
232 UI.getCurrent().removeWindow(dialog);
233 }
234
235 });
236 }
237
238 /* (non-Javadoc)
239 * @see eu.etaxonomy.cdm.vaadin.view.INewTaxonComposite#setListener(eu.etaxonomy.cdm.vaadin.view.INewTaxonBaseComponentListener)
240 */
241 @Override
242 public void setListener(INewTaxonBaseComponentListener listener) {
243 this.listener = listener;
244
245 }
246
247 @AutoGenerated
248 private VerticalLayout buildMainLayout() {
249 // common part: create layout
250 mainLayout = new VerticalLayout();
251 mainLayout.setImmediate(false);
252 mainLayout.setWidth("420px");
253 mainLayout.setHeight("240px");
254 mainLayout.setMargin(true);
255
256 // top-level component properties
257 setWidth("420px");
258 setHeight("240px");
259
260 // gridLayout
261 gridLayout = buildGridLayout();
262 mainLayout.addComponent(gridLayout);
263 mainLayout.setExpandRatio(gridLayout, 1.0f);
264
265 // horizontalLayout
266 horizontalLayout = buildHorizontalLayout();
267 mainLayout.addComponent(horizontalLayout);
268 mainLayout.setComponentAlignment(horizontalLayout, new Alignment(20));
269
270 // cdmProgressComponent
271 cdmProgressComponent = new CdmProgressComponent();
272 cdmProgressComponent.setImmediate(false);
273 cdmProgressComponent.setWidth("-1px");
274 cdmProgressComponent.setHeight("-1px");
275 mainLayout.addComponent(cdmProgressComponent);
276 mainLayout.setComponentAlignment(cdmProgressComponent, new Alignment(48));
277
278 return mainLayout;
279 }
280
281 @AutoGenerated
282 private GridLayout buildGridLayout() {
283 // common part: create layout
284 gridLayout = new GridLayout();
285 gridLayout.setImmediate(false);
286 gridLayout.setWidth("-1px");
287 gridLayout.setHeight("140px");
288 gridLayout.setMargin(false);
289 gridLayout.setSpacing(true);
290 gridLayout.setColumns(2);
291 gridLayout.setRows(4);
292
293 // accTaxonLabel
294 accTaxonLabel = new Label();
295 accTaxonLabel.setImmediate(false);
296 accTaxonLabel.setWidth("-1px");
297 accTaxonLabel.setHeight("-1px");
298 accTaxonLabel.setValue("Acc. Taxon : ");
299 gridLayout.addComponent(accTaxonLabel, 0, 0);
300 gridLayout.setComponentAlignment(accTaxonLabel, new Alignment(34));
301
302 // accTaxonNameValue
303 accTaxonNameValue = new Label();
304 accTaxonNameValue.setImmediate(false);
305 accTaxonNameValue.setWidth("-1px");
306 accTaxonNameValue.setHeight("-1px");
307 accTaxonNameValue.setValue("Taxon Name");
308 gridLayout.addComponent(accTaxonNameValue, 1, 0);
309 gridLayout.setComponentAlignment(accTaxonNameValue, new Alignment(33));
310
311 // nameLabel
312 nameLabel = new Label();
313 nameLabel.setImmediate(false);
314 nameLabel.setWidth("-1px");
315 nameLabel.setHeight("-1px");
316 nameLabel.setValue("Name : ");
317 gridLayout.addComponent(nameLabel, 0, 1);
318 gridLayout.setComponentAlignment(nameLabel, new Alignment(34));
319
320 // nameTextField
321 nameTextField = new TextFieldNFix();
322 nameTextField.setImmediate(false);
323 nameTextField.setWidth("190px");
324 nameTextField.setHeight("-1px");
325 nameTextField.setInvalidAllowed(false);
326 nameTextField.setRequired(true);
327 gridLayout.addComponent(nameTextField, 1, 1);
328 gridLayout.setComponentAlignment(nameTextField, new Alignment(33));
329
330 // accTaxonSecLabel
331 accTaxonSecLabel = new Label();
332 accTaxonSecLabel.setImmediate(false);
333 accTaxonSecLabel.setWidth("-1px");
334 accTaxonSecLabel.setHeight("-1px");
335 accTaxonSecLabel.setValue("Acc. Taxon Secundum : ");
336 gridLayout.addComponent(accTaxonSecLabel, 0, 2);
337 gridLayout.setComponentAlignment(accTaxonSecLabel, new Alignment(34));
338
339 // accTaxonSecComboBox
340 accTaxonSecComboBox = new ComboBox();
341 accTaxonSecComboBox.setImmediate(false);
342 accTaxonSecComboBox.setWidth("190px");
343 accTaxonSecComboBox.setHeight("-1px");
344 accTaxonSecComboBox.setInvalidAllowed(false);
345 accTaxonSecComboBox.setRequired(true);
346 gridLayout.addComponent(accTaxonSecComboBox, 1, 2);
347 gridLayout.setComponentAlignment(accTaxonSecComboBox, new Alignment(33));
348
349 // synSecLabel
350 synSecLabel = new Label();
351 synSecLabel.setImmediate(false);
352 synSecLabel.setWidth("-1px");
353 synSecLabel.setHeight("-1px");
354 synSecLabel.setValue("Synonym Secundum : ");
355 gridLayout.addComponent(synSecLabel, 0, 3);
356
357 // synComboBox
358 synComboBox = new ComboBox();
359 synComboBox.setImmediate(false);
360 synComboBox.setWidth("190px");
361 synComboBox.setHeight("-1px");
362 synComboBox.setInvalidAllowed(false);
363 synComboBox.setRequired(true);
364 gridLayout.addComponent(synComboBox, 1, 3);
365
366 return gridLayout;
367 }
368
369 @AutoGenerated
370 private HorizontalLayout buildHorizontalLayout() {
371 // common part: create layout
372 horizontalLayout = new HorizontalLayout();
373 horizontalLayout.setImmediate(false);
374 horizontalLayout.setWidth("-1px");
375 horizontalLayout.setHeight("-1px");
376 horizontalLayout.setMargin(false);
377 horizontalLayout.setSpacing(true);
378
379 // saveButton
380 saveButton = new Button();
381 saveButton.setCaption("Save");
382 saveButton.setImmediate(true);
383 saveButton.setWidth("-1px");
384 saveButton.setHeight("-1px");
385 horizontalLayout.addComponent(saveButton);
386 horizontalLayout.setComponentAlignment(saveButton, new Alignment(20));
387
388 // cancelButton
389 cancelButton = new Button();
390 cancelButton.setCaption("Cancel");
391 cancelButton.setImmediate(true);
392 cancelButton.setWidth("-1px");
393 cancelButton.setHeight("-1px");
394 horizontalLayout.addComponent(cancelButton);
395 horizontalLayout.setComponentAlignment(cancelButton, new Alignment(20));
396
397 return horizontalLayout;
398 }
399
400
401 }