2 * Copyright (C) 2021 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.vaadin
.view
;
11 import org
.apache
.commons
.lang3
.StringUtils
;
12 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
13 import org
.vaadin
.spring
.events
.EventBus
;
15 import com
.vaadin
.data
.validator
.RegexpValidator
;
16 import com
.vaadin
.navigator
.View
;
17 import com
.vaadin
.navigator
.ViewChangeListener
.ViewChangeEvent
;
18 import com
.vaadin
.spring
.annotation
.SpringView
;
19 import com
.vaadin
.ui
.Alignment
;
20 import com
.vaadin
.ui
.Button
;
21 import com
.vaadin
.ui
.FormLayout
;
22 import com
.vaadin
.ui
.Label
;
23 import com
.vaadin
.ui
.PasswordField
;
24 import com
.vaadin
.ui
.TextField
;
25 import com
.vaadin
.ui
.VerticalLayout
;
26 import com
.vaadin
.ui
.themes
.ValoTheme
;
28 import eu
.etaxonomy
.cdm
.vaadin
.data
.validator
.PasswordsMatchValidator
;
29 import eu
.etaxonomy
.cdm
.vaadin
.data
.validator
.PasswordsPolicyValidator
;
30 import eu
.etaxonomy
.cdm
.vaadin
.event
.UserAccountEvent
;
31 import eu
.etaxonomy
.vaadin
.mvp
.AbstractView
;
34 * @author a.kohlbecker
37 @SpringView(name
=AccountRegistrationViewBean
.NAME
)
38 public class AccountRegistrationViewBean
39 extends AbstractView
<AccountRegistrationView
,AccountRegistrationPresenter
>
40 implements AccountRegistrationView
, View
{
42 private static final long serialVersionUID
= 2066153054170511405L;
45 private EventBus
.UIEventBus uiEventBus
;
47 public static final String NAME
= "accountCreation";
49 private Label header
= new Label();
51 private TextField emailAddress
= new TextField("Email address");
53 private TextField userName
= new TextField("User name");
55 private PasswordField password1Field
= new PasswordField ("Password");
57 private PasswordField password2Field
= new PasswordField("Repeat password");
59 private TextField prefix
= new TextField("Name prefix");
61 private TextField familyName
= new TextField("Family name");
63 private TextField givenName
= new TextField("Given name");
65 private Button registerButton
= new Button("Register user account");
67 private Label messageLabel
= new Label();
70 protected void initContent() {
71 VerticalLayout root
= new VerticalLayout();
73 FormLayout formLayout
= new FormLayout();
74 formLayout
.addComponents(header
, userName
, password1Field
, password2Field
, prefix
, givenName
, familyName
, registerButton
, messageLabel
);
75 formLayout
.setComponentAlignment(messageLabel
, Alignment
.MIDDLE_CENTER
);
76 formLayout
.setMargin(true);
77 formLayout
.setSizeUndefined();
79 header
.setValue("Register a user account");
80 header
.setStyleName(ValoTheme
.LABEL_H3
);
81 registerButton
.setStyleName(ValoTheme
.BUTTON_PRIMARY
);
82 registerButton
.setEnabled(false);
83 registerButton
.setWidth(100, Unit
.PERCENTAGE
);
84 registerButton
.addClickListener(e
-> {
85 getViewEventBus().publish(this, new UserAccountEvent(UserAccountEvent
.UserAccountAction
.REGISTER_ACCOUNT
, e
));
87 messageLabel
.setCaptionAsHtml(true);
88 messageLabel
.setVisible(false);
90 setCompositionRoot(root
);
91 root
.addComponent(formLayout
);
92 root
.setComponentAlignment(formLayout
, Alignment
.MIDDLE_CENTER
);
96 userName
.setRequired(true);
97 password1Field
.setRequired(true);
98 password2Field
.setRequired(true);
100 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."));
101 password1Field
.addValidator(new PasswordsPolicyValidator());
102 password2Field
.addValidator(new PasswordsMatchValidator("The passwords are not identical.", password1Field
, password2Field
));
103 password1Field
.addValueChangeListener(e
-> updateResetButtonState());
104 password2Field
.addValueChangeListener(e
-> updateResetButtonState());
106 // value will be set by presenter and user must not change it here
107 emailAddress
.setEnabled(false);
108 emailAddress
.setReadOnly(true);
111 prefix
.setInputPrompt("Dr., Prof, ...");
114 private void updateResetButtonState() {
115 registerButton
.setEnabled(StringUtils
.isNoneBlank(password1Field
.getValue()) && password1Field
.getErrorMessage() == null && password2Field
.getErrorMessage() == null);
119 // TODO pull up to AbstractView and let AbstractView implement View?
120 public void enter(ViewChangeEvent event
) {
121 getPresenter().onViewEnter();
125 public void showErrorMessage(String text
) {
126 messageLabel
.setValue(text
);
127 messageLabel
.setStyleName(ValoTheme
.LABEL_FAILURE
);
128 messageLabel
.setVisible(true);
133 public void showSuccessMessage(String text
) {
134 messageLabel
.setValue(text
);
135 messageLabel
.setStyleName(ValoTheme
.LABEL_SUCCESS
);
136 messageLabel
.setVisible(true);
140 public void disableForm() {
141 password1Field
.setEnabled(false);
142 password2Field
.setEnabled(false);
143 registerButton
.setEnabled(false);
147 public TextField
getUserName() {
152 public PasswordField
getPassword1Field() {
153 return password1Field
;
157 public TextField
getEmailAddress() {
162 public TextField
getPrefix() {
167 public TextField
getFamilyName() {
172 public TextField
getGivenName() {