Project

General

Profile

Download (11.5 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 Label accTaxonNameValue;
66
    @AutoGenerated
67
    private ComboBox secComboBox;
68
    @AutoGenerated
69
    private Label secLabel;
70
    @AutoGenerated
71
    private TextField nameTextField;
72
    @AutoGenerated
73
    private Label nameLabel;
74
    @AutoGenerated
75
    private Label accTaxonLabel;
76
    private INewTaxonBaseComponentListener listener;
77

    
78

    
79
    private final Window dialog;
80
    private final IdUuidName accTaxonIdUuid;
81
    private final IdUuidName classificationIdUuid;
82

    
83
    private static final Logger logger = Logger.getLogger(NewTaxonBaseComposite.class);
84

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

    
100
        this.listener = listener;
101
        this.dialog = dialog;
102
        this.accTaxonIdUuid = accTaxonIdUuid;
103
        this.classificationIdUuid = classificationIdUuid;
104

    
105
        addUIListeners();
106

    
107
        if(accTaxonName == null || accTaxonName.isEmpty()) {
108
            accTaxonLabel.setVisible(false);
109
            accTaxonNameValue.setVisible(false);
110
        } else {
111
            accTaxonNameValue.setValue(accTaxonName);
112
        }
113
        init();
114
    }
115

    
116
    public void init() {
117

    
118
        initSecComboBox();
119

    
120
    }
121

    
122

    
123
    private void initSecComboBox() {
124

    
125
        secComboBox.setNullSelectionAllowed(false);
126
        secComboBox.setItemCaptionPropertyId("titleCache");
127
        secComboBox.setImmediate(true);
128
        if(listener != null) {
129
            secComboBox.setContainerDataSource(listener.getSecRefContainer());
130
        }
131
    }
132

    
133
    private void addUIListeners() {
134
        addSaveButtonListener();
135
        addCancelButtonListener();
136
    }
137

    
138
    private void addSaveButtonListener() {
139
        saveButton.addClickListener(new Button.ClickListener() {
140

    
141
            @Override
142
            public void buttonClick(ClickEvent event) {
143

    
144

    
145
                try {
146
                    nameTextField.validate();
147
                    secComboBox.validate();
148
                } catch (EmptyValueException e) {
149
                    Notification notification = new Notification("Invalid input", "Neither Name or Secundum can be empty", Type.WARNING_MESSAGE);
150
                    notification.setDelayMsec(2000);
151
                    notification.show(Page.getCurrent());
152
                    return;
153
                }
154

    
155
                CdmVaadinUtilities.setEnabled(mainLayout, false, null);
156

    
157
                CdmVaadinUtilities.exec(new CdmVaadinOperation(1000, cdmProgressComponent) {
158
                    @Override
159
                    public boolean execute() {
160
                        setProgress("Saving Taxon " + nameTextField.getValue());
161
                        IdUuidName taxonBaseIdUuid;
162
                        if(accTaxonIdUuid == null) {
163
                            taxonBaseIdUuid = listener.newTaxon(nameTextField.getValue(),secComboBox.getValue(), classificationIdUuid.getUuid());
164
                        } else {
165
                            taxonBaseIdUuid = listener.newSynonym(nameTextField.getValue(),secComboBox.getValue(), accTaxonIdUuid.getUuid());
166
                        }
167

    
168
                        Object rowId = new RowId(taxonBaseIdUuid.getId());
169
                        fireEvent(new CdmChangeEvent(Action.Create, Arrays.asList(rowId), NewTaxonBaseComposite.class));
170

    
171
                        return true;
172
                    }
173

    
174
                    @Override
175
                    public void postOpUIUpdate(boolean success) {
176
                        if(success) {
177
                            UI.getCurrent().removeWindow(dialog);
178
                        } else {
179
                            CdmVaadinUtilities.setEnabled(mainLayout, true, null);
180
                        }
181
                    }
182
                });
183
            }
184
        });
185
    }
186

    
187
    private void addCancelButtonListener() {
188
        cancelButton.addClickListener(new Button.ClickListener() {
189

    
190
            @Override
191
            public void buttonClick(ClickEvent event) {
192
                UI.getCurrent().removeWindow(dialog);
193
            }
194

    
195
        });
196
    }
197

    
198
    /* (non-Javadoc)
199
     * @see eu.etaxonomy.cdm.vaadin.view.INewTaxonComposite#setListener(eu.etaxonomy.cdm.vaadin.view.INewTaxonBaseComponentListener)
200
     */
201
    @Override
202
    public void setListener(INewTaxonBaseComponentListener listener) {
203
        this.listener = listener;
204

    
205
    }
206

    
207
    @AutoGenerated
208
    private VerticalLayout buildMainLayout() {
209
        // common part: create layout
210
        mainLayout = new VerticalLayout();
211
        mainLayout.setImmediate(false);
212
        mainLayout.setWidth("340px");
213
        mainLayout.setHeight("200px");
214
        mainLayout.setMargin(true);
215

    
216
        // top-level component properties
217
        setWidth("340px");
218
        setHeight("200px");
219

    
220
        // gridLayout
221
        gridLayout = buildGridLayout();
222
        mainLayout.addComponent(gridLayout);
223
        mainLayout.setExpandRatio(gridLayout, 1.0f);
224

    
225
        // horizontalLayout
226
        horizontalLayout = buildHorizontalLayout();
227
        mainLayout.addComponent(horizontalLayout);
228
        mainLayout.setComponentAlignment(horizontalLayout, new Alignment(20));
229

    
230
        // cdmProgressComponent
231
        cdmProgressComponent = new CdmProgressComponent();
232
        cdmProgressComponent.setImmediate(false);
233
        cdmProgressComponent.setWidth("-1px");
234
        cdmProgressComponent.setHeight("-1px");
235
        mainLayout.addComponent(cdmProgressComponent);
236
        mainLayout.setComponentAlignment(cdmProgressComponent, new Alignment(48));
237

    
238
        return mainLayout;
239
    }
240

    
241
    @AutoGenerated
242
    private GridLayout buildGridLayout() {
243
        // common part: create layout
244
        gridLayout = new GridLayout();
245
        gridLayout.setImmediate(false);
246
        gridLayout.setWidth("-1px");
247
        gridLayout.setHeight("-1px");
248
        gridLayout.setMargin(false);
249
        gridLayout.setSpacing(true);
250
        gridLayout.setColumns(2);
251
        gridLayout.setRows(4);
252

    
253
        // accTaxonLabel
254
        accTaxonLabel = new Label();
255
        accTaxonLabel.setImmediate(false);
256
        accTaxonLabel.setWidth("-1px");
257
        accTaxonLabel.setHeight("-1px");
258
        accTaxonLabel.setValue("Acc. Taxon : ");
259
        gridLayout.addComponent(accTaxonLabel, 0, 0);
260
        gridLayout.setComponentAlignment(accTaxonLabel, new Alignment(34));
261

    
262
        // nameLabel
263
        nameLabel = new Label();
264
        nameLabel.setImmediate(false);
265
        nameLabel.setWidth("-1px");
266
        nameLabel.setHeight("-1px");
267
        nameLabel.setValue("Name : ");
268
        gridLayout.addComponent(nameLabel, 0, 1);
269
        gridLayout.setComponentAlignment(nameLabel, new Alignment(34));
270

    
271
        // nameTextField
272
        nameTextField = new TextField();
273
        nameTextField.setImmediate(false);
274
        nameTextField.setWidth("190px");
275
        nameTextField.setHeight("-1px");
276
        nameTextField.setInvalidAllowed(false);
277
        nameTextField.setRequired(true);
278
        gridLayout.addComponent(nameTextField, 1, 1);
279
        gridLayout.setComponentAlignment(nameTextField, new Alignment(33));
280

    
281
        // secLabel
282
        secLabel = new Label();
283
        secLabel.setImmediate(false);
284
        secLabel.setWidth("-1px");
285
        secLabel.setHeight("-1px");
286
        secLabel.setValue("Secundum : ");
287
        gridLayout.addComponent(secLabel, 0, 2);
288
        gridLayout.setComponentAlignment(secLabel, new Alignment(34));
289

    
290
        // secComboBox
291
        secComboBox = new ComboBox();
292
        secComboBox.setImmediate(false);
293
        secComboBox.setWidth("190px");
294
        secComboBox.setHeight("-1px");
295
        secComboBox.setInvalidAllowed(false);
296
        secComboBox.setRequired(true);
297
        gridLayout.addComponent(secComboBox, 1, 2);
298
        gridLayout.setComponentAlignment(secComboBox, new Alignment(33));
299

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

    
309
        return gridLayout;
310
    }
311

    
312
    @AutoGenerated
313
    private HorizontalLayout buildHorizontalLayout() {
314
        // common part: create layout
315
        horizontalLayout = new HorizontalLayout();
316
        horizontalLayout.setImmediate(false);
317
        horizontalLayout.setWidth("-1px");
318
        horizontalLayout.setHeight("-1px");
319
        horizontalLayout.setMargin(false);
320
        horizontalLayout.setSpacing(true);
321

    
322
        // saveButton
323
        saveButton = new Button();
324
        saveButton.setCaption("Save");
325
        saveButton.setImmediate(true);
326
        saveButton.setWidth("-1px");
327
        saveButton.setHeight("-1px");
328
        horizontalLayout.addComponent(saveButton);
329
        horizontalLayout.setComponentAlignment(saveButton, new Alignment(20));
330

    
331
        // cancelButton
332
        cancelButton = new Button();
333
        cancelButton.setCaption("Cancel");
334
        cancelButton.setImmediate(true);
335
        cancelButton.setWidth("-1px");
336
        cancelButton.setHeight("-1px");
337
        horizontalLayout.addComponent(cancelButton);
338
        horizontalLayout.setComponentAlignment(cancelButton, new Alignment(20));
339

    
340
        return horizontalLayout;
341
    }
342

    
343

    
344
}
(5-5/6)