Project

General

Profile

Download (14.3 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.component.TextFieldNFix;
36
import eu.etaxonomy.cdm.vaadin.container.IdUuidName;
37
import eu.etaxonomy.cdm.vaadin.session.CdmChangeEvent;
38
import eu.etaxonomy.cdm.vaadin.session.CdmChangeEvent.Action;
39
import eu.etaxonomy.cdm.vaadin.util.CdmVaadinOperation;
40
import eu.etaxonomy.cdm.vaadin.util.CdmVaadinUtilities;
41

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

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

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

    
81

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

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

    
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
}
(9-9/12)