Project

General

Profile

Download (14.2 KB) Statistics
| Branch: | Tag: | Revision:
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.log4j.Logger;
14

    
15
import com.vaadin.annotations.AutoGenerated;
16
import com.vaadin.data.Validator.EmptyValueException;
17
import com.vaadin.data.util.sqlcontainer.RowId;
18
import com.vaadin.server.Page;
19
import com.vaadin.ui.Alignment;
20
import com.vaadin.ui.Button;
21
import com.vaadin.ui.Button.ClickEvent;
22
import com.vaadin.ui.ComboBox;
23
import com.vaadin.ui.CustomComponent;
24
import com.vaadin.ui.GridLayout;
25
import com.vaadin.ui.HorizontalLayout;
26
import com.vaadin.ui.Label;
27
import com.vaadin.ui.Notification;
28
import com.vaadin.ui.Notification.Type;
29
import com.vaadin.ui.TextField;
30
import com.vaadin.ui.UI;
31
import com.vaadin.ui.VerticalLayout;
32
import com.vaadin.ui.Window;
33

    
34
import eu.etaxonomy.cdm.vaadin.component.CdmProgressComponent;
35
import eu.etaxonomy.cdm.vaadin.container.IdUuidName;
36
import eu.etaxonomy.cdm.vaadin.session.CdmChangeEvent;
37
import eu.etaxonomy.cdm.vaadin.session.CdmChangeEvent.Action;
38
import eu.etaxonomy.cdm.vaadin.util.CdmVaadinOperation;
39
import eu.etaxonomy.cdm.vaadin.util.CdmVaadinUtilities;
40

    
41
/**
42
 * @author cmathew
43
 * @date 2 Apr 2015
44
 *
45
 */
46
public class NewTaxonBaseComposite extends CustomComponent implements INewTaxonBaseComposite {
47

    
48
    /*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */
49

    
50
    @AutoGenerated
51
    private VerticalLayout mainLayout;
52
    @AutoGenerated
53
    private CdmProgressComponent cdmProgressComponent;
54
    @AutoGenerated
55
    private HorizontalLayout horizontalLayout;
56
    @AutoGenerated
57
    private Button cancelButton;
58
    @AutoGenerated
59
    private Button saveButton;
60
    @AutoGenerated
61
    private GridLayout gridLayout;
62
    @AutoGenerated
63
    private ComboBox synComboBox;
64
    @AutoGenerated
65
    private Label synSecLabel;
66
    @AutoGenerated
67
    private ComboBox accTaxonSecComboBox;
68
    @AutoGenerated
69
    private Label accTaxonSecLabel;
70
    @AutoGenerated
71
    private TextField nameTextField;
72
    @AutoGenerated
73
    private Label nameLabel;
74
    @AutoGenerated
75
    private Label accTaxonNameValue;
76
    @AutoGenerated
77
    private Label accTaxonLabel;
78
    private INewTaxonBaseComponentListener listener;
79

    
80

    
81
    private final Window dialog;
82
    private final IdUuidName accTaxonIun;
83
    private final IdUuidName classificationIun;
84

    
85
    private static final Logger logger = Logger.getLogger(NewTaxonBaseComposite.class);
86

    
87
    private static final String CHOOSE_SECUNDUM_PROMPT = "Choose Secundum ....";
88

    
89
    /**
90
     * The constructor should first build the main layout, set the
91
     * composition root and then do any custom initialization.
92
     *
93
     * The constructor will not be automatically regenerated by the
94
     * visual editor.
95
     */
96
    public NewTaxonBaseComposite(Window dialog,
97
            INewTaxonBaseComponentListener listener,
98
            IdUuidName accTaxonIun,
99
            String accTaxonName,
100
            IdUuidName classificationIun) {
101
        buildMainLayout();
102
        setCompositionRoot(mainLayout);
103

    
104
        this.listener = listener;
105
        this.dialog = dialog;
106
        this.accTaxonIun = accTaxonIun;
107
        this.classificationIun = classificationIun;
108

    
109
        addUIListeners();
110

    
111
        if(accTaxonName == null || accTaxonName.isEmpty()) {
112
            // this is the case where we create a new taxon
113
            accTaxonLabel.setVisible(false);
114
            accTaxonNameValue.setVisible(false);
115
            synSecLabel.setVisible(false);
116
            synComboBox.setVisible(false);
117
        } else {
118
            // this is the case where we create a new synonym
119
            accTaxonNameValue.setValue(accTaxonName);
120
        }
121
        init();
122
    }
123

    
124
    public void init() {
125
        initAccTaxonSecComboBox();
126
        if(accTaxonIun != null) {
127
            initSynSecComboBox();
128
        }
129
    }
130

    
131
    private void  initAccTaxonSecComboBox() {
132
        accTaxonSecComboBox.setNullSelectionAllowed(false);
133
        accTaxonSecComboBox.setItemCaptionPropertyId("titleCache");
134
        accTaxonSecComboBox.setImmediate(true);
135
        if(listener != null) {
136
            accTaxonSecComboBox.setContainerDataSource(listener.getAccTaxonSecRefContainer());
137
            Object selectedSecItemId = listener.getClassificationRefId(classificationIun.getUuid());
138
            if(selectedSecItemId != null) {
139
                accTaxonSecComboBox.setValue(selectedSecItemId);
140
            } else {
141
                accTaxonSecComboBox.setInputPrompt(CHOOSE_SECUNDUM_PROMPT);
142
            }
143
        }
144
    }
145

    
146
    private void initSynSecComboBox() {
147
        synComboBox.setNullSelectionAllowed(false);
148
        synComboBox.setItemCaptionPropertyId("titleCache");
149
        synComboBox.setImmediate(true);
150
        if(listener != null) {
151
            synComboBox.setContainerDataSource(listener.getSynSecRefContainer());
152
            Object selectedSecItemId = listener.getClassificationRefId(classificationIun.getUuid());
153
            if(selectedSecItemId != null) {
154
                synComboBox.setValue(selectedSecItemId);
155
            } else {
156
                synComboBox.setInputPrompt(CHOOSE_SECUNDUM_PROMPT);
157
            }
158
        }
159
    }
160

    
161

    
162

    
163
    private void addUIListeners() {
164
        addSaveButtonListener();
165
        addCancelButtonListener();
166
    }
167

    
168
    private void addSaveButtonListener() {
169
        saveButton.addClickListener(new Button.ClickListener() {
170

    
171
            @Override
172
            public void buttonClick(ClickEvent event) {
173
                try {
174
                    nameTextField.validate();
175
                    accTaxonSecComboBox.validate();
176
                    if(accTaxonIun != null) {
177
                        synComboBox.validate();
178
                    }
179
                } catch (EmptyValueException e) {
180
                    Notification notification = new Notification("Invalid input", "Neither Name or Secundum can be empty", Type.WARNING_MESSAGE);
181
                    notification.show(Page.getCurrent());
182
                    return;
183
                }
184

    
185
                CdmVaadinUtilities.setEnabled(mainLayout, false, null);
186

    
187
                CdmVaadinUtilities.exec(new CdmVaadinOperation(500, cdmProgressComponent) {
188
                    @Override
189
                    public boolean execute() {
190
                        setProgress("Saving Taxon " + nameTextField.getValue());
191
                        IdUuidName taxonBaseIdUuid;
192
                        boolean newTaxon = false;
193
                        try {
194
                            if(accTaxonIun == null) {
195
                                taxonBaseIdUuid = listener.newTaxon(nameTextField.getValue(),accTaxonSecComboBox.getValue(), classificationIun.getUuid());
196
                                newTaxon = true;
197
                            } else {
198
                                taxonBaseIdUuid = listener.newSynonym(nameTextField.getValue(),
199
                                        accTaxonSecComboBox.getValue(),
200
                                        accTaxonSecComboBox.getValue(),
201
                                        accTaxonIun.getUuid());
202
                                newTaxon = false;
203
                            }
204
                            Object rowId = new RowId(taxonBaseIdUuid.getId());
205
                            registerDelayedEvent(new CdmChangeEvent(Action.Create, Arrays.asList(rowId, newTaxon), NewTaxonBaseComposite.this));
206
                        } catch (IllegalArgumentException iae) {
207
                            setException(iae);
208
                            return false;
209
                        }
210
                        return true;
211
                    }
212

    
213
                    @Override
214
                    public void postOpUIUpdate(boolean success) {
215
                        if(success) {
216
                            UI.getCurrent().removeWindow(dialog);
217
                        } else {
218
                            CdmVaadinUtilities.setEnabled(mainLayout, true, null);
219
                        }
220
                    }
221
                });
222
            }
223
        });
224
    }
225

    
226
    private void addCancelButtonListener() {
227
        cancelButton.addClickListener(new Button.ClickListener() {
228

    
229
            @Override
230
            public void buttonClick(ClickEvent event) {
231
                UI.getCurrent().removeWindow(dialog);
232
            }
233

    
234
        });
235
    }
236

    
237
    /* (non-Javadoc)
238
     * @see eu.etaxonomy.cdm.vaadin.view.INewTaxonComposite#setListener(eu.etaxonomy.cdm.vaadin.view.INewTaxonBaseComponentListener)
239
     */
240
    @Override
241
    public void setListener(INewTaxonBaseComponentListener listener) {
242
        this.listener = listener;
243

    
244
    }
245

    
246
    @AutoGenerated
247
    private VerticalLayout buildMainLayout() {
248
        // common part: create layout
249
        mainLayout = new VerticalLayout();
250
        mainLayout.setImmediate(false);
251
        mainLayout.setWidth("420px");
252
        mainLayout.setHeight("240px");
253
        mainLayout.setMargin(true);
254

    
255
        // top-level component properties
256
        setWidth("420px");
257
        setHeight("240px");
258

    
259
        // gridLayout
260
        gridLayout = buildGridLayout();
261
        mainLayout.addComponent(gridLayout);
262
        mainLayout.setExpandRatio(gridLayout, 1.0f);
263

    
264
        // horizontalLayout
265
        horizontalLayout = buildHorizontalLayout();
266
        mainLayout.addComponent(horizontalLayout);
267
        mainLayout.setComponentAlignment(horizontalLayout, new Alignment(20));
268

    
269
        // cdmProgressComponent
270
        cdmProgressComponent = new CdmProgressComponent();
271
        cdmProgressComponent.setImmediate(false);
272
        cdmProgressComponent.setWidth("-1px");
273
        cdmProgressComponent.setHeight("-1px");
274
        mainLayout.addComponent(cdmProgressComponent);
275
        mainLayout.setComponentAlignment(cdmProgressComponent, new Alignment(48));
276

    
277
        return mainLayout;
278
    }
279

    
280
    @AutoGenerated
281
    private GridLayout buildGridLayout() {
282
        // common part: create layout
283
        gridLayout = new GridLayout();
284
        gridLayout.setImmediate(false);
285
        gridLayout.setWidth("-1px");
286
        gridLayout.setHeight("140px");
287
        gridLayout.setMargin(false);
288
        gridLayout.setSpacing(true);
289
        gridLayout.setColumns(2);
290
        gridLayout.setRows(4);
291

    
292
        // accTaxonLabel
293
        accTaxonLabel = new Label();
294
        accTaxonLabel.setImmediate(false);
295
        accTaxonLabel.setWidth("-1px");
296
        accTaxonLabel.setHeight("-1px");
297
        accTaxonLabel.setValue("Acc. Taxon : ");
298
        gridLayout.addComponent(accTaxonLabel, 0, 0);
299
        gridLayout.setComponentAlignment(accTaxonLabel, new Alignment(34));
300

    
301
        // accTaxonNameValue
302
        accTaxonNameValue = new Label();
303
        accTaxonNameValue.setImmediate(false);
304
        accTaxonNameValue.setWidth("-1px");
305
        accTaxonNameValue.setHeight("-1px");
306
        accTaxonNameValue.setValue("Taxon Name");
307
        gridLayout.addComponent(accTaxonNameValue, 1, 0);
308
        gridLayout.setComponentAlignment(accTaxonNameValue, new Alignment(33));
309

    
310
        // nameLabel
311
        nameLabel = new Label();
312
        nameLabel.setImmediate(false);
313
        nameLabel.setWidth("-1px");
314
        nameLabel.setHeight("-1px");
315
        nameLabel.setValue("Name : ");
316
        gridLayout.addComponent(nameLabel, 0, 1);
317
        gridLayout.setComponentAlignment(nameLabel, new Alignment(34));
318

    
319
        // nameTextField
320
        nameTextField = new TextField();
321
        nameTextField.setImmediate(false);
322
        nameTextField.setWidth("190px");
323
        nameTextField.setHeight("-1px");
324
        nameTextField.setInvalidAllowed(false);
325
        nameTextField.setRequired(true);
326
        gridLayout.addComponent(nameTextField, 1, 1);
327
        gridLayout.setComponentAlignment(nameTextField, new Alignment(33));
328

    
329
        // accTaxonSecLabel
330
        accTaxonSecLabel = new Label();
331
        accTaxonSecLabel.setImmediate(false);
332
        accTaxonSecLabel.setWidth("-1px");
333
        accTaxonSecLabel.setHeight("-1px");
334
        accTaxonSecLabel.setValue("Acc. Taxon Secundum : ");
335
        gridLayout.addComponent(accTaxonSecLabel, 0, 2);
336
        gridLayout.setComponentAlignment(accTaxonSecLabel, new Alignment(34));
337

    
338
        // accTaxonSecComboBox
339
        accTaxonSecComboBox = new ComboBox();
340
        accTaxonSecComboBox.setImmediate(false);
341
        accTaxonSecComboBox.setWidth("190px");
342
        accTaxonSecComboBox.setHeight("-1px");
343
        accTaxonSecComboBox.setInvalidAllowed(false);
344
        accTaxonSecComboBox.setRequired(true);
345
        gridLayout.addComponent(accTaxonSecComboBox, 1, 2);
346
        gridLayout.setComponentAlignment(accTaxonSecComboBox, new Alignment(33));
347

    
348
        // synSecLabel
349
        synSecLabel = new Label();
350
        synSecLabel.setImmediate(false);
351
        synSecLabel.setWidth("-1px");
352
        synSecLabel.setHeight("-1px");
353
        synSecLabel.setValue("Synonym Secundum : ");
354
        gridLayout.addComponent(synSecLabel, 0, 3);
355

    
356
        // synComboBox
357
        synComboBox = new ComboBox();
358
        synComboBox.setImmediate(false);
359
        synComboBox.setWidth("190px");
360
        synComboBox.setHeight("-1px");
361
        synComboBox.setInvalidAllowed(false);
362
        synComboBox.setRequired(true);
363
        gridLayout.addComponent(synComboBox, 1, 3);
364

    
365
        return gridLayout;
366
    }
367

    
368
    @AutoGenerated
369
    private HorizontalLayout buildHorizontalLayout() {
370
        // common part: create layout
371
        horizontalLayout = new HorizontalLayout();
372
        horizontalLayout.setImmediate(false);
373
        horizontalLayout.setWidth("-1px");
374
        horizontalLayout.setHeight("-1px");
375
        horizontalLayout.setMargin(false);
376
        horizontalLayout.setSpacing(true);
377

    
378
        // saveButton
379
        saveButton = new Button();
380
        saveButton.setCaption("Save");
381
        saveButton.setImmediate(true);
382
        saveButton.setWidth("-1px");
383
        saveButton.setHeight("-1px");
384
        horizontalLayout.addComponent(saveButton);
385
        horizontalLayout.setComponentAlignment(saveButton, new Alignment(20));
386

    
387
        // cancelButton
388
        cancelButton = new Button();
389
        cancelButton.setCaption("Cancel");
390
        cancelButton.setImmediate(true);
391
        cancelButton.setWidth("-1px");
392
        cancelButton.setHeight("-1px");
393
        horizontalLayout.addComponent(cancelButton);
394
        horizontalLayout.setComponentAlignment(cancelButton, new Alignment(20));
395

    
396
        return horizontalLayout;
397
    }
398

    
399

    
400
}
(9-9/12)