Java tutorial
/******************************************************************************* * Copyright (c)2014 Prometheus Consulting * * 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 nz.co.senanque.vaadinsupport.viewmanager; import java.io.Serializable; import java.util.HashMap; import java.util.Map; import javax.security.auth.login.LoginException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; import org.springframework.context.MessageSourceAware; import org.springframework.context.support.MessageSourceAccessor; import com.vaadin.addon.touchkit.ui.TouchKitApplication; import com.vaadin.ui.Button; import com.vaadin.ui.Button.ClickEvent; import com.vaadin.ui.Button.ClickListener; import com.vaadin.ui.Form; import com.vaadin.ui.PasswordField; import com.vaadin.ui.TextField; import com.vaadin.ui.Window.Notification; /** * * @author Roger Parkinson * @version $Revision:$ */ public class TouchLoginForm extends Form implements InitializingBean, MessageSourceAware, Serializable { private Logger logger = LoggerFactory.getLogger(this.getClass()); private static final long serialVersionUID = 8927668036479913166L; String usernameCaption; String passwordCaption; String submitCaption; String welcomeCaption; private MessageSource m_messageSource; @Autowired private TouchLoginListener m_loginListener; public TouchLoginForm() { } public TouchLoginListener getLoginListener() { return m_loginListener; } public void setLoginListener(TouchLoginListener loginListener) { m_loginListener = loginListener; } public void setMessageSource(MessageSource messageSource) { m_messageSource = messageSource; } public void afterPropertiesSet() throws Exception { MessageSourceAccessor messageSourceAccessor = new MessageSourceAccessor(m_messageSource); usernameCaption = messageSourceAccessor.getMessage("username"); passwordCaption = messageSourceAccessor.getMessage("password"); submitCaption = messageSourceAccessor.getMessage("login.button"); welcomeCaption = messageSourceAccessor.getMessage("welcome"); final TextField userName = new TextField(usernameCaption); userName.setImmediate(true); addField("userName", userName); final PasswordField password = new PasswordField(passwordCaption); password.setImmediate(true); addField("password", password); Button submit = new Button(submitCaption); addField("submit", submit); submit.addListener(new ClickListener() { private static final long serialVersionUID = 5201900702970450254L; public void buttonClick(ClickEvent event) { Map<String, String> map = new HashMap<String, String>(); map.put("user", String.valueOf(userName.getValue())); map.put("password", String.valueOf(password.getValue())); if (getLoginListener() != null) { try { getLoginListener().onLogin(map); } catch (Exception e) { Throwable cause = e.getCause(); if (cause == null || !(cause instanceof LoginException)) { logger.error(e.getMessage(), e); } MessageSourceAccessor messageSourceAccessor = new MessageSourceAccessor(m_messageSource); String message = messageSourceAccessor.getMessage("Bad.Login", "Bad Login"); TouchKitApplication.get().getMainWindow().showNotification(message, Notification.TYPE_ERROR_MESSAGE); return; } } TouchKitApplication.get().setUser(userName.getValue()); } }); } }