Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.karmasms;
- import com.karmasms.model.KarmaUser;
- import com.karmasms.restclient.ApiClient;
- import com.vaadin.data.validator.EmailValidator;
- import com.vaadin.data.validator.NullValidator;
- import com.vaadin.server.FontAwesome;
- import com.vaadin.ui.Button;
- import com.vaadin.ui.FormLayout;
- import com.vaadin.ui.HorizontalLayout;
- import com.vaadin.ui.Panel;
- import com.vaadin.ui.PasswordField;
- import com.vaadin.ui.TextField;
- import com.vaadin.ui.themes.ValoTheme;
- /**
- *
- * @author bashizip
- *
- */
- public class LoginForm extends HorizontalLayout {
- private final ILoginStateListener loginStateListner;
- TextField tfUsername;
- PasswordField tfPassword;
- FormLayout loginLayout;
- Panel panel;
- Button passwordForgotten;
- public LoginForm(ILoginStateListener loginStateListner) {
- setMargin(true);
- this.loginStateListner = loginStateListner;
- panel = new Panel("Login");
- loginLayout = new FormLayout();
- loginLayout.setMargin(true);
- loginLayout.setImmediate(true);
- tfUsername = new TextField("Email");
- tfPassword = new PasswordField("Mot de Passe");
- tfUsername.setIcon(FontAwesome.USER);
- tfPassword.setIcon(FontAwesome.LOCK);
- passwordForgotten = new Button("Mot de passe oublié?");
- passwordForgotten.setStyleName(ValoTheme.BUTTON_LINK);
- passwordForgotten.addClickListener(new Button.ClickListener() {
- @Override
- public void buttonClick(Button.ClickEvent event) {
- doLogin();
- }
- });
- Button btnLogin = new Button("Connection", new LoginClickListener());
- btnLogin.setIcon(FontAwesome.CHECK);
- btnLogin.setStyleName(ValoTheme.BUTTON_PRIMARY);
- btnLogin.setStyleName(ValoTheme.BUTTON_FRIENDLY);
- loginLayout.setSpacing(true);
- loginLayout.addComponents(tfUsername, tfPassword, btnLogin);
- loginLayout.addComponent(passwordForgotten);
- validateValues();
- panel.setContent(loginLayout);
- addComponent(panel);
- }
- class LoginClickListener implements Button.ClickListener {
- @Override
- public void buttonClick(Button.ClickEvent event) {
- if (!tfUsername.isValid() || !tfPassword.isValid()) {
- return;
- }
- doLogin();
- }
- }
- void doLogin() {
- KarmaUser userToLogin = new KarmaUser(tfUsername.getValue());
- userToLogin.setPassword(tfPassword.getValue());
- KarmaUser res = null;
- try {
- res = ApiClient.getMyApiServiceClient().login(userToLogin);
- } catch (Exception e) {
- }
- if (res != null) {
- if (res.getRepresentant() != null) {
- if (!res.getRepresentant().equals("")) {
- loginStateListner.onLoginSuccessFull(res);
- }
- } else {
- loginStateListner.onWrongPassword();
- }
- }
- }
- public interface ILoginStateListener {
- void onLoginSuccessFull(KarmaUser user);
- void onWrongPassword();
- void onAccountNotFound();
- }
- private void validateValues() {
- tfUsername.addValidator(new EmailValidator("Wrong Email Adress"));
- tfUsername.addValidator(new NullValidator("Username canot be null", false));
- tfUsername.setRequired(true);
- tfPassword.setRequired(true);
- tfUsername.setImmediate(true);
- tfPassword.setImmediate(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement