Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. package com.karmasms;
  2.  
  3. import com.karmasms.model.KarmaUser;
  4. import com.karmasms.restclient.ApiClient;
  5. import com.vaadin.data.validator.EmailValidator;
  6. import com.vaadin.data.validator.NullValidator;
  7. import com.vaadin.server.FontAwesome;
  8. import com.vaadin.ui.Button;
  9. import com.vaadin.ui.FormLayout;
  10. import com.vaadin.ui.HorizontalLayout;
  11. import com.vaadin.ui.Panel;
  12. import com.vaadin.ui.PasswordField;
  13. import com.vaadin.ui.TextField;
  14. import com.vaadin.ui.themes.ValoTheme;
  15.  
  16. /**
  17. *
  18. * @author bashizip
  19. *
  20. */
  21. public class LoginForm extends HorizontalLayout {
  22.  
  23. private final ILoginStateListener loginStateListner;
  24.  
  25. TextField tfUsername;
  26. PasswordField tfPassword;
  27. FormLayout loginLayout;
  28. Panel panel;
  29. Button passwordForgotten;
  30.  
  31. public LoginForm(ILoginStateListener loginStateListner) {
  32.  
  33. setMargin(true);
  34.  
  35. this.loginStateListner = loginStateListner;
  36. panel = new Panel("Login");
  37. loginLayout = new FormLayout();
  38. loginLayout.setMargin(true);
  39. loginLayout.setImmediate(true);
  40.  
  41. tfUsername = new TextField("Email");
  42. tfPassword = new PasswordField("Mot de Passe");
  43. tfUsername.setIcon(FontAwesome.USER);
  44. tfPassword.setIcon(FontAwesome.LOCK);
  45.  
  46. passwordForgotten = new Button("Mot de passe oublié?");
  47. passwordForgotten.setStyleName(ValoTheme.BUTTON_LINK);
  48.  
  49. passwordForgotten.addClickListener(new Button.ClickListener() {
  50.  
  51. @Override
  52. public void buttonClick(Button.ClickEvent event) {
  53. doLogin();
  54. }
  55. });
  56.  
  57. Button btnLogin = new Button("Connection", new LoginClickListener());
  58. btnLogin.setIcon(FontAwesome.CHECK);
  59. btnLogin.setStyleName(ValoTheme.BUTTON_PRIMARY);
  60. btnLogin.setStyleName(ValoTheme.BUTTON_FRIENDLY);
  61.  
  62. loginLayout.setSpacing(true);
  63. loginLayout.addComponents(tfUsername, tfPassword, btnLogin);
  64. loginLayout.addComponent(passwordForgotten);
  65.  
  66. validateValues();
  67. panel.setContent(loginLayout);
  68. addComponent(panel);
  69.  
  70. }
  71.  
  72. class LoginClickListener implements Button.ClickListener {
  73.  
  74. @Override
  75. public void buttonClick(Button.ClickEvent event) {
  76. if (!tfUsername.isValid() || !tfPassword.isValid()) {
  77. return;
  78. }
  79. doLogin();
  80. }
  81.  
  82. }
  83.  
  84. void doLogin() {
  85.  
  86. KarmaUser userToLogin = new KarmaUser(tfUsername.getValue());
  87. userToLogin.setPassword(tfPassword.getValue());
  88. KarmaUser res = null;
  89.  
  90. try {
  91. res = ApiClient.getMyApiServiceClient().login(userToLogin);
  92. } catch (Exception e) {
  93. }
  94. if (res != null) {
  95.  
  96. if (res.getRepresentant() != null) {
  97.  
  98. if (!res.getRepresentant().equals("")) {
  99. loginStateListner.onLoginSuccessFull(res);
  100. }
  101. } else {
  102. loginStateListner.onWrongPassword();
  103. }
  104. }
  105.  
  106. }
  107.  
  108. public interface ILoginStateListener {
  109.  
  110. void onLoginSuccessFull(KarmaUser user);
  111.  
  112. void onWrongPassword();
  113.  
  114. void onAccountNotFound();
  115. }
  116.  
  117. private void validateValues() {
  118.  
  119. tfUsername.addValidator(new EmailValidator("Wrong Email Adress"));
  120. tfUsername.addValidator(new NullValidator("Username canot be null", false));
  121. tfUsername.setRequired(true);
  122. tfPassword.setRequired(true);
  123. tfUsername.setImmediate(true);
  124. tfPassword.setImmediate(true);
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement