From d5db82de362814a62f3031f303a23c834953caaa Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Wed, 20 Jun 2012 16:28:05 +0300 Subject: [PATCH] Add (partially broken) mini tutorial code to help detecting breakage --- .../v7a1/AutoGeneratingForm.java | 77 +++++++++++++++++++ .../minitutorials/v7a1/BasicApplication.java | 28 +++++++ .../v7a1/CreatingPreserveState.java | 31 ++++++++ .../minitutorials/v7a1/DefineRootTheme.java | 31 ++++++++ .../DifferentFeaturesForDifferentClients.java | 63 +++++++++++++++ .../v7a1/FindCurrentRootAndApplication.java | 42 ++++++++++ .../v7a1/MultiTabApplication.java | 51 ++++++++++++ .../v7a1/UsingBeanValidation.java | 74 ++++++++++++++++++ .../minitutorials/v7a1/UsingUriFragments.java | 54 +++++++++++++ .../v7a1/UsingXyzWhenInitializing.java | 53 +++++++++++++ 10 files changed, 504 insertions(+) create mode 100644 tests/testbench/com/vaadin/tests/minitutorials/v7a1/AutoGeneratingForm.java create mode 100644 tests/testbench/com/vaadin/tests/minitutorials/v7a1/BasicApplication.java create mode 100644 tests/testbench/com/vaadin/tests/minitutorials/v7a1/CreatingPreserveState.java create mode 100644 tests/testbench/com/vaadin/tests/minitutorials/v7a1/DefineRootTheme.java create mode 100644 tests/testbench/com/vaadin/tests/minitutorials/v7a1/DifferentFeaturesForDifferentClients.java create mode 100644 tests/testbench/com/vaadin/tests/minitutorials/v7a1/FindCurrentRootAndApplication.java create mode 100644 tests/testbench/com/vaadin/tests/minitutorials/v7a1/MultiTabApplication.java create mode 100644 tests/testbench/com/vaadin/tests/minitutorials/v7a1/UsingBeanValidation.java create mode 100644 tests/testbench/com/vaadin/tests/minitutorials/v7a1/UsingUriFragments.java create mode 100644 tests/testbench/com/vaadin/tests/minitutorials/v7a1/UsingXyzWhenInitializing.java diff --git a/tests/testbench/com/vaadin/tests/minitutorials/v7a1/AutoGeneratingForm.java b/tests/testbench/com/vaadin/tests/minitutorials/v7a1/AutoGeneratingForm.java new file mode 100644 index 0000000000..8eef5c08e8 --- /dev/null +++ b/tests/testbench/com/vaadin/tests/minitutorials/v7a1/AutoGeneratingForm.java @@ -0,0 +1,77 @@ +/* +@VaadinApache2LicenseForJavaFiles@ + */ + +package com.vaadin.tests.minitutorials.v7a1; + +import com.vaadin.data.fieldgroup.BeanFieldGroup; +import com.vaadin.data.fieldgroup.FieldGroup; +import com.vaadin.data.util.BeanItem; +import com.vaadin.terminal.WrappedRequest; +import com.vaadin.ui.Root; + +/** + * Mini tutorial code for + * https://vaadin.com/wiki/-/wiki/Main/Auto%20generating%20 + * a%20form%20based%20on%20a%20bean%20-%20Vaadin%206%20style%20Form + * + * @author Vaadin Ltd + * @version @VERSION@ + * @since 7.0.0 + */ +public class AutoGeneratingForm extends Root { + + @Override + protected void init(WrappedRequest request) { + FieldGroup fieldGroup = new BeanFieldGroup(Person.class); + + // We need an item data source before we create the fields to be able to + // find the properties, otherwise we have to specify them by hand + fieldGroup.setItemDataSource(new BeanItem(new Person("John", + "Doe", 34))); + + // Loop through the properties, build fields for them and add the fields + // to this root + for (Object propertyId : fieldGroup.getUnboundPropertyIds()) { + addComponent(fieldGroup.buildAndBind(propertyId)); + } + } + +} + +class Person { + private String firstName, lastName; + private int age; + + // + setters and getters + + public Person(String firstName, String lastName, int age) { + this.firstName = firstName; + this.lastName = lastName; + this.age = age; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } +} diff --git a/tests/testbench/com/vaadin/tests/minitutorials/v7a1/BasicApplication.java b/tests/testbench/com/vaadin/tests/minitutorials/v7a1/BasicApplication.java new file mode 100644 index 0000000000..3907209097 --- /dev/null +++ b/tests/testbench/com/vaadin/tests/minitutorials/v7a1/BasicApplication.java @@ -0,0 +1,28 @@ +/* +@VaadinApache2LicenseForJavaFiles@ + */ + +package com.vaadin.tests.minitutorials.v7a1; + +import com.vaadin.terminal.WrappedRequest; +import com.vaadin.ui.Label; +import com.vaadin.ui.Root; +import com.vaadin.ui.VerticalLayout; + +/** + * Mini tutorial code for + * https://vaadin.com/wiki/-/wiki/Main/Creating%20a%20basic%20application + * + * @author Vaadin Ltd + * @version @VERSION@ + * @since 7.0.0 + */ +public class BasicApplication extends Root { + + @Override + protected void init(WrappedRequest request) { + VerticalLayout view = new VerticalLayout(); + view.addComponent(new Label("Hello Vaadin!")); + setContent(view); + } +} diff --git a/tests/testbench/com/vaadin/tests/minitutorials/v7a1/CreatingPreserveState.java b/tests/testbench/com/vaadin/tests/minitutorials/v7a1/CreatingPreserveState.java new file mode 100644 index 0000000000..c175fd6065 --- /dev/null +++ b/tests/testbench/com/vaadin/tests/minitutorials/v7a1/CreatingPreserveState.java @@ -0,0 +1,31 @@ +/* +@VaadinApache2LicenseForJavaFiles@ + */ + +package com.vaadin.tests.minitutorials.v7a1; + +import com.vaadin.terminal.WrappedRequest; +import com.vaadin.ui.Root; +import com.vaadin.ui.TextField; + +/** + * Mini tutorial code for + * https://vaadin.com/wiki/-/wiki/Main/Creating%20an%20application + * %20that%20preserves%20state%20on%20refresh + * + * @author Vaadin Ltd + * @version @VERSION@ + * @since 7.0.0 + */ +public class CreatingPreserveState extends Root { + private static int windowCounter = 0; + + @Override + public void init(WrappedRequest request) { + TextField tf = new TextField("Window #" + (++windowCounter)); + tf.setImmediate(true); + getContent().addComponent(tf); + getApplication().setRootPreserved(true); + } + +} diff --git a/tests/testbench/com/vaadin/tests/minitutorials/v7a1/DefineRootTheme.java b/tests/testbench/com/vaadin/tests/minitutorials/v7a1/DefineRootTheme.java new file mode 100644 index 0000000000..3bd17547fa --- /dev/null +++ b/tests/testbench/com/vaadin/tests/minitutorials/v7a1/DefineRootTheme.java @@ -0,0 +1,31 @@ +/* +@VaadinApache2LicenseForJavaFiles@ + */ + +package com.vaadin.tests.minitutorials.v7a1; + +import com.vaadin.annotations.Theme; +import com.vaadin.terminal.WrappedRequest; +import com.vaadin.ui.Label; +import com.vaadin.ui.Root; +import com.vaadin.ui.VerticalLayout; + +/** + * Mini tutorial code for + * https://vaadin.com/wiki/-/wiki/Main/Defining%20the%20theme%20for%20a%20Root + * + * @author Vaadin Ltd + * @version @VERSION@ + * @since 7.0.0 + */ +@Theme("hello-theme") +public class DefineRootTheme extends Root { + + @Override + protected void init(WrappedRequest request) { + VerticalLayout view = new VerticalLayout(); + view.addComponent(new Label("Hello Vaadin")); + setContent(view); + } + +} diff --git a/tests/testbench/com/vaadin/tests/minitutorials/v7a1/DifferentFeaturesForDifferentClients.java b/tests/testbench/com/vaadin/tests/minitutorials/v7a1/DifferentFeaturesForDifferentClients.java new file mode 100644 index 0000000000..cf4c3b9267 --- /dev/null +++ b/tests/testbench/com/vaadin/tests/minitutorials/v7a1/DifferentFeaturesForDifferentClients.java @@ -0,0 +1,63 @@ +/* +@VaadinApache2LicenseForJavaFiles@ + */ + +package com.vaadin.tests.minitutorials.v7a1; + +import com.vaadin.Application; +import com.vaadin.RootRequiresMoreInformationException; +import com.vaadin.terminal.WrappedRequest; +import com.vaadin.terminal.WrappedRequest.BrowserDetails; +import com.vaadin.terminal.gwt.server.WebBrowser; +import com.vaadin.ui.Label; +import com.vaadin.ui.Root; + +/** + * Mini tutorial code for + * https://vaadin.com/wiki/-/wiki/Main/Creating%20an%20application + * %20with%20different%20features%20for%20different%20clients + * + * @author Vaadin Ltd + * @version @VERSION@ + * @since 7.0.0 + */ +public class DifferentFeaturesForDifferentClients extends Application { + + @Override + protected Root getRoot(WrappedRequest request) + throws RootRequiresMoreInformationException { + BrowserDetails browserDetails = request.getBrowserDetails(); + // This is a limitation of 7.0.0.alpha1 that there is no better way to + // check if WebBrowser has been fully initialized + if (browserDetails.getUriFragment() == null) { + throw new RootRequiresMoreInformationException(); + } + + // could also use screen size, browser version etc. + if (browserDetails.getWebBrowser().isTouchDevice()) { + return new TouchRoot(); + } else { + return new DefaultRoot(); + } + } +} + +class DefaultRoot extends Root { + @Override + protected void init(WrappedRequest request) { + getContent().addComponent( + new Label("This browser does not support touch events")); + } +} + +class TouchRoot extends Root { + @Override + protected void init(WrappedRequest request) { + WebBrowser webBrowser = request.getBrowserDetails().getWebBrowser(); + String screenSize = "" + webBrowser.getScreenWidth() + "x" + + webBrowser.getScreenHeight(); + getContent().addComponent( + new Label("Using a touch enabled device with screen size" + + screenSize)); + } +} diff --git a/tests/testbench/com/vaadin/tests/minitutorials/v7a1/FindCurrentRootAndApplication.java b/tests/testbench/com/vaadin/tests/minitutorials/v7a1/FindCurrentRootAndApplication.java new file mode 100644 index 0000000000..bfdeab3980 --- /dev/null +++ b/tests/testbench/com/vaadin/tests/minitutorials/v7a1/FindCurrentRootAndApplication.java @@ -0,0 +1,42 @@ +/* +@VaadinApache2LicenseForJavaFiles@ + */ + +package com.vaadin.tests.minitutorials.v7a1; + +import com.vaadin.Application; +import com.vaadin.terminal.WrappedRequest; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; +import com.vaadin.ui.Button.ClickListener; +import com.vaadin.ui.Root; + +/** + * Mini tutorial code for + * https://vaadin.com/wiki/-/wiki/Main/Finding%20the%20current + * %20Root%20and%20Application + * + * @author Vaadin Ltd + * @version @VERSION@ + * @since 7.0.0 + */ +public class FindCurrentRootAndApplication extends Root { + + @Override + protected void init(WrappedRequest request) { + Button helloButton = new Button("Say Hello"); + helloButton.addListener(new ClickListener() { + public void buttonClick(ClickEvent event) { + String msg = "Running in "; + msg += Application.getCurrentApplication().isProductionMode() ? "production" + : "debug"; + msg += " mode"; + Root.getCurrentRoot().showNotification(msg); + Root.getCurrentRoot().showNotification("Hello Vaadin user!"); + } + }); + + addComponent(helloButton); + } + +} diff --git a/tests/testbench/com/vaadin/tests/minitutorials/v7a1/MultiTabApplication.java b/tests/testbench/com/vaadin/tests/minitutorials/v7a1/MultiTabApplication.java new file mode 100644 index 0000000000..89c6ef52c4 --- /dev/null +++ b/tests/testbench/com/vaadin/tests/minitutorials/v7a1/MultiTabApplication.java @@ -0,0 +1,51 @@ +/* +@VaadinApache2LicenseForJavaFiles@ + */ + +package com.vaadin.tests.minitutorials.v7a1; + +import com.vaadin.terminal.ExternalResource; +import com.vaadin.terminal.WrappedRequest; +import com.vaadin.ui.Label; +import com.vaadin.ui.Link; +import com.vaadin.ui.Root; +import com.vaadin.ui.VerticalLayout; + +/** + * Mini tutorial code for + * https://vaadin.com/wiki/-/wiki/Main/Creating%20multi%20tab%20applications + * + * @author Vaadin Ltd + * @version @VERSION@ + * @since 7.0.0 + */ +public class MultiTabApplication extends Root { + + private class MainView extends VerticalLayout { + public MainView() { + addComponent(new Link("Edit person 1", new ExternalResource( + "?editPerson=person1"))); + addComponent(new Link("Edit person 2", new ExternalResource( + "?editPerson=person2"))); + } + } + + private class EditPersonView extends VerticalLayout { + + public EditPersonView(String person) { + addComponent(new Label("Editor for " + person)); + } + + } + + @Override + public void init(WrappedRequest request) { + String person = request.getParameter("editPerson"); + if (person == null) { + setContent(new MainView()); + } else { + setContent(new EditPersonView(person)); + } + } + +} diff --git a/tests/testbench/com/vaadin/tests/minitutorials/v7a1/UsingBeanValidation.java b/tests/testbench/com/vaadin/tests/minitutorials/v7a1/UsingBeanValidation.java new file mode 100644 index 0000000000..eb297ebd36 --- /dev/null +++ b/tests/testbench/com/vaadin/tests/minitutorials/v7a1/UsingBeanValidation.java @@ -0,0 +1,74 @@ +/* +@VaadinApache2LicenseForJavaFiles@ + */ + +package com.vaadin.tests.minitutorials.v7a1; + +import javax.validation.constraints.Max; +import javax.validation.constraints.Min; +import javax.validation.constraints.Size; + +import com.vaadin.data.util.BeanItem; +import com.vaadin.data.validator.BeanValidator; +import com.vaadin.terminal.WrappedRequest; +import com.vaadin.ui.Root; +import com.vaadin.ui.TextField; + +/** + * Mini tutorial code for + * https://vaadin.com/wiki/-/wiki/Main/Using%20Bean%20Validation + * %20to%20validate%20input + * + * @author Vaadin Ltd + * @version @VERSION@ + * @since 7.0.0 + */ +public class UsingBeanValidation extends Root { + class Person { + + @Size(min = 5, max = 50) + private String name; + + @Min(0) + @Max(100) + private int age; + + // + constructor + setters + getters + + public Person(String name, int age) { + this.name = name; + this.age = age; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + private void setN() { + // TODO Auto-generated method stub + + } + } + + @Override + protected void init(WrappedRequest request) { + Person person = new Person("John", 26); + BeanItem item = new BeanItem(person); + + TextField firstName = new TextField("First name", + item.getItemProperty("name")); + firstName.setImmediate(true); + addComponent(firstName); + + firstName.addValidator(new BeanValidator(Person.class, "name")); + } + +} diff --git a/tests/testbench/com/vaadin/tests/minitutorials/v7a1/UsingUriFragments.java b/tests/testbench/com/vaadin/tests/minitutorials/v7a1/UsingUriFragments.java new file mode 100644 index 0000000000..73fd49089a --- /dev/null +++ b/tests/testbench/com/vaadin/tests/minitutorials/v7a1/UsingUriFragments.java @@ -0,0 +1,54 @@ +/* +@VaadinApache2LicenseForJavaFiles@ + */ + +package com.vaadin.tests.minitutorials.v7a1; + +import com.vaadin.terminal.Page.FragmentChangedEvent; +import com.vaadin.terminal.Page.FragmentChangedListener; +import com.vaadin.terminal.WrappedRequest; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; +import com.vaadin.ui.Label; +import com.vaadin.ui.Root; + +/** + * Mini tutorial code for + * https://vaadin.com/wiki/-/wiki/Main/Using%20URI%20fragments + * + * @author Vaadin Ltd + * @version @VERSION@ + * @since 7.0.0 + */ +public class UsingUriFragments extends Root { + + @Override + protected void init(WrappedRequest request) { + Label label = new Label("Hello, your fragment is " + + request.getBrowserDetails().getUriFragment()); + getContent().addComponent(label); + + // React to fragment changes + addListener(new FragmentChangedListener() { + public void fragmentChanged(FragmentChangedEvent source) { + handleFragment(source.getFragment()); + } + }); + + // Handle the fragment received in the initial request + handleFragment(request.getBrowserDetails().getUriFragment()); + + addComponent(new Button("Show and set fragment", + new Button.ClickListener() { + public void buttonClick(ClickEvent event) { + handleFragment(getFragment()); + setFragment("customFragment"); + } + })); + } + + private void handleFragment(String uriFragment) { + addComponent(new Label("Got new fragment: " + uriFragment)); + } + +} diff --git a/tests/testbench/com/vaadin/tests/minitutorials/v7a1/UsingXyzWhenInitializing.java b/tests/testbench/com/vaadin/tests/minitutorials/v7a1/UsingXyzWhenInitializing.java new file mode 100644 index 0000000000..87d8351f77 --- /dev/null +++ b/tests/testbench/com/vaadin/tests/minitutorials/v7a1/UsingXyzWhenInitializing.java @@ -0,0 +1,53 @@ +/* +@VaadinApache2LicenseForJavaFiles@ + */ + +package com.vaadin.tests.minitutorials.v7a1; + +import com.vaadin.terminal.WrappedRequest; +import com.vaadin.terminal.gwt.server.WebBrowser; +import com.vaadin.ui.Label; +import com.vaadin.ui.Root; + +/** + * Mini tutorial code for + * https://vaadin.com/wiki/-/wiki/Main/Using%20URI%20or%20 + * parameters%20or%20screen%20size%20when%20initializing%20an%20application + * + * @author Vaadin Ltd + * @version @VERSION@ + * @since 7.0.0 + */ +public class UsingXyzWhenInitializing extends Root { + + @Override + protected void init(WrappedRequest request) { + String name = request.getParameter("name"); + if (name == null) { + name = "Unknown"; + } + + getContent().addComponent(new Label("Hello " + name)); + + String pathInfo = request.getRequestPathInfo(); + if ("/viewSource".equals(pathInfo)) { + getContent().addComponent(new Label("This is the source")); + } else { + getContent().addComponent(new Label("Welcome to my application")); + } + + WebBrowser browser = request.getBrowserDetails().getWebBrowser(); + String resolution = "Your browser window on startup was " + + browser.getClientWidth() + "x" + browser.getClientHeight(); + if (browser.getClientWidth() > 1024) { + getContent().addComponent( + new Label("The is the large version of the application. " + + resolution)); + } else { + getContent().addComponent( + new Label("This is the small version of the application. " + + resolution)); + } + } + +} -- 2.39.5