You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SimpleLoginView.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package com.vaadin.tests.minitutorials.v70;
  2. import com.vaadin.navigator.View;
  3. import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
  4. import com.vaadin.shared.ui.MarginInfo;
  5. import com.vaadin.ui.Alignment;
  6. import com.vaadin.ui.Button;
  7. import com.vaadin.ui.Button.ClickEvent;
  8. import com.vaadin.ui.CustomComponent;
  9. import com.vaadin.ui.VerticalLayout;
  10. import com.vaadin.ui.themes.Reindeer;
  11. import com.vaadin.v7.data.validator.LegacyAbstractValidator;
  12. import com.vaadin.v7.data.validator.LegacyEmailValidator;
  13. import com.vaadin.v7.ui.LegacyPasswordField;
  14. import com.vaadin.v7.ui.LegacyTextField;
  15. public class SimpleLoginView extends CustomComponent
  16. implements View, Button.ClickListener {
  17. public static final String NAME = "login";
  18. private final LegacyTextField user;
  19. private final LegacyPasswordField password;
  20. private final Button loginButton;
  21. public SimpleLoginView() {
  22. setSizeFull();
  23. // Create the user input field
  24. user = new LegacyTextField("User:");
  25. user.setWidth("300px");
  26. user.setRequired(true);
  27. user.setInputPrompt("Your username (eg. joe@email.com)");
  28. user.addValidator(
  29. new LegacyEmailValidator("Username must be an email address"));
  30. user.setInvalidAllowed(false);
  31. // Create the password input field
  32. password = new LegacyPasswordField("Password:");
  33. password.setWidth("300px");
  34. password.addValidator(new PasswordValidator());
  35. password.setRequired(true);
  36. password.setValue("");
  37. password.setNullRepresentation("");
  38. // Create login button
  39. loginButton = new Button("Login", this);
  40. // Add both to a panel
  41. VerticalLayout fields = new VerticalLayout(user, password, loginButton);
  42. fields.setCaption(
  43. "Please login to access the application. (test@test.com/passw0rd)");
  44. fields.setSpacing(true);
  45. fields.setMargin(new MarginInfo(true, true, true, false));
  46. fields.setSizeUndefined();
  47. // The view root layout
  48. VerticalLayout viewLayout = new VerticalLayout(fields);
  49. viewLayout.setSizeFull();
  50. viewLayout.setComponentAlignment(fields, Alignment.MIDDLE_CENTER);
  51. viewLayout.setStyleName(Reindeer.LAYOUT_BLUE);
  52. setCompositionRoot(viewLayout);
  53. }
  54. @Override
  55. public void enter(ViewChangeEvent event) {
  56. // focus the username field when user arrives to the login view
  57. user.focus();
  58. }
  59. /*
  60. * Validator for validating the passwords
  61. */
  62. private static final class PasswordValidator
  63. extends LegacyAbstractValidator<String> {
  64. public PasswordValidator() {
  65. super("The password provided is not valid");
  66. }
  67. @Override
  68. protected boolean isValidValue(String value) {
  69. /*
  70. * Password must be at least 8 characters long and contain at least
  71. * one number
  72. */
  73. if (value != null
  74. && (value.length() < 8 || !value.matches(".*\\d.*"))) {
  75. return false;
  76. }
  77. return true;
  78. }
  79. @Override
  80. public Class<String> getType() {
  81. return String.class;
  82. }
  83. }
  84. @Override
  85. public void buttonClick(ClickEvent event) {
  86. /*
  87. * Validate the fields using the navigator. By using validors for the
  88. * fields we reduce the amount of queries we have to use to the database
  89. * for wrongly entered passwords
  90. */
  91. if (!user.isValid() || !password.isValid()) {
  92. return;
  93. }
  94. String username = user.getValue();
  95. String password = this.password.getValue();
  96. /*
  97. * Validate username and password with database here. For examples sake
  98. * I use a dummy username and password.
  99. */
  100. boolean isValid = username.equals("test@test.com")
  101. && password.equals("passw0rd");
  102. if (isValid) {
  103. // Store the current user in the service session
  104. getSession().setAttribute("user", username);
  105. // Navigate to main view
  106. getUI().getNavigator().navigateTo(SimpleLoginMainView.NAME);
  107. } else {
  108. // Wrong password clear the password field and refocuses it
  109. this.password.setValue(null);
  110. this.password.focus();
  111. }
  112. }
  113. }