Project

General

Profile

Download (14.3 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
 * Copyright (C) 2015 EDIT
4
 * European Distributed Institute of Taxonomy
5
 * http://www.e-taxonomy.eu
6
 *
7
 * The contents of this file are subject to the Mozilla Public License Version 1.1
8
 * See LICENSE.TXT at the top of this package for the full license terms.
9
 */
10
package eu.etaxonomy.cdm.vaadin.component;
11

    
12
import java.util.Arrays;
13

    
14
import org.apache.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.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
import eu.etaxonomy.cdm.vaadin.view.INewTaxonBaseComponentListener;
41
import eu.etaxonomy.cdm.vaadin.view.INewTaxonBaseComposite;
42

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

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

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

    
82

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

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

    
89
    private static final String CHOOSE_SECUNDUM_PROMPT = "Choose Secundum ....";
90

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

    
106
        this.listener = listener;
107
        this.dialog = dialog;
108
        this.accTaxonIun = accTaxonIun;
109
        this.classificationIun = classificationIun;
110

    
111
        addUIListeners();
112

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

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

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

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

    
163

    
164

    
165
    private void addUIListeners() {
166
        addSaveButtonListener();
167
        addCancelButtonListener();
168
    }
169

    
170
    private void addSaveButtonListener() {
171
        saveButton.addClickListener(new Button.ClickListener() {
172

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

    
187
                CdmVaadinUtilities.setEnabled(mainLayout, false, null);
188

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

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

    
228
    private void addCancelButtonListener() {
229
        cancelButton.addClickListener(new Button.ClickListener() {
230

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

    
236
        });
237
    }
238

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

    
246
    }
247

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

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

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

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

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

    
279
        return mainLayout;
280
    }
281

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

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

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

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

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

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

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

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

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

    
367
        return gridLayout;
368
    }
369

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

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

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

    
398
        return horizontalLayout;
399
    }
400

    
401

    
402
}
(6-6/7)