summaryrefslogtreecommitdiffstats
path: root/documentation/articles/CreatingAnApplicationWithDifferentFeaturesForDifferentClients.asciidoc
blob: c8b073868f275f902358429df2dca72c0b12b23a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
[[creating-an-application-with-different-features-for-different-clients]]
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

[source,java]
....
public class DefaultUI extends UI {
  @Override
  protected void init(VaadinRequest request) {
    setContent(
        new Label("This browser does not support touch events"));
  }
}
....

[source,java]
....
public class TouchUI extends UI {
  @Override
  protected void init(VaadinRequest request) {
    WebBrowser webBrowser = getPage().getWebBrowser();
    String screenSize = "" + webBrowser.getScreenWidth() + "x"
        + webBrowser.getScreenHeight();
    setContent(new Label("Using a touch enabled device with screen size"
        + screenSize));
  }
}
....

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

[source,java]
....
public class DifferentFeaturesForDifferentClients extends UIProvider {

  @Override
  public Class<? extends UI> getUIClass(UIClassSelectionEvent event) {
    // could also use browser version etc.
    if (event.getRequest().getHeader("user-agent").contains("mobile")) {
      return TouchUI.class;
    } else {
      return DefaultUI.class;
    }
  }
}
....

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.

[source,xml]
....
<servlet>
  <servlet-name>My Vaadin App</servlet-name>
  <servlet-class>com.vaadin.server.VaadinServlet</servlet-class>
  <init-param>
    <description>Vaadin UI</description>
    <param-name>UIProvider</param-name>
    <param-value>com.example.myexampleproject.DifferentFeaturesForDifferentClients</param-value>
  </init-param>
</servlet>
....

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