2 * Copyright (C) 2017 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
.vaadin
.spring
.events
.EventScope
;
14 import com
.vaadin
.data
.validator
.EmailValidator
;
15 import com
.vaadin
.data
.validator
.StringLengthValidator
;
16 import com
.vaadin
.event
.ShortcutAction
.KeyCode
;
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
.ClickEvent
;
22 import com
.vaadin
.ui
.VerticalLayout
;
23 import com
.vaadin
.ui
.themes
.ValoTheme
;
25 import eu
.etaxonomy
.cdm
.vaadin
.component
.LoginDialog
;
26 import eu
.etaxonomy
.cdm
.vaadin
.event
.AuthenticationAttemptEvent
;
27 import eu
.etaxonomy
.cdm
.vaadin
.event
.UserAccountEvent
;
28 import eu
.etaxonomy
.vaadin
.mvp
.AbstractView
;
29 import eu
.etaxonomy
.vaadin
.ui
.navigation
.NavigationEvent
;
32 * @author a.kohlbecker
36 @SpringView(name
=LoginViewBean
.NAME
)
37 public class LoginViewBean
extends AbstractView
<LoginPresenter
> implements LoginView
, View
{
39 private static final long serialVersionUID
= 8527714663738364972L;
41 public static final String NAME
= "login";
43 private LoginDialog loginDialog
= new LoginDialog();
45 public LoginViewBean() {
50 protected void initContent() {
51 VerticalLayout root
= new VerticalLayout();
53 root
.addComponent(loginDialog
);
55 root
.setComponentAlignment(loginDialog
, Alignment
.MIDDLE_CENTER
);
56 setCompositionRoot(root
);
59 loginDialog
.getLoginButton().addClickListener(e
-> handleLoginClick(e
));
60 loginDialog
.getLoginButton().setClickShortcut(KeyCode
.ENTER
);
61 // NOTE: null viewName will be replaced by the default view name in NavigationManagerBean
62 loginDialog
.getCancelLoginButton().addClickListener(e
-> getViewEventBus().publish(EventScope
.UI
, this, new NavigationEvent(null)));
65 loginDialog
.getRegisterButton().addClickListener(e
-> {
66 getViewEventBus().publish(this, new UserAccountEvent(UserAccountEvent
.UserAccountAction
.REGISTER_ACCOUNT
, e
));
69 loginDialog
.getCancelRegistrationButton().addClickListener(e
-> getViewEventBus().publish(EventScope
.UI
, this, new NavigationEvent(null)));
70 loginDialog
.getEmail().addValidator(new EmailValidator("The enterd E-mail address is not valid."));
71 loginDialog
.getEmail().addValueChangeListener(e
-> updateRegisterButtonState());
72 // further validators added in the presenter
74 // --- Password reset tab
75 StringLengthValidator nameOrEmailValidator
= new StringLengthValidator("Please enter your username or email address.");
76 loginDialog
.getUserNameOrEmail().addValidator(nameOrEmailValidator
);
77 loginDialog
.getUserNameOrEmail().addTextChangeListener(e
-> {
78 String text
= e
.getText();
79 loginDialog
.getSendOnetimeLogin().setEnabled(text
!= null && text
.length() > 1);
81 loginDialog
.getSendOnetimeLogin().addClickListener(e
-> {
82 getViewEventBus().publish(this, new UserAccountEvent(UserAccountEvent
.UserAccountAction
.REQUEST_PASSWORD_RESET
,e
));
86 private void updateRegisterButtonState() {
87 loginDialog
.getRegisterButton().setEnabled(StringUtils
.isNoneBlank(loginDialog
.getEmail().getValue()) && loginDialog
.getEmail().getErrorMessage() == null);
90 private void handleLoginClick(ClickEvent e
) {
91 getViewEventBus().publish(EventScope
.UI
, this, new AuthenticationAttemptEvent(e
, loginDialog
.getUserName().getValue()));
95 public LoginDialog
getLoginDialog(){
100 // TODO pull up to AbstractView and let AbstractView implement View?
101 public void enter(ViewChangeEvent event
) {
102 getPresenter().onViewEnter();
106 public void showErrorMessage(String text
){
107 loginDialog
.getLoginMessageLabel().setVisible(true);
108 loginDialog
.getLoginMessageLabel().setStyleName(ValoTheme
.BUTTON_TINY
+ " " + ValoTheme
.LABEL_FAILURE
);
109 loginDialog
.getLoginMessageLabel().setValue(text
);
113 public void clearMessage(){
114 loginDialog
.getLoginMessageLabel().setVisible(false);
115 loginDialog
.getLoginMessageLabel().setStyleName("");
116 loginDialog
.getLoginMessageLabel().setValue("");