ref #9498 using PasswordResetService for Password Recover tab
[cdm-vaadin.git] / src / main / java / eu / etaxonomy / cdm / vaadin / view / LoginViewBean.java
1 /**
2 * Copyright (C) 2017 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.vaadin.spring.events.EventScope;
12
13 import com.vaadin.data.validator.StringLengthValidator;
14 import com.vaadin.event.ShortcutAction.KeyCode;
15 import com.vaadin.navigator.View;
16 import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
17 import com.vaadin.spring.annotation.SpringView;
18 import com.vaadin.ui.Alignment;
19 import com.vaadin.ui.Button.ClickEvent;
20 import com.vaadin.ui.VerticalLayout;
21 import com.vaadin.ui.themes.ValoTheme;
22
23 import eu.etaxonomy.cdm.vaadin.component.LoginDialog;
24 import eu.etaxonomy.cdm.vaadin.event.AuthenticationAttemptEvent;
25 import eu.etaxonomy.cdm.vaadin.event.PasswordRevoveryEvent;
26 import eu.etaxonomy.cdm.vaadin.event.RegisterNewUserEvent;
27 import eu.etaxonomy.vaadin.mvp.AbstractView;
28 import eu.etaxonomy.vaadin.ui.navigation.NavigationEvent;
29
30 /**
31 * @author a.kohlbecker
32 * @since Apr 25, 2017
33 *
34 */
35 @SpringView(name=LoginViewBean.NAME)
36 public class LoginViewBean extends AbstractView<LoginPresenter> implements LoginView, View {
37
38 private static final long serialVersionUID = 8527714663738364972L;
39
40 public static final String NAME = "login";
41
42 private LoginDialog loginDialog = new LoginDialog();
43
44 public LoginViewBean() {
45 super();
46 }
47
48 @Override
49 protected void initContent() {
50 VerticalLayout root = new VerticalLayout();
51 root.setSizeFull();
52 root.addComponent(loginDialog);
53 root.setMargin(true);
54 root.setComponentAlignment(loginDialog, Alignment.MIDDLE_CENTER);
55 setCompositionRoot(root);
56
57 loginDialog.getLoginButton().addClickListener(e -> handleLoginClick(e));
58 loginDialog.getLoginButton().setClickShortcut(KeyCode.ENTER);
59 loginDialog.getRegisterButton().addClickListener(e -> getViewEventBus().publish(EventScope.UI, this, new RegisterNewUserEvent(e)));
60 loginDialog.getSendOnetimeLogin().addClickListener(e -> {
61 getViewEventBus().publish(this, new PasswordRevoveryEvent(e));
62 });
63 // NOTE: null viewName will be replaced by the default view name in NavigationManagerBean
64 loginDialog.getCancelLoginButton().addClickListener(e -> getViewEventBus().publish(EventScope.UI, this, new NavigationEvent(null)));
65
66 loginDialog.getCancelRegistrationButton().addClickListener(e -> getViewEventBus().publish(EventScope.UI, this, new NavigationEvent(null)));
67
68 StringLengthValidator nameOrEmailValidator = new StringLengthValidator("Please enter your username or email address.");
69 loginDialog.getUserNameOrEmail().addValidator(nameOrEmailValidator);
70 loginDialog.getUserNameOrEmail().addTextChangeListener(e -> {
71 String text = e.getText();
72 logger.debug("text: " + text);
73 loginDialog.getSendOnetimeLogin().setEnabled(text != null && text.length() > 1);
74 });
75 }
76
77 private void handleLoginClick(ClickEvent e) {
78 getViewEventBus().publish(EventScope.UI, this, new AuthenticationAttemptEvent(e, loginDialog.getUserName().getValue()));
79 }
80
81 @Override
82 public LoginDialog getLoginDialog(){
83 return loginDialog;
84 }
85
86 @Override
87 // TODO pull up to AbstractView and let AbstractView implement View?
88 public void enter(ViewChangeEvent event) {
89 getPresenter().onViewEnter();
90 }
91
92 @Override
93 public void showErrorMessage(String text){
94 loginDialog.getLoginMessageLabel().setVisible(true);
95 loginDialog.getLoginMessageLabel().setStyleName(ValoTheme.BUTTON_TINY + " " + ValoTheme.LABEL_FAILURE);
96 loginDialog.getLoginMessageLabel().setValue(text);
97 }
98
99 @Override
100 public void clearMessage(){
101 loginDialog.getLoginMessageLabel().setVisible(false);
102 loginDialog.getLoginMessageLabel().setStyleName("");
103 loginDialog.getLoginMessageLabel().setValue("");
104 }
105 }