/*
 * Copyright 2002-2004 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.springframework.richclient.security;

import javax.swing.JComponent;

import net.sf.acegisecurity.Authentication;
import net.sf.acegisecurity.providers.UsernamePasswordAuthenticationToken;

import org.springframework.richclient.form.AbstractForm;
import org.springframework.richclient.form.FormModelHelper;
import org.springframework.richclient.form.builder.TableFormBuilder;

/**
 * This class provides a simple form for capturing a username and password from the user.
 * It also generates an {@link Authentication} token from the entered values.
 * 
 * @author Larry Streepy
 * @see #getAuthentication()
 */
public class LoginForm extends AbstractForm {
    private static final String FORM_ID = "credentials";

    private LoginDetails loginDetails;

    private JComponent usernameField;
    private JComponent passwordField;

    /**
     * Constructor.
     */
    public LoginForm() {
        super( FORM_ID );

        loginDetails = createLoginDetails();
        setFormModel( FormModelHelper.createUnbufferedFormModel( loginDetails ) );
    }

    /**
     * Set the user name in the form.
     * @param userName to install
     */
    public void setUserName(String userName) {
        if( isControlCreated() ) {
            getValueModel( LoginDetails.PROPERTY_USERNAME ).setValue( userName );
        } else {
            loginDetails.setUsername( userName );
        }
    }

    /**
     * Get an Authentication token that contains the current username and password.
     * @return authentication token
     */
    public Authentication getAuthentication() {
        return new UsernamePasswordAuthenticationToken( loginDetails.getUsername(), loginDetails.getPassword() );
    }

    /**
     * Create the form object to hold our login information.
     * @return constructed form object
     */
    protected LoginDetails createLoginDetails() {
        return new LoginDetails();
    }

    /**
     * Construct the form with the username and password fields.
     */
    protected JComponent createFormControl() {
        TableFormBuilder formBuilder = new TableFormBuilder( getBindingFactory() );
        usernameField = formBuilder.add( LoginDetails.PROPERTY_USERNAME )[1];
        formBuilder.row();
        passwordField = formBuilder.addPasswordField( LoginDetails.PROPERTY_PASSWORD )[1];
        return formBuilder.getForm();
    }

    public boolean requestFocusInWindow() {
        // Put the focus on the right field
        String username = loginDetails.getUsername();
        JComponent field = (username != null && username.length() > 0) ? passwordField : usernameField;
        return field.requestFocusInWindow();
    }

}
@@ -2034,287 +2034,708 @@
   * 
-Get an Authentication token that contains the current username and password.%0A     * @return authentication token%0A     */%0A    public Authentication getAuthentication() %7B%0A        return new UsernamePasswordAuthenticationToken( loginDetails.getUsername(), loginDetails.getPassword()
+Set the password in the form.%0A     * @param password to install%0A     */%0A    public void setPassword(String password) %7B%0A        if( isControlCreated() ) %7B%0A            getValueModel( LoginDetails.PROPERTY_PASSWORD ).setValue( password );%0A        %7D else %7B%0A            loginDetails.setPassword( password );%0A        %7D%0A    %7D%0A%0A    /**%0A     * Get an Authentication token that contains the current username and password.%0A     * @return authentication token%0A     */%0A    public Authentication getAuthentication() %7B%0A        String username = loginDetails.getUsername().trim();%0A        String password = loginDetails.getPassword().trim();%0A        return new UsernamePasswordAuthenticationToken( username, password
  );%0A

Add setPassword method and trim username and password prior to constructing authenticaiton token.