summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn Ahlroos <john@vaadin.com>2013-01-18 10:45:01 +0200
committerVaadin Code Review <review@vaadin.com>2013-02-18 12:41:04 +0000
commit7df9f960992cc9acfd4bbb1aa9a84af1205cd6ec (patch)
tree4bdfc6c62ce04ff2daf6884dd9dc6424144b785c
parent4e8a6db5b2c70b73287af523b3eb1d29002eca77 (diff)
downloadvaadin-framework-7df9f960992cc9acfd4bbb1aa9a84af1205cd6ec.tar.gz
vaadin-framework-7df9f960992cc9acfd4bbb1aa9a84af1205cd6ec.zip
Added tutorial examples for creating a simple login view #10076
Change-Id: I4cce618a6937cd1c0f3fc57e742666fb4749fa5a
-rw-r--r--uitest/src/com/vaadin/tests/minitutorials/v70/SimpleLoginMainView.java42
-rw-r--r--uitest/src/com/vaadin/tests/minitutorials/v70/SimpleLoginUI.java64
-rw-r--r--uitest/src/com/vaadin/tests/minitutorials/v70/SimpleLoginView.java137
3 files changed, 243 insertions, 0 deletions
diff --git a/uitest/src/com/vaadin/tests/minitutorials/v70/SimpleLoginMainView.java b/uitest/src/com/vaadin/tests/minitutorials/v70/SimpleLoginMainView.java
new file mode 100644
index 0000000000..cb59aa1608
--- /dev/null
+++ b/uitest/src/com/vaadin/tests/minitutorials/v70/SimpleLoginMainView.java
@@ -0,0 +1,42 @@
+package com.vaadin.tests.minitutorials.v70;
+
+import com.vaadin.navigator.View;
+import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.Button.ClickEvent;
+import com.vaadin.ui.CssLayout;
+import com.vaadin.ui.CustomComponent;
+import com.vaadin.ui.Label;
+
+public class SimpleLoginMainView extends CustomComponent implements View {
+
+ public static final String NAME = "";
+
+ Label text = new Label();
+
+ Button logout = new Button("Logout", new Button.ClickListener() {
+
+ @Override
+ public void buttonClick(ClickEvent event) {
+
+ // "Logout" the user
+ getSession().setAttribute("user", null);
+
+ // Refresh this view, should redirect to login view
+ getUI().getNavigator().navigateTo(NAME);
+ }
+ });
+
+ public SimpleLoginMainView() {
+ setCompositionRoot(new CssLayout(text, logout));
+ }
+
+ @Override
+ public void enter(ViewChangeEvent event) {
+ // Get the user name from the session
+ String username = String.valueOf(getSession().getAttribute("user"));
+
+ // And show the username
+ text.setValue("Hello " + username);
+ }
+}
diff --git a/uitest/src/com/vaadin/tests/minitutorials/v70/SimpleLoginUI.java b/uitest/src/com/vaadin/tests/minitutorials/v70/SimpleLoginUI.java
new file mode 100644
index 0000000000..2fbbff10a8
--- /dev/null
+++ b/uitest/src/com/vaadin/tests/minitutorials/v70/SimpleLoginUI.java
@@ -0,0 +1,64 @@
+package com.vaadin.tests.minitutorials.v70;
+
+import com.vaadin.navigator.Navigator;
+import com.vaadin.navigator.ViewChangeListener;
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.ui.UI;
+
+public class SimpleLoginUI extends UI {
+
+ @Override
+ protected void init(VaadinRequest request) {
+
+ /*
+ * Create a new instance of the navigator. The navigator will attach
+ * itself automatically to this view.
+ */
+ new Navigator(this, this);
+
+ /*
+ * The initial log view where the user can login to the application
+ */
+ getNavigator().addView(SimpleLoginView.NAME, SimpleLoginView.class);
+
+ /*
+ * Add the main view of the application
+ */
+ getNavigator().addView(SimpleLoginMainView.NAME,
+ SimpleLoginMainView.class);
+
+ /*
+ * We use a view change handler to ensure the user is always redirected
+ * to the login view if the user is not logged in.
+ */
+ getNavigator().addViewChangeListener(new ViewChangeListener() {
+
+ @Override
+ public boolean beforeViewChange(ViewChangeEvent event) {
+
+ // Check if a user has logged in
+ boolean isLoggedIn = getSession().getAttribute("user") != null;
+ boolean isLoginView = event.getNewView() instanceof SimpleLoginView;
+
+ if (!isLoggedIn && !isLoginView) {
+ // Redirect to login view always if a user has not yet
+ // logged in
+ getNavigator().navigateTo(SimpleLoginView.NAME);
+ return false;
+
+ } else if (isLoggedIn && isLoginView) {
+ // If someone tries to access to login view while logged in,
+ // then cancel
+ return false;
+ }
+
+ return true;
+ }
+
+ @Override
+ public void afterViewChange(ViewChangeEvent event) {
+
+ }
+ });
+ }
+}
diff --git a/uitest/src/com/vaadin/tests/minitutorials/v70/SimpleLoginView.java b/uitest/src/com/vaadin/tests/minitutorials/v70/SimpleLoginView.java
new file mode 100644
index 0000000000..88a2a8f678
--- /dev/null
+++ b/uitest/src/com/vaadin/tests/minitutorials/v70/SimpleLoginView.java
@@ -0,0 +1,137 @@
+package com.vaadin.tests.minitutorials.v70;
+
+import com.vaadin.data.validator.AbstractValidator;
+import com.vaadin.data.validator.EmailValidator;
+import com.vaadin.navigator.View;
+import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
+import com.vaadin.shared.ui.MarginInfo;
+import com.vaadin.ui.Alignment;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.Button.ClickEvent;
+import com.vaadin.ui.CustomComponent;
+import com.vaadin.ui.PasswordField;
+import com.vaadin.ui.TextField;
+import com.vaadin.ui.VerticalLayout;
+import com.vaadin.ui.themes.Reindeer;
+
+public class SimpleLoginView extends CustomComponent implements View,
+ Button.ClickListener {
+
+ public static final String NAME = "login";
+
+ private final TextField user;
+
+ private final PasswordField password;
+
+ private final Button loginButton;
+
+ public SimpleLoginView() {
+ setSizeFull();
+
+ // Create the user input field
+ user = new TextField("User:");
+ user.setWidth("300px");
+ user.setRequired(true);
+ user.setInputPrompt("Your username (eg. joe@email.com)");
+ user.addValidator(new EmailValidator("Username must be an email address"));
+ user.setInvalidAllowed(false);
+
+ // Create the password input field
+ password = new PasswordField("Password:");
+ password.setWidth("300px");
+ password.addValidator(new PasswordValidator());
+ password.setRequired(true);
+ password.setValue("");
+ password.setNullRepresentation("");
+
+ // Create login button
+ loginButton = new Button("Login", this);
+
+ // Add both to a panel
+ VerticalLayout fields = new VerticalLayout(user, password, loginButton);
+ fields.setCaption("Please login to access the application. (test@test.com/passw0rd)");
+ fields.setSpacing(true);
+ fields.setMargin(new MarginInfo(true, true, true, false));
+ fields.setSizeUndefined();
+
+ // The view root layout
+ VerticalLayout viewLayout = new VerticalLayout(fields);
+ viewLayout.setSizeFull();
+ viewLayout.setComponentAlignment(fields, Alignment.MIDDLE_CENTER);
+ viewLayout.setStyleName(Reindeer.LAYOUT_BLUE);
+ setCompositionRoot(viewLayout);
+ }
+
+ @Override
+ public void enter(ViewChangeEvent event) {
+ // focus the username field when user arrives to the login view
+ user.focus();
+ }
+
+ /*
+ * Validator for validating the passwords
+ */
+ private static final class PasswordValidator extends
+ AbstractValidator<String> {
+
+ public PasswordValidator() {
+ super("The password provided is not valid");
+ }
+
+ @Override
+ protected boolean isValidValue(String value) {
+ /*
+ * Password must be at least 8 characters long and contain at least
+ * one number
+ */
+ if (value != null
+ && (value.length() < 8 || !value.matches(".*\\d.*"))) {
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public Class<String> getType() {
+ return String.class;
+ }
+ }
+
+ @Override
+ public void buttonClick(ClickEvent event) {
+
+ /*
+ * Validate the fields using the navigator. By using validors for the
+ * fields we reduce the amount of queries we have to use to the database
+ * for wrongly entered passwords
+ */
+ if (!user.isValid() || !password.isValid()) {
+ return;
+ }
+
+ String username = user.getValue();
+ String password = this.password.getValue();
+
+ /*
+ * Validate username and password with database here. For examples sake
+ * I use a dummy username and password.
+ */
+ boolean isValid = username.equals("test@test.com")
+ && password.equals("passw0rd");
+
+ if(isValid){
+ // Store the current user in the service session
+ getSession().setAttribute("user", username);
+
+ // Navigate to main view
+ getUI().getNavigator().navigateTo(SimpleLoginMainView.NAME);
+
+ } else {
+
+ // Wrong password clear the password field and refocuses it
+ this.password.setValue(null);
+ this.password.focus();
+ }
+ }
+}
+