summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorErik Lumme <erik@vaadin.com>2017-09-14 13:45:44 +0300
committerErik Lumme <erik@vaadin.com>2017-09-14 13:45:44 +0300
commit5da6f980c0585d7f3df3c65eb8a4eee88ce344b7 (patch)
treeb70e9b5b533a42ca4395987008f3eb249fb39496
parent6bec699425a42004c7a66f027c30288ba7d46555 (diff)
downloadvaadin-framework-5da6f980c0585d7f3df3c65eb8a4eee88ce344b7.tar.gz
vaadin-framework-5da6f980c0585d7f3df3c65eb8a4eee88ce344b7.zip
Migrate FindingTheCurrentRootAndApplication
-rw-r--r--documentation/articles/FindingTheCurrentRootAndApplication.asciidoc44
-rw-r--r--documentation/articles/contents.asciidoc1
2 files changed, 45 insertions, 0 deletions
diff --git a/documentation/articles/FindingTheCurrentRootAndApplication.asciidoc b/documentation/articles/FindingTheCurrentRootAndApplication.asciidoc
new file mode 100644
index 0000000000..05b90308de
--- /dev/null
+++ b/documentation/articles/FindingTheCurrentRootAndApplication.asciidoc
@@ -0,0 +1,44 @@
+[[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).
diff --git a/documentation/articles/contents.asciidoc b/documentation/articles/contents.asciidoc
index b4a1a7e51e..3567389f02 100644
--- a/documentation/articles/contents.asciidoc
+++ b/documentation/articles/contents.asciidoc
@@ -9,3 +9,4 @@
- link:ChangingTheDefaultConvertersForAnApplication.asciidoc[Changing the default converters for an application]
- link:CreatingAnApplicationWithDifferentFeaturesForDifferentClients.asciidoc[Creating an application with different features for different clients]
- link:VAccessControl.asciidoc[V - Access control]
+- link:FindingTheCurrentRootAndApplication.asciidoc[Finding the current root and application]