Project

General

Profile

Download (7.7 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 com.vaadin.annotations.AutoGenerated;
15
import com.vaadin.data.Validator.EmptyValueException;
16
import com.vaadin.server.Page;
17
import com.vaadin.ui.Alignment;
18
import com.vaadin.ui.Button;
19
import com.vaadin.ui.Button.ClickEvent;
20
import com.vaadin.ui.ComboBox;
21
import com.vaadin.ui.CustomComponent;
22
import com.vaadin.ui.GridLayout;
23
import com.vaadin.ui.Label;
24
import com.vaadin.ui.Notification;
25
import com.vaadin.ui.Notification.Type;
26
import com.vaadin.ui.TextField;
27
import com.vaadin.ui.UI;
28
import com.vaadin.ui.Window;
29

    
30
import eu.etaxonomy.cdm.vaadin.container.IdAndUuid;
31
import eu.etaxonomy.cdm.vaadin.session.CdmChangeEvent;
32
import eu.etaxonomy.cdm.vaadin.session.CdmChangeEvent.Action;
33
import eu.etaxonomy.cdm.vaadin.util.CdmVaadinSessionUtilities;
34
import eu.etaxonomy.cdm.vaadin.view.INewTaxonBaseComponentListener;
35
import eu.etaxonomy.cdm.vaadin.view.INewTaxonBaseComposite;
36

    
37
/**
38
 * @author cmathew
39
 * @date 2 Apr 2015
40
 *
41
 */
42
public class NewTaxonBaseComposite extends CustomComponent implements INewTaxonBaseComposite {
43

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

    
46
    @AutoGenerated
47
    private GridLayout mainLayout;
48
    @AutoGenerated
49
    private Button cancelButton;
50
    @AutoGenerated
51
    private Button saveButton;
52
    @AutoGenerated
53
    private ComboBox secComboBox;
54
    @AutoGenerated
55
    private Label secLabel;
56
    @AutoGenerated
57
    private TextField nameTextField;
58
    @AutoGenerated
59
    private Label nameLabel;
60
    private INewTaxonBaseComponentListener listener;
61

    
62

    
63
    private final Window dialog;
64
    private final IdAndUuid accTaxonIdUuid;
65
    /**
66
     * The constructor should first build the main layout, set the
67
     * composition root and then do any custom initialization.
68
     *
69
     * The constructor will not be automatically regenerated by the
70
     * visual editor.
71
     */
72
    public NewTaxonBaseComposite(Window dialog, INewTaxonBaseComponentListener listener, IdAndUuid accTaxonIdUuid) {
73
        buildMainLayout();
74
        setCompositionRoot(mainLayout);
75

    
76
        this.listener = listener;
77
        this.dialog = dialog;
78
        this.accTaxonIdUuid = accTaxonIdUuid;
79
        addUIListeners();
80

    
81
        init();
82
    }
83

    
84
    public void init() {
85
        initNameTextField();
86
        initSecComboBox();
87
    }
88

    
89
    private void initNameTextField() {
90
        //nameTextField.addValidator(new StringLengthValidator("Name cannot be empty", 1,100, false));
91
    }
92

    
93
    private void initSecComboBox() {
94

    
95
        secComboBox.setNullSelectionAllowed(false);
96
        secComboBox.setItemCaptionPropertyId("titleCache");
97
        secComboBox.setImmediate(true);
98
        if(listener != null) {
99
            secComboBox.setContainerDataSource(listener.getSecRefContainer());
100
        }
101
    }
102

    
103
    private void addUIListeners() {
104
        addSaveButtonListener();
105
        addCancelButtonListener();
106
    }
107

    
108
    private void addSaveButtonListener() {
109
        saveButton.addClickListener(new Button.ClickListener() {
110

    
111
            @Override
112
            public void buttonClick(ClickEvent event) {
113
                try {
114
                    nameTextField.validate();
115
                    secComboBox.validate();
116

    
117
                    IdAndUuid taxonIdUuid;
118
                    if(accTaxonIdUuid == null) {
119
                        taxonIdUuid = listener.newTaxon(nameTextField.getValue(),secComboBox.getValue());
120
                    } else {
121
                        listener.newSynonym(nameTextField.getValue(),secComboBox.getValue(), accTaxonIdUuid.getUuid());
122
                        taxonIdUuid = accTaxonIdUuid;
123
                    }
124
                    CdmVaadinSessionUtilities.getCurrentCdmDataChangeService()
125
                        .fireChangeEvent(new CdmChangeEvent(Action.Create, Arrays.asList(taxonIdUuid.getId()), NewTaxonBaseComposite.class), true);
126
                    UI.getCurrent().removeWindow(dialog);
127
                } catch (EmptyValueException e) {
128
                    Notification notification = new Notification("Invalid input", "Neither Name or Secundum can be empty", Type.WARNING_MESSAGE);
129
                    notification.setDelayMsec(2000);
130
                    notification.show(Page.getCurrent());
131
                }
132
            }
133

    
134
        });
135
    }
136

    
137
    private void addCancelButtonListener() {
138
        cancelButton.addClickListener(new Button.ClickListener() {
139

    
140
            @Override
141
            public void buttonClick(ClickEvent event) {
142
               UI.getCurrent().removeWindow(dialog);
143
            }
144

    
145
        });
146
    }
147

    
148
    /* (non-Javadoc)
149
     * @see eu.etaxonomy.cdm.vaadin.view.INewTaxonComposite#setListener(eu.etaxonomy.cdm.vaadin.view.INewTaxonBaseComponentListener)
150
     */
151
    @Override
152
    public void setListener(INewTaxonBaseComponentListener listener) {
153
        this.listener = listener;
154

    
155
    }
156

    
157
    @AutoGenerated
158
    private GridLayout buildMainLayout() {
159
        // common part: create layout
160
        mainLayout = new GridLayout();
161
        mainLayout.setImmediate(false);
162
        mainLayout.setWidth("320px");
163
        mainLayout.setHeight("120px");
164
        mainLayout.setMargin(true);
165
        mainLayout.setSpacing(true);
166
        mainLayout.setColumns(2);
167
        mainLayout.setRows(3);
168

    
169
        // top-level component properties
170
        setWidth("320px");
171
        setHeight("120px");
172

    
173
        // nameLabel
174
        nameLabel = new Label();
175
        nameLabel.setImmediate(false);
176
        nameLabel.setWidth("-1px");
177
        nameLabel.setHeight("-1px");
178
        nameLabel.setValue("Name : ");
179
        mainLayout.addComponent(nameLabel, 0, 0);
180
        mainLayout.setComponentAlignment(nameLabel, new Alignment(34));
181

    
182
        // nameTextField
183
        nameTextField = new TextField();
184
        nameTextField.setImmediate(false);
185
        nameTextField.setWidth("190px");
186
        nameTextField.setHeight("-1px");
187
        nameTextField.setInvalidAllowed(false);
188
        nameTextField.setRequired(true);
189
        mainLayout.addComponent(nameTextField, 1, 0);
190
        mainLayout.setComponentAlignment(nameTextField, new Alignment(33));
191

    
192
        // secLabel
193
        secLabel = new Label();
194
        secLabel.setImmediate(false);
195
        secLabel.setWidth("-1px");
196
        secLabel.setHeight("-1px");
197
        secLabel.setValue("Secundum : ");
198
        mainLayout.addComponent(secLabel, 0, 1);
199
        mainLayout.setComponentAlignment(secLabel, new Alignment(34));
200

    
201
        // secComboBox
202
        secComboBox = new ComboBox();
203
        secComboBox.setImmediate(false);
204
        secComboBox.setWidth("190px");
205
        secComboBox.setHeight("-1px");
206
        secComboBox.setInvalidAllowed(false);
207
        secComboBox.setRequired(true);
208
        mainLayout.addComponent(secComboBox, 1, 1);
209
        mainLayout.setComponentAlignment(secComboBox, new Alignment(33));
210

    
211
        // saveButton
212
        saveButton = new Button();
213
        saveButton.setCaption("Save");
214
        saveButton.setImmediate(true);
215
        saveButton.setWidth("-1px");
216
        saveButton.setHeight("-1px");
217
        mainLayout.addComponent(saveButton, 0, 2);
218
        mainLayout.setComponentAlignment(saveButton, new Alignment(34));
219

    
220
        // cancelButton
221
        cancelButton = new Button();
222
        cancelButton.setCaption("Cancel");
223
        cancelButton.setImmediate(true);
224
        cancelButton.setWidth("-1px");
225
        cancelButton.setHeight("-1px");
226
        mainLayout.addComponent(cancelButton, 1, 2);
227
        mainLayout.setComponentAlignment(cancelButton, new Alignment(33));
228

    
229
        return mainLayout;
230
    }
231

    
232

    
233
}
(2-2/3)