Hello Matt.
What do you think about the code below?
It's an implementation of a login/logout process using the LoginPanel.
As the method login() is in the authenticator, and I need to display a specific form after a successful login, I had to do something like
public BananaAuthenticator(LoginDemoWindow contactMain) {
caller = contactMain;
}
Is the code below OK or is there any better way to do the same thing?
import org.apache.commons.lang.StringUtils;
import org.jcommon.security.Authenticator;
import org.jseamless.Button;
import org.jseamless.Label;
import org.jseamless.TextInput;
import org.jseamless.container.Application;
import org.jseamless.container.Form;
import org.jseamless.container.FormItem;
import org.jseamless.event.ActionEvent;
import org.jseamless.event.ActionListener;
import org.jseamlessx.LoginPanel;
public class LoginDemoWindow extends Application {
private static final long serialVersionUID = -1366775191657487249L;
protected static final String[] DOMAINS = { "Ananas Forest",
"Arachide Forest", "Banana Forest", "Jungle", };
private static final String APP_NAME = "Login Test";
private Authenticator aut = new BananaAuthenticator(this);
public LoginDemoWindow() {
super(APP_NAME);
add(getLoginPane());
}
public void displayContactForm() {
FormItem firstNameformRow = new FormItem("First Name");
TextInput firstNameTextInput = new TextInput();
FormItem lastNameFormRow = new FormItem("Last Name");
TextInput lastNameTextInput = new TextInput();
Button submitButton = new Button("Submit");
Button logoutButton = new Button("Logout");
firstNameformRow.add(firstNameTextInput);
lastNameFormRow.add(lastNameTextInput);
logoutButton.addActionListener(handleLogout());
Form contactForm = new Form();
contactForm.add(new Label("Welcome "
+ ((User) getProperty("user")).getUName()));
contactForm.add(logoutButton);
contactForm.add(firstNameformRow);
contactForm.add(lastNameFormRow);
contactForm.add(submitButton);
add(contactForm);
}
private ActionListener handleLogout() {
return new ActionListener() {
private static final long serialVersionUID = -1107120494191953903L;
public void action(ActionEvent evt) {
aut.logout();
}
};
}
public LoginPanel getLoginPane() {
return new LoginPanel("Login", false, aut, null, false);
}
}
class BananaAuthenticator implements Authenticator {
boolean coolUser = false;
LoginDemoWindow caller;
public BananaAuthenticator(LoginDemoWindow contactMain) {
caller = contactMain;
}
@Override
public String getPassword() {
return null;
}
@Override
public String getRealm() {
return null;
}
@Override
public String[] getRealms() {
return LoginDemoWindow.DOMAINS;
}
@Override
public String getUsername() {
return null;
}
@Override
public boolean isAuthenticated() {
return false;
}
@Override
public boolean login(String uname, String pass, String domain,
boolean testMode) {
if (StringUtils.isBlank(uname) || StringUtils.isBlank(pass)
|| StringUtils.isBlank(domain)) {
return false;
}
/** display the real form only after successful login */
caller.removeChildAt(0);
caller.setProperty("user", new User(uname));
caller.displayContactForm();
return true;
}
@Override
public void logout() {
this.coolUser = false;
/** After logout, remove the form, and display login form */
caller.removeChildAt(0);
caller.setProperty("user", null);
caller.add(caller.getLoginPane());
}
}
class User {
String uName;
String lName;
public String getUName() {
return uName;
}
public User(String name) {
super();
uName = name;
}