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.

LoginFormUI.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package com.vaadin.tests.components.loginform;
  2. import java.util.Optional;
  3. import com.vaadin.server.VaadinRequest;
  4. import com.vaadin.tests.components.AbstractReindeerTestUI;
  5. import com.vaadin.ui.Button;
  6. import com.vaadin.ui.Button.ClickListener;
  7. import com.vaadin.ui.HorizontalLayout;
  8. import com.vaadin.ui.Label;
  9. import com.vaadin.ui.LoginForm;
  10. import com.vaadin.ui.VerticalLayout;
  11. public class LoginFormUI extends AbstractReindeerTestUI {
  12. private HorizontalLayout loginFormLayout;
  13. protected LoginForm loginForm;
  14. @Override
  15. protected void setup(VaadinRequest request) {
  16. loginFormLayout = new HorizontalLayout();
  17. loginForm = new LoginForm();
  18. loginForm.setSizeUndefined();
  19. getUsernameCaption().ifPresent(loginForm::setUsernameCaption);
  20. getPasswordCaption().ifPresent(loginForm::setPasswordCaption);
  21. getLoginCaption().ifPresent(loginForm::setLoginButtonCaption);
  22. updateCaption();
  23. loginForm.addLoginListener(event -> login(event.getSource(),
  24. event.getLoginParameter("username"),
  25. event.getLoginParameter("password")));
  26. loginFormLayout.addComponent(loginForm);
  27. Button changeWidth = new Button("Change width",
  28. (ClickListener) event -> {
  29. if (loginForm.getWidth() < 0) {
  30. loginForm.setWidth("300px");
  31. } else {
  32. loginForm.setWidth(null);
  33. }
  34. updateCaption();
  35. });
  36. Button changeHeight = new Button("Change height",
  37. (ClickListener) event -> {
  38. if (loginForm.getHeight() < 0) {
  39. loginForm.setHeight("200px");
  40. } else {
  41. loginForm.setHeight(null);
  42. }
  43. updateCaption();
  44. });
  45. addComponent(loginFormLayout);
  46. addComponent(changeWidth);
  47. addComponent(changeHeight);
  48. }
  49. protected Optional<String> getUsernameCaption() {
  50. return Optional.empty();
  51. }
  52. protected Optional<String> getPasswordCaption() {
  53. return Optional.empty();
  54. }
  55. protected Optional<String> getLoginCaption() {
  56. return Optional.empty();
  57. }
  58. protected void updateCaption() {
  59. float width = loginForm.getWidth();
  60. float height = loginForm.getHeight();
  61. String w = width < 0 ? "auto" : (int) width + "px";
  62. String h = height < 0 ? "auto" : (int) height + "px";
  63. loginForm.setCaption("LoginForm (" + w + "/" + h + ")");
  64. }
  65. protected void login(LoginForm loginForm, String user, String password) {
  66. VerticalLayout infoLayout = new VerticalLayout();
  67. Label info = new Label(
  68. "User '" + user + "', password='" + password + "' logged in");
  69. info.setId("info");
  70. Button logoutButton = new Button("Log out", (ClickListener) event -> {
  71. Button b = event.getButton();
  72. loginFormLayout.replaceComponent(b.getParent(),
  73. (LoginForm) b.getData());
  74. });
  75. logoutButton.setData(loginForm);
  76. infoLayout.addComponent(info);
  77. infoLayout.addComponent(logoutButton);
  78. loginFormLayout.replaceComponent(loginForm, infoLayout);
  79. }
  80. @Override
  81. protected String getTestDescription() {
  82. return "Basic test for the LoginForm component.";
  83. }
  84. @Override
  85. protected Integer getTicketNumber() {
  86. return 3597;
  87. }
  88. }