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
        initSecComboBox();
118
    }
119

    
120
    private void initSecComboBox() {
121

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

    
130
    private void addUIListeners() {
131
        addSaveButtonListener();
132
        addCancelButtonListener();
133
    }
134

    
135
    private void addSaveButtonListener() {
136
        saveButton.addClickListener(new Button.ClickListener() {
137

    
138
            @Override
139
            public void buttonClick(ClickEvent event) {
140
                try {
141
                    nameTextField.validate();
142
                    secComboBox.validate();
143
                } catch (EmptyValueException e) {
144
                    Notification notification = new Notification("Invalid input", "Neither Name or Secundum can be empty", Type.WARNING_MESSAGE);
145
                    notification.setDelayMsec(2000);
146
                    notification.show(Page.getCurrent());
147
                    return;
148
                }
149

    
150
                CdmVaadinUtilities.setEnabled(mainLayout, false, null);
151

    
152
                CdmVaadinUtilities.exec(new CdmVaadinOperation(1000, cdmProgressComponent) {
153
                    @Override
154
                    public boolean execute() {
155
                        setProgress("Saving Taxon " + nameTextField.getValue());
156
                        IdUuidName taxonBaseIdUuid;
157
                        if(accTaxonIdUuid == null) {
158
                            taxonBaseIdUuid = listener.newTaxon(nameTextField.getValue(),secComboBox.getValue(), classificationIdUuid.getUuid());
159
                        } else {
160
                            taxonBaseIdUuid = listener.newSynonym(nameTextField.getValue(),secComboBox.getValue(), accTaxonIdUuid.getUuid());
161
                        }
162
                        Object rowId = new RowId(taxonBaseIdUuid.getId());
163
                        registerDelayedEvent(new CdmChangeEvent(Action.Create, Arrays.asList(rowId), NewTaxonBaseComposite.class));
164
                        return true;
165
                    }
166

    
167
                    @Override
168
                    public void postOpUIUpdate(boolean success) {
169
                        if(success) {
170
                            UI.getCurrent().removeWindow(dialog);
171
                        } else {
172
                            CdmVaadinUtilities.setEnabled(mainLayout, true, null);
173
                        }
174
                    }
175
                });
176
            }
177
        });
178
    }
179

    
180
    private void addCancelButtonListener() {
181
        cancelButton.addClickListener(new Button.ClickListener() {
182

    
183
            @Override
184
            public void buttonClick(ClickEvent event) {
185
                UI.getCurrent().removeWindow(dialog);
186
            }
187

    
188
        });
189
    }
190

    
191
    /* (non-Javadoc)
192
     * @see eu.etaxonomy.cdm.vaadin.view.INewTaxonComposite#setListener(eu.etaxonomy.cdm.vaadin.view.INewTaxonBaseComponentListener)
193
     */
194
    @Override
195
    public void setListener(INewTaxonBaseComponentListener listener) {
196
        this.listener = listener;
197

    
198
    }
199

    
200
    @AutoGenerated
201
    private VerticalLayout buildMainLayout() {
202
        // common part: create layout
203
        mainLayout = new VerticalLayout();
204
        mainLayout.setImmediate(false);
205
        mainLayout.setWidth("340px");
206
        mainLayout.setHeight("200px");
207
        mainLayout.setMargin(true);
208

    
209
        // top-level component properties
210
        setWidth("340px");
211
        setHeight("200px");
212

    
213
        // gridLayout
214
        gridLayout = buildGridLayout();
215
        mainLayout.addComponent(gridLayout);
216
        mainLayout.setExpandRatio(gridLayout, 1.0f);
217

    
218
        // horizontalLayout
219
        horizontalLayout = buildHorizontalLayout();
220
        mainLayout.addComponent(horizontalLayout);
221
        mainLayout.setComponentAlignment(horizontalLayout, new Alignment(20));
222

    
223
        // cdmProgressComponent
224
        cdmProgressComponent = new CdmProgressComponent();
225
        cdmProgressComponent.setImmediate(false);
226
        cdmProgressComponent.setWidth("-1px");
227
        cdmProgressComponent.setHeight("-1px");
228
        mainLayout.addComponent(cdmProgressComponent);
229
        mainLayout.setComponentAlignment(cdmProgressComponent, new Alignment(48));
230

    
231
        return mainLayout;
232
    }
233

    
234
    @AutoGenerated
235
    private GridLayout buildGridLayout() {
236
        // common part: create layout
237
        gridLayout = new GridLayout();
238
        gridLayout.setImmediate(false);
239
        gridLayout.setWidth("-1px");
240
        gridLayout.setHeight("-1px");
241
        gridLayout.setMargin(false);
242
        gridLayout.setSpacing(true);
243
        gridLayout.setColumns(2);
244
        gridLayout.setRows(4);
245

    
246
        // accTaxonLabel
247
        accTaxonLabel = new Label();
248
        accTaxonLabel.setImmediate(false);
249
        accTaxonLabel.setWidth("-1px");
250
        accTaxonLabel.setHeight("-1px");
251
        accTaxonLabel.setValue("Acc. Taxon : ");
252
        gridLayout.addComponent(accTaxonLabel, 0, 0);
253
        gridLayout.setComponentAlignment(accTaxonLabel, new Alignment(34));
254

    
255
        // nameLabel
256
        nameLabel = new Label();
257
        nameLabel.setImmediate(false);
258
        nameLabel.setWidth("-1px");
259
        nameLabel.setHeight("-1px");
260
        nameLabel.setValue("Name : ");
261
        gridLayout.addComponent(nameLabel, 0, 1);
262
        gridLayout.setComponentAlignment(nameLabel, new Alignment(34));
263

    
264
        // nameTextField
265
        nameTextField = new TextField();
266
        nameTextField.setImmediate(false);
267
        nameTextField.setWidth("190px");
268
        nameTextField.setHeight("-1px");
269
        nameTextField.setInvalidAllowed(false);
270
        nameTextField.setRequired(true);
271
        gridLayout.addComponent(nameTextField, 1, 1);
272
        gridLayout.setComponentAlignment(nameTextField, new Alignment(33));
273

    
274
        // secLabel
275
        secLabel = new Label();
276
        secLabel.setImmediate(false);
277
        secLabel.setWidth("-1px");
278
        secLabel.setHeight("-1px");
279
        secLabel.setValue("Secundum : ");
280
        gridLayout.addComponent(secLabel, 0, 2);
281
        gridLayout.setComponentAlignment(secLabel, new Alignment(34));
282

    
283
        // secComboBox
284
        secComboBox = new ComboBox();
285
        secComboBox.setImmediate(false);
286
        secComboBox.setWidth("190px");
287
        secComboBox.setHeight("-1px");
288
        secComboBox.setInvalidAllowed(false);
289
        secComboBox.setRequired(true);
290
        gridLayout.addComponent(secComboBox, 1, 2);
291
        gridLayout.setComponentAlignment(secComboBox, new Alignment(33));
292

    
293
        // accTaxonNameValue
294
        accTaxonNameValue = new Label();
295
        accTaxonNameValue.setImmediate(false);
296
        accTaxonNameValue.setWidth("-1px");
297
        accTaxonNameValue.setHeight("-1px");
298
        accTaxonNameValue.setValue("Taxon Name");
299
        gridLayout.addComponent(accTaxonNameValue, 1, 0);
300
        gridLayout.setComponentAlignment(accTaxonNameValue, new Alignment(33));
301

    
302
        return gridLayout;
303
    }
304

    
305
    @AutoGenerated
306
    private HorizontalLayout buildHorizontalLayout() {
307
        // common part: create layout
308
        horizontalLayout = new HorizontalLayout();
309
        horizontalLayout.setImmediate(false);
310
        horizontalLayout.setWidth("-1px");
311
        horizontalLayout.setHeight("-1px");
312
        horizontalLayout.setMargin(false);
313
        horizontalLayout.setSpacing(true);
314

    
315
        // saveButton
316
        saveButton = new Button();
317
        saveButton.setCaption("Save");
318
        saveButton.setImmediate(true);
319
        saveButton.setWidth("-1px");
320
        saveButton.setHeight("-1px");
321
        horizontalLayout.addComponent(saveButton);
322
        horizontalLayout.setComponentAlignment(saveButton, new Alignment(20));
323

    
324
        // cancelButton
325
        cancelButton = new Button();
326
        cancelButton.setCaption("Cancel");
327
        cancelButton.setImmediate(true);
328
        cancelButton.setWidth("-1px");
329
        cancelButton.setHeight("-1px");
330
        horizontalLayout.addComponent(cancelButton);
331
        horizontalLayout.setComponentAlignment(cancelButton, new Alignment(20));
332

    
333
        return horizontalLayout;
334
    }
335

    
336

    
337
}
(7-7/8)