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.

FindingTheCurrentRootAndApplication.asciidoc 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. ---
  2. title: Finding The Current Root And Application
  3. order: 9
  4. layout: page
  5. ---
  6. [[finding-the-current-root-and-application]]
  7. = Finding the current root and application
  8. There are many cases where you need a reference to the active
  9. `Application` or `Root`, for instance for showing notifications in a click
  10. listener. It is possible to get a reference to the component from the
  11. event and then a reference from the component to the `Root` but Vaadin
  12. also offers an easier way through two static methods:
  13. [source,java]
  14. ....
  15. Root.getCurrent()
  16. Application.getCurrent()
  17. ....
  18. For example when you want to show the name of the current Root class:
  19. [source,java]
  20. ....
  21. Button helloButton = new Button("Say Hello");
  22. helloButton.addListener(new ClickListener() {
  23. public void buttonClick(ClickEvent event) {
  24. Notification.show("This Root is " + Root.getCurrent().getClass().getSimpleName());
  25. }
  26. });
  27. ....
  28. Similarly for `Application`, for instance to find out if the application
  29. is running in production mode:
  30. [source,java]
  31. ....
  32. public void buttonClick(ClickEvent event) {
  33. String msg = "Running in ";
  34. msg += Application.getCurrent().isProductionMode() ?
  35. "production" : "debug";
  36. msg += " mode";
  37. Notification.show(msg);
  38. }
  39. ....
  40. *Note* that these are based on `ThreadLocal` so they won't work in a
  41. background thread (or otherwise outside the standard request scope).