From: Artur Signell Date: Tue, 23 Aug 2016 19:03:53 +0000 (+0300) Subject: Clean up LoginForm X-Git-Tag: 8.0.0.alpha1~84 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ee5e3ec7bdf4590c22f555b013de3b9c49cbd553;p=vaadin-framework.git Clean up LoginForm Change-Id: If05d20348fc999941d7d46170937533c29e1ad1b --- diff --git a/server/src/main/java/com/vaadin/ui/LoginForm.java b/server/src/main/java/com/vaadin/ui/LoginForm.java index d5ec373a43..9e986d34fb 100644 --- a/server/src/main/java/com/vaadin/ui/LoginForm.java +++ b/server/src/main/java/com/vaadin/ui/LoginForm.java @@ -19,8 +19,8 @@ package com.vaadin.ui; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.Serializable; -import java.io.UnsupportedEncodingException; import java.lang.reflect.Method; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; @@ -28,6 +28,7 @@ import com.vaadin.server.StreamResource; import com.vaadin.shared.ui.loginform.LoginFormConstants; import com.vaadin.shared.ui.loginform.LoginFormRpc; import com.vaadin.shared.ui.loginform.LoginFormState; +import com.vaadin.util.ReflectTools; /** * Login form with auto-completion and auto-fill for all major browsers. You can @@ -57,42 +58,53 @@ import com.vaadin.shared.ui.loginform.LoginFormState; public class LoginForm extends AbstractSingleComponentContainer { /** - * This event is sent when login form is submitted. + * Event sent when the login form is submitted. */ - public static class LoginEvent extends Event { + public static class LoginEvent extends Component.Event { private Map params; - private LoginEvent(Component source, Map params) { + /** + * Creates a login event using the given source and the given + * parameters. + * + * @param source + * the source of the event + * @param params + */ + private LoginEvent(LoginForm source, Map params) { super(source); this.params = params; } + @Override + public LoginForm getSource() { + return (LoginForm) super.getSource(); + } + /** - * Access method to form values by field names. + * Gets the login parameter with the given name. * * @param name - * @return value in given field + * the name of the parameter + * @return the value of the parameter or null if no such parameter is + * present */ public String getLoginParameter(String name) { - if (params.containsKey(name)) { - return params.get(name); - } else { - return null; - } + return params.get(name); } } /** - * Login listener is a class capable to listen LoginEvents sent from - * LoginBox + * Listener triggered when a login occurs in a {@link LoginForm}. */ public interface LoginListener extends Serializable { /** - * This method is fired on each login form post. + * Event method invoked when the login button is pressed in a login + * form. * * @param event - * Login event + * the login event */ public void onLogin(LoginEvent event); } @@ -105,28 +117,13 @@ public class LoginForm extends AbstractSingleComponentContainer { implements StreamResource.StreamSource { @Override public InputStream getStream() { - try { - return new ByteArrayInputStream( - "Success".toString().getBytes("UTF-8")); - } catch (UnsupportedEncodingException e) { - return null; - } - - } - } - - static { - try { - ON_LOGIN_METHOD = LoginListener.class.getDeclaredMethod("onLogin", - new Class[] { LoginEvent.class }); - } catch (final java.lang.NoSuchMethodException e) { - // This should never happen - throw new java.lang.RuntimeException( - "Internal error finding methods in LoginForm"); + return new ByteArrayInputStream( + "Success".getBytes(StandardCharsets.UTF_8)); } } - private static final Method ON_LOGIN_METHOD; + private static final Method ON_LOGIN_METHOD = ReflectTools + .findMethod(LoginListener.class, "onLogin", LoginEvent.class); private boolean initialized; @@ -141,36 +138,36 @@ public class LoginForm extends AbstractSingleComponentContainer { * @since 7.7 */ protected TextField createUsernameField() { - checkInitialized(); + throwIfInitialized(); TextField field = new TextField(getUsernameCaption()); field.focus(); return field; } /** - * Returns the caption set with {@link #setUsernameCaption(String)}. Note - * that this method might not match what is shown to the user if + * Gets the caption set with {@link #setUsernameCaption(String)}. Note that + * this method might not match what is shown to the user if * {@link #createUsernameField()} has been overridden. * - * @return user name field caption + * @return the user name field caption */ public String getUsernameCaption() { return usernameCaption; } /** - * Set the caption of the user name field. Note that the caption can only be - * set with this method before the login form has been initialized + * Sets the caption of the user name field. Note that the caption can only + * be set with this method before the login form has been initialized * (attached). *

* As an alternative to calling this method, the method * {@link #createUsernameField()} can be overridden. * - * @param cap - * new caption + * @param usernameCaption + * the caption to set for the user name field */ - public void setUsernameCaption(String cap) { - usernameCaption = cap; + public void setUsernameCaption(String usernameCaption) { + this.usernameCaption = usernameCaption; } /** @@ -180,16 +177,17 @@ public class LoginForm extends AbstractSingleComponentContainer { * @since 7.7 */ protected PasswordField createPasswordField() { - checkInitialized(); + throwIfInitialized(); return new PasswordField(getPasswordCaption()); } /** - * Returns the caption set with {@link #setPasswordCaption(String)}. Note - * that this method might not match what is shown to the user if + * Gets the caption set with {@link #setPasswordCaption(String)}. Note that + * this method might not match what is shown to the user if * {@link #createPasswordField()} has been overridden. * - * @return password field caption + * + * @return the password field caption */ public String getPasswordCaption() { return passwordCaption; @@ -203,12 +201,11 @@ public class LoginForm extends AbstractSingleComponentContainer { * As an alternative to calling this method, the method * {@link #createPasswordField()} can be overridden. * - * @param cap - * new caption + * @param passwordCaption + * the caption for the password field */ - public void setPasswordCaption(String cap) { - passwordCaption = cap; - ; + public void setPasswordCaption(String passwordCaption) { + this.passwordCaption = passwordCaption; } /** @@ -218,34 +215,34 @@ public class LoginForm extends AbstractSingleComponentContainer { * @since 7.7 */ protected Button createLoginButton() { - checkInitialized(); + throwIfInitialized(); return new Button(getLoginButtonCaption()); } /** - * Returns the caption set with {@link #setLoginButtonCaption(String)}. Note + * Gets the caption set with {@link #setLoginButtonCaption(String)}. Note * that this method might not match what is shown to the user if * {@link #createLoginButton()} has been overridden. * - * @return login button caption + * @return the login button caption */ public String getLoginButtonCaption() { return loginButtonCaption; } /** - * Set the caption of the login button. Note that the caption can only be + * Sets the caption of the login button. Note that the caption can only be * set with this method before the login form has been initialized * (attached). *

* As an alternative to calling this method, the method * {@link #createLoginButton()} can be overridden. * - * @param cap + * @param loginButtonCaption * new caption */ - public void setLoginButtonCaption(String cap) { - loginButtonCaption = cap; + public void setLoginButtonCaption(String loginButtonCaption) { + this.loginButtonCaption = loginButtonCaption; } @Override @@ -253,13 +250,18 @@ public class LoginForm extends AbstractSingleComponentContainer { return (LoginFormState) super.getState(); } + @Override + protected LoginFormState getState(boolean markAsDirty) { + return (LoginFormState) super.getState(markAsDirty); + } + @Override public void attach() { super.attach(); init(); } - private void checkInitialized() { + private void throwIfInitialized() { if (initialized) { throw new IllegalStateException( "Already initialized. The create methods may not be called explicitly."); @@ -327,26 +329,28 @@ public class LoginForm extends AbstractSingleComponentContainer { } private TextField getUsernameField() { + assert initialized; return (TextField) getState().userNameFieldConnector; } private PasswordField getPasswordField() { + assert initialized; return (PasswordField) getState().passwordFieldConnector; } private Button getLoginButton() { + assert initialized; return (Button) getState().loginButtonConnector; } - /* - * (non-Javadoc) - * - * Handle the login. In deferred mode, this method is called after the dummy - * POST request that triggers the password manager has been completed. In - * direct mode (the default setting), it is called directly when the user - * hits the enter key or clicks on the login button. In the latter case, you - * cannot change the URL in the method or the password manager will not be - * triggered. + /** + * Handles the login. + *

+ * In deferred mode, this method is called after the dummy POST request that + * triggers the password manager has been completed. In direct mode (the + * default setting), it is called directly when the user hits the enter key + * or clicks on the login button. In the latter case, you cannot change the + * URL in the method or the password manager will not be triggered. */ private void login() { HashMap params = new HashMap(); @@ -357,39 +361,25 @@ public class LoginForm extends AbstractSingleComponentContainer { } /** - * Adds LoginListener to handle login logic + * Adds a {@link LoginListener}. + *

+ * The listener is called when the user presses the login button. * * @param listener + * the listener to add */ public void addLoginListener(LoginListener listener) { addListener(LoginEvent.class, listener, ON_LOGIN_METHOD); } /** - * @deprecated As of 7.0, replaced by - * {@link #addLoginListener(LoginListener)} - **/ - @Deprecated - public void addListener(LoginListener listener) { - addLoginListener(listener); - } - - /** - * Removes LoginListener + * Removes a {@link LoginListener}. * * @param listener + * the listener to remove */ public void removeLoginListener(LoginListener listener) { removeListener(LoginEvent.class, listener, ON_LOGIN_METHOD); } - /** - * @deprecated As of 7.0, replaced by - * {@link #removeLoginListener(LoginListener)} - **/ - @Deprecated - public void removeListener(LoginListener listener) { - removeLoginListener(listener); - } - } diff --git a/uitest/src/main/java/com/vaadin/tests/components/loginform/LoginFormTest.java b/uitest/src/main/java/com/vaadin/tests/components/loginform/LoginFormTest.java index 9379968570..100e28171e 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/loginform/LoginFormTest.java +++ b/uitest/src/main/java/com/vaadin/tests/components/loginform/LoginFormTest.java @@ -24,7 +24,7 @@ public class LoginFormTest extends TestBase { loginForm.setSizeUndefined(); updateCaption(); - loginForm.addListener(new LoginListener() { + loginForm.addLoginListener(new LoginListener() { /** * @@ -33,8 +33,7 @@ public class LoginFormTest extends TestBase { @Override public void onLogin(LoginEvent event) { - login((LoginForm) event.getSource(), - event.getLoginParameter("username"), + login(event.getSource(), event.getLoginParameter("username"), event.getLoginParameter("password")); } diff --git a/uitest/src/main/java/com/vaadin/tests/components/loginform/LoginFormUIInLoginHandler.java b/uitest/src/main/java/com/vaadin/tests/components/loginform/LoginFormUIInLoginHandler.java index 1f608cad7c..034a650eeb 100755 --- a/uitest/src/main/java/com/vaadin/tests/components/loginform/LoginFormUIInLoginHandler.java +++ b/uitest/src/main/java/com/vaadin/tests/components/loginform/LoginFormUIInLoginHandler.java @@ -12,7 +12,7 @@ public class LoginFormUIInLoginHandler extends TestBase { @Override protected void setup() { LoginForm lf = new LoginForm(); - lf.addListener(new LoginListener() { + lf.addLoginListener(new LoginListener() { @Override public void onLogin(LoginEvent event) { @@ -23,7 +23,7 @@ public class LoginFormUIInLoginHandler extends TestBase { } else { addComponent(new Label("UI.getCurrent() is null")); } - UI r2 = ((LoginForm) event.getSource()).getUI(); + UI r2 = event.getSource().getUI(); if (r2 != null) { addComponent(new Label( "event.getSource().data: " + r2.getData())); diff --git a/uitest/src/main/java/com/vaadin/tests/components/loginform/LoginFormWithMultipleWindows.java b/uitest/src/main/java/com/vaadin/tests/components/loginform/LoginFormWithMultipleWindows.java index 3a9ce202c7..cb25163d77 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/loginform/LoginFormWithMultipleWindows.java +++ b/uitest/src/main/java/com/vaadin/tests/components/loginform/LoginFormWithMultipleWindows.java @@ -21,7 +21,7 @@ public class LoginFormWithMultipleWindows extends LegacyApplication { LoginForm loginForm = new LoginForm(); loginForm.setSizeUndefined(); - loginForm.addListener(new LoginListener() { + loginForm.addLoginListener(new LoginListener() { private static final long serialVersionUID = 1L;