From 422c08c6145b8c8eaff4f453b5c0276beef27290 Mon Sep 17 00:00:00 2001
From: Artur Signell <artur@vaadin.com>
Date: Wed, 30 Jul 2014 07:53:28 +0000
Subject: Allow changing theme on the fly (#2874, #14139, #14124)

* Updates UI and overlay container class names when the theme changes
* Initially verifies that the theme has actually been loaded (for the embed case)
  and class names have been properly set
* Forces a state change to all components to re-translate theme:// URLs
* Runs a full layout after the new theme has been loaded and activated

Change-Id: I5a7391abe1bb467130bbb4660e4829b43f3e4255
---
 server/src/com/vaadin/ui/UI.java | 27 ++++++++++++++++++++++-----
 1 file changed, 22 insertions(+), 5 deletions(-)

(limited to 'server/src/com')

diff --git a/server/src/com/vaadin/ui/UI.java b/server/src/com/vaadin/ui/UI.java
index a72cbe5c30..5abeea9480 100644
--- a/server/src/com/vaadin/ui/UI.java
+++ b/server/src/com/vaadin/ui/UI.java
@@ -549,8 +549,6 @@ public abstract class UI extends AbstractSingleComponentContainer implements
 
     private boolean resizeLazy = false;
 
-    private String theme;
-
     private Navigator navigator;
 
     private PushConnection pushConnection = null;
@@ -633,7 +631,7 @@ public abstract class UI extends AbstractSingleComponentContainer implements
         this.embedId = embedId;
 
         // Actual theme - used for finding CustomLayout templates
-        theme = request.getParameter("theme");
+        getState().theme = request.getParameter("theme");
 
         getPage().init(request);
 
@@ -1135,12 +1133,31 @@ public abstract class UI extends AbstractSingleComponentContainer implements
     }
 
     /**
-     * Gets the theme that was used when the UI was initialized.
+     * Gets the theme currently in use by this UI
      * 
      * @return the theme name
      */
     public String getTheme() {
-        return theme;
+        return getState(false).theme;
+    }
+
+    /**
+     * Sets the theme currently in use by this UI
+     * <p>
+     * Calling this method will remove the old theme (CSS file) from the
+     * application and add the new theme.
+     * <p>
+     * Note that this method is NOT SAFE to call in a portal environment or
+     * other environment where there are multiple UIs on the same page. The old
+     * CSS file will be removed even if there are other UIs on the page which
+     * are still using it.
+     * 
+     * @since 7.3
+     * @param theme
+     *            The new theme name
+     */
+    public void setTheme(String theme) {
+        getState().theme = theme;
     }
 
     /**
-- 
cgit v1.2.3