Skip to content

Instantly share code, notes, and snippets.

@bashizip
Last active November 23, 2016 10:09
Show Gist options
  • Save bashizip/3470e5c75d904fdc38afb4b36598db47 to your computer and use it in GitHub Desktop.
Save bashizip/3470e5c75d904fdc38afb4b36598db47 to your computer and use it in GitHub Desktop.
Vaadin 7 Login Layout sample
package com.karmasms;
import com.karmasms.model.KarmaUser;
import com.karmasms.restclient.ApiClient;
import com.vaadin.data.validator.EmailValidator;
import com.vaadin.data.validator.NullValidator;
import com.vaadin.server.FontAwesome;
import com.vaadin.ui.Button;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.themes.ValoTheme;
/**
*
* @author bashizip
*
*/
public class LoginForm extends HorizontalLayout {
private final ILoginStateListener loginStateListner;
TextField tfUsername;
PasswordField tfPassword;
FormLayout loginLayout;
Panel panel;
Button passwordForgotten;
public LoginForm(ILoginStateListener loginStateListner) {
setMargin(true);
this.loginStateListner = loginStateListner;
panel = new Panel("Login");
loginLayout = new FormLayout();
loginLayout.setMargin(true);
loginLayout.setImmediate(true);
tfUsername = new TextField("Email");
tfPassword = new PasswordField("Mot de Passe");
tfUsername.setIcon(FontAwesome.USER);
tfPassword.setIcon(FontAwesome.LOCK);
passwordForgotten = new Button("Mot de passe oublié?");
passwordForgotten.setStyleName(ValoTheme.BUTTON_LINK);
passwordForgotten.addClickListener(new Button.ClickListener() {
@Override
public void buttonClick(Button.ClickEvent event) {
doLogin();
}
});
Button btnLogin = new Button("Connection", new LoginClickListener());
btnLogin.setIcon(FontAwesome.CHECK);
btnLogin.setStyleName(ValoTheme.BUTTON_PRIMARY);
btnLogin.setStyleName(ValoTheme.BUTTON_FRIENDLY);
loginLayout.setSpacing(true);
loginLayout.addComponents(tfUsername, tfPassword, btnLogin);
loginLayout.addComponent(passwordForgotten);
validateValues();
panel.setContent(loginLayout);
addComponent(panel);
}
class LoginClickListener implements Button.ClickListener {
@Override
public void buttonClick(Button.ClickEvent event) {
if (!tfUsername.isValid() || !tfPassword.isValid()) {
return;
}
doLogin();
}
}
void doLogin() {
KarmaUser userToLogin = new KarmaUser(tfUsername.getValue());
userToLogin.setPassword(tfPassword.getValue());
KarmaUser res = null;
try {
res = ApiClient.getMyApiServiceClient().login(userToLogin);
} catch (Exception e) {
}
if (res != null) {
if (res.getRepresentant() != null) {
if (!res.getRepresentant().equals("")) {
loginStateListner.onLoginSuccessFull(res);
}
} else {
loginStateListner.onWrongPassword();
}
}
}
public interface ILoginStateListener {
void onLoginSuccessFull(KarmaUser user);
void onWrongPassword();
void onAccountNotFound();
}
private void validateValues() {
tfUsername.addValidator(new EmailValidator("Wrong Email Adress"));
tfUsername.addValidator(new NullValidator("Username canot be null", false));
tfUsername.setRequired(true);
tfPassword.setRequired(true);
tfUsername.setImmediate(true);
tfPassword.setImmediate(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment