001 /* 002 * Copyright 2002-2004 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 005 * use this file except in compliance with the License. You may obtain a copy of 006 * the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 013 * License for the specific language governing permissions and limitations under 014 * the License. 015 */ 016 package org.springframework.richclient.security; 017 018 import javax.swing.JComponent; 019 020 import org.springframework.richclient.form.AbstractForm; 021 import org.springframework.richclient.form.FormModelHelper; 022 import org.springframework.richclient.form.builder.TableFormBuilder; 023 import org.springframework.security.Authentication; 024 import org.springframework.security.providers.UsernamePasswordAuthenticationToken; 025 026 /** 027 * This class provides a simple form for capturing a username and password from the user. 028 * It also generates an {@link Authentication} token from the entered values. 029 * 030 * @author Larry Streepy 031 * @see #getAuthentication() 032 */ 033 public class LoginForm extends AbstractForm { 034 private static final String FORM_ID = "credentials"; 035 036 private LoginDetails loginDetails; 037 038 private JComponent usernameField; 039 private JComponent passwordField; 040 041 /** 042 * Constructor. 043 */ 044 public LoginForm() { 045 super( FORM_ID ); 046 047 loginDetails = createLoginDetails(); 048 setFormModel( FormModelHelper.createUnbufferedFormModel( loginDetails ) ); 049 } 050 051 /** 052 * Set the user name in the form. 053 * @param userName to install 054 */ 055 public void setUserName(String userName) { 056 if( isControlCreated() ) { 057 getValueModel( LoginDetails.PROPERTY_USERNAME ).setValue( userName ); 058 } else { 059 loginDetails.setUsername( userName ); 060 } 061 } 062 063 /** 064 * Set the password in the form. 065 * @param password to install 066 */ 067 public void setPassword(String password) { 068 if( isControlCreated() ) { 069 getValueModel( LoginDetails.PROPERTY_PASSWORD ).setValue( password ); 070 } else { 071 loginDetails.setPassword( password ); 072 } 073 } 074 075 /** 076 * Get an Authentication token that contains the current username and password. 077 * @return authentication token 078 */ 079 public Authentication getAuthentication() { 080 String username = loginDetails.getUsername().trim(); 081 String password = loginDetails.getPassword().trim(); 082 return new UsernamePasswordAuthenticationToken( username, password ); 083 } 084 085 /** 086 * Create the form object to hold our login information. 087 * @return constructed form object 088 */ 089 protected LoginDetails createLoginDetails() { 090 return new LoginDetails(); 091 } 092 093 /** 094 * Construct the form with the username and password fields. 095 */ 096 protected JComponent createFormControl() { 097 TableFormBuilder formBuilder = new TableFormBuilder( getBindingFactory() ); 098 usernameField = formBuilder.add( LoginDetails.PROPERTY_USERNAME )[1]; 099 formBuilder.row(); 100 passwordField = formBuilder.addPasswordField( LoginDetails.PROPERTY_PASSWORD )[1]; 101 return formBuilder.getForm(); 102 } 103 104 public boolean requestFocusInWindow() { 105 // Put the focus on the right field 106 String username = loginDetails.getUsername(); 107 JComponent field = (username != null && username.length() > 0) ? passwordField : usernameField; 108 return field.requestFocusInWindow(); 109 } 110 111 }