Creating an application with different features for different clients

Providing different features for different clients can be done by creating a specialized UIProvider for the application.

We start by creating the specialized UI’s

Java

  1. public class DefaultUI extends UI {
  2. @Override
  3. protected void init(VaadinRequest request) {
  4. setContent(
  5. new Label("This browser does not support touch events"));
  6. }
  7. }

Java

  1. public class TouchUI extends UI {
  2. @Override
  3. protected void init(VaadinRequest request) {
  4. WebBrowser webBrowser = getPage().getWebBrowser();
  5. String screenSize = "" + webBrowser.getScreenWidth() + "x"
  6. + webBrowser.getScreenHeight();
  7. setContent(new Label("Using a touch enabled device with screen size"
  8. + screenSize));
  9. }
  10. }

We then define an UIProvider which knows what UI the application should return.

Java

  1. public class DifferentFeaturesForDifferentClients extends UIProvider {
  2. @Override
  3. public Class<? extends UI> getUIClass(UIClassSelectionEvent event) {
  4. // could also use browser version etc.
  5. if (event.getRequest().getHeader("user-agent").contains("mobile")) {
  6. return TouchUI.class;
  7. } else {
  8. return DefaultUI.class;
  9. }
  10. }
  11. }

Now that we have an UIProvider we need to tell Vaadin to use it. This is most easily done by defining the UIProvider class in web.xml instead of defining a UI class.

XML

  1. <servlet>
  2. <servlet-name>My Vaadin App</servlet-name>
  3. <servlet-class>com.vaadin.server.VaadinServlet</servlet-class>
  4. <init-param>
  5. <description>Vaadin UI</description>
  6. <param-name>UIProvider</param-name>
  7. <param-value>com.example.myexampleproject.DifferentFeaturesForDifferentClients</param-value>
  8. </init-param>
  9. </servlet>

Each UI can have its own feature set, layout and theme.