Project

General

Profile

Download (5.79 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2021 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.view;
10

    
11
import org.apache.commons.lang3.StringUtils;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.core.env.Environment;
14
import org.vaadin.spring.events.EventBus;
15

    
16
import com.vaadin.data.validator.RegexpValidator;
17
import com.vaadin.navigator.View;
18
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
19
import com.vaadin.spring.annotation.SpringView;
20
import com.vaadin.ui.Alignment;
21
import com.vaadin.ui.Button;
22
import com.vaadin.ui.FormLayout;
23
import com.vaadin.ui.Label;
24
import com.vaadin.ui.PasswordField;
25
import com.vaadin.ui.TextField;
26
import com.vaadin.ui.VerticalLayout;
27
import com.vaadin.ui.themes.ValoTheme;
28

    
29
import eu.etaxonomy.cdm.vaadin.data.validator.PasswordsMatchValidator;
30
import eu.etaxonomy.cdm.vaadin.data.validator.PasswordsPolicyValidator;
31
import eu.etaxonomy.cdm.vaadin.event.UserAccountEvent;
32
import eu.etaxonomy.vaadin.mvp.AbstractView;
33

    
34
/**
35
 * @author a.kohlbecker
36
 * @since Nov 11, 2021
37
 */
38
@SpringView(name=AccountRegistrationViewBean.NAME)
39
public class AccountRegistrationViewBean extends AbstractView<AccountRegistrationPresenter> implements AccountRegistrationView, View  {
40

    
41
    private static final long serialVersionUID = 2066153054170511405L;
42

    
43
    @Autowired
44
    private EventBus.UIEventBus uiEventBus;
45

    
46
    @Autowired
47
    private Environment env;
48

    
49
    public static final String NAME = "accountCreation";
50

    
51
    private Label header = new Label();
52

    
53
    private TextField emailAddress = new TextField("Email address");
54

    
55
    private TextField userName = new TextField("User name");
56

    
57
    private PasswordField password1Field = new PasswordField ("Password");
58

    
59
    private PasswordField password2Field = new PasswordField("Repeat password");
60

    
61
    private TextField prefix = new TextField("Name prefix");
62

    
63
    private TextField familyName = new TextField("Family name");
64

    
65
    private TextField givenName = new TextField("Given name");
66

    
67
    private Button registerButton = new Button("Register user account");
68

    
69
    private Label messageLabel = new Label();
70

    
71
    @Override
72
    protected void initContent() {
73
        VerticalLayout root = new VerticalLayout();
74

    
75
        FormLayout formLayout = new FormLayout();
76
        formLayout.addComponents(header, userName, password1Field, password2Field, prefix, givenName, familyName, registerButton, messageLabel);
77
        formLayout.setComponentAlignment(messageLabel, Alignment.MIDDLE_CENTER);
78
        formLayout.setMargin(true);
79
        formLayout.setSizeUndefined();
80

    
81
        header.setValue("Register a user account");
82
        header.setStyleName(ValoTheme.LABEL_H3);
83
        registerButton.setStyleName(ValoTheme.BUTTON_PRIMARY);
84
        registerButton.setEnabled(false);
85
        registerButton.setWidth(100, Unit.PERCENTAGE);
86
        registerButton.addClickListener(e -> {
87
            getViewEventBus().publish(this, new UserAccountEvent(UserAccountEvent.UserAccountAction.REGISTER_ACCOUNT, e));
88
        });
89
        messageLabel.setCaptionAsHtml(true);
90
        messageLabel.setVisible(false);
91

    
92
        setCompositionRoot(root);
93
        root.addComponent(formLayout);
94
        root.setComponentAlignment(formLayout, Alignment.MIDDLE_CENTER);
95

    
96
        root.setSizeFull();
97

    
98
        userName.setRequired(true);
99
        password1Field.setRequired(true);
100
        password2Field.setRequired(true);
101

    
102
        userName.addValidator(new RegexpValidator("[A-Za-z0-9_\\.\\-]{3,}", true, "User names may contain the characters \"A-Z a-z 0-9 _ . -\", and must be at least 3 characters long."));
103
        password1Field.addValidator(new PasswordsPolicyValidator());
104
        password2Field.addValidator(new PasswordsMatchValidator("The passwords are not identical.", password1Field, password2Field));
105
        password1Field.addValueChangeListener(e -> updateResetButtonState());
106
        password2Field.addValueChangeListener(e -> updateResetButtonState());
107

    
108
        // value will be set by presenter and user must not change it here
109
        emailAddress.setEnabled(false);
110
        emailAddress.setReadOnly(true);
111

    
112

    
113
        prefix.setInputPrompt("Dr., Prof, ...");
114
    }
115

    
116
    private void updateResetButtonState() {
117
        registerButton.setEnabled(StringUtils.isNoneBlank(password1Field.getValue()) && password1Field.getErrorMessage() == null && password2Field.getErrorMessage() == null);
118
    }
119

    
120
    @Override
121
    // TODO pull up to AbstractView and let AbstractView implement View?
122
    public void enter(ViewChangeEvent event) {
123
        getPresenter().onViewEnter();
124
    }
125

    
126
    @Override
127
    public void showErrorMessage(String text) {
128
        messageLabel.setValue(text);
129
        messageLabel.setStyleName(ValoTheme.LABEL_FAILURE);
130
        messageLabel.setVisible(true);
131
        disableForm();
132
    }
133

    
134
    @Override
135
    public void showSuccessMessage(String text) {
136
        messageLabel.setValue(text);
137
        messageLabel.setStyleName(ValoTheme.LABEL_SUCCESS);
138
        messageLabel.setVisible(true);
139
        disableForm();
140
    }
141

    
142
    public void disableForm() {
143
        password1Field.setEnabled(false);
144
        password2Field.setEnabled(false);
145
        registerButton.setEnabled(false);
146
    }
147

    
148
    @Override
149
    public TextField getUserName() {
150
        return userName;
151
    }
152

    
153
    @Override
154
    public PasswordField getPassword1Field() {
155
        return password1Field;
156
    }
157

    
158
     @Override
159
    public TextField getEmailAddress() {
160
        return emailAddress;
161
    }
162

    
163
     @Override
164
     public TextField getPrefix() {
165
        return prefix;
166
    }
167

    
168
     @Override
169
     public TextField getFamilyName() {
170
        return familyName;
171
    }
172

    
173
     @Override
174
     public TextField getGivenName() {
175
        return givenName;
176
    }
177

    
178

    
179
}
(4-4/18)