blob: f151b5b07f7c6ddef28a3d184e3bc051c6fbe82c (
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
|
---
title: Finding The Current Root And Application
order: 9
layout: page
---
[[finding-the-current-root-and-application]]
Finding the current root and application
----------------------------------------
There are many cases where you need a reference to the active
`Application` or `Root`, for instance for showing notifications in a click
listener. It is possible to get a reference to the component from the
event and then a reference from the component to the `Root` but Vaadin
also offers an easier way through two static methods:
[source,java]
....
Root.getCurrent()
Application.getCurrent()
....
For example when you want to show the name of the current Root class:
[source,java]
....
Button helloButton = new Button("Say Hello");
helloButton.addListener(new ClickListener() {
public void buttonClick(ClickEvent event) {
Notification.show("This Root is " + Root.getCurrent().getClass().getSimpleName());
}
});
....
Similarly for `Application`, for instance to find out if the application
is running in production mode:
[source,java]
....
public void buttonClick(ClickEvent event) {
String msg = "Running in ";
msg += Application.getCurrent().isProductionMode() ?
"production" : "debug";
msg += " mode";
Notification.show(msg);
}
....
*Note* that these are based on `ThreadLocal` so they won't work in a
background thread (or otherwise outside the standard request scope).
|