You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CreatingAnApplicationWithDifferentFeaturesForDifferentClients.asciidoc 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. ---
  2. title: Creating An Application With Different Features For Different Clients
  3. order: 7
  4. layout: page
  5. ---
  6. [[creating-an-application-with-different-features-for-different-clients]]
  7. = Creating an application with different features for different clients
  8. Providing different features for different clients can be done by
  9. creating a specialized UIProvider for the application.
  10. We start by creating the specialized UI's
  11. [source,java]
  12. ....
  13. public class DefaultUI extends UI {
  14. @Override
  15. protected void init(VaadinRequest request) {
  16. setContent(
  17. new Label("This browser does not support touch events"));
  18. }
  19. }
  20. ....
  21. [source,java]
  22. ....
  23. public class TouchUI extends UI {
  24. @Override
  25. protected void init(VaadinRequest request) {
  26. WebBrowser webBrowser = getPage().getWebBrowser();
  27. String screenSize = "" + webBrowser.getScreenWidth() + "x"
  28. + webBrowser.getScreenHeight();
  29. setContent(new Label("Using a touch enabled device with screen size"
  30. + screenSize));
  31. }
  32. }
  33. ....
  34. We then define an UIProvider which knows what UI the application should
  35. return.
  36. [source,java]
  37. ....
  38. public class DifferentFeaturesForDifferentClients extends UIProvider {
  39. @Override
  40. public Class<? extends UI> getUIClass(UIClassSelectionEvent event) {
  41. // could also use browser version etc.
  42. if (event.getRequest().getHeader("user-agent").contains("mobile")) {
  43. return TouchUI.class;
  44. } else {
  45. return DefaultUI.class;
  46. }
  47. }
  48. }
  49. ....
  50. Now that we have an `UIProvider` we need to tell Vaadin to use it. This is
  51. most easily done by defining the `UIProvider` class in web.xml instead of
  52. defining a UI class.
  53. [source,xml]
  54. ....
  55. <servlet>
  56. <servlet-name>My Vaadin App</servlet-name>
  57. <servlet-class>com.vaadin.server.VaadinServlet</servlet-class>
  58. <init-param>
  59. <description>Vaadin UI</description>
  60. <param-name>UIProvider</param-name>
  61. <param-value>com.example.myexampleproject.DifferentFeaturesForDifferentClients</param-value>
  62. </init-param>
  63. </servlet>
  64. ....
  65. Each UI can have its own feature set, layout and theme.