setTheme method in vaadin 7 ?

come on… the typical use case would probably not be the user changing theme for whatever reason… but rather presenting the user with a theme selected at runtime (most likely at root-init) based on the url or another dynamic setting.

The option of using annotations just does not solve this case. (with this solution only… why even bother having the possibility to have more than one theme??)
The alternative of overriding and replacing the application class just seems soo unnecessary when it could have been done much easier.

Please make it possible to change the theme within the UI.init (or Root or whatever it will be called) method in a version soon

[quote=Leif Åstrand]

And now from the Vaadin Blog:

So now what? Application. getThemeForRoot () was the solution, and now that solution is gone. Like the other posters above, I need to select themes at runtime.

See UIProvider.getTheme() and
this thread
.

If you are implying that I need to override getTheme() in my UI class, I tried that:
public String getTheme() {
return ChameleonTheme.THEME_NAME;
}
and found it to not work.

Any other suggestions would be appreciated.

My use case: I’d like users to set a theme they prefer, even if it requires a logout/full refresh.

Just develop your own theme switching mechanism. It’s easy.
Even with Vaadin 6, I was always annoyed by the page reload effect when switching themes with setTheme().
So I decided to do it differently:
My application has 3 parts: menu, header and content.
In my custom switchTheme method, I set the style of those 3 elements like this:

public final void witchTheme(String theme) {
    header.setStyleName(HEADER_STYLE_NAME + theme);
    menu.setStyleName(MENU_STYLE_NAME + theme);
    content.setStyleName(CONTENT_STYLE_NAME + theme);
}

All my “themes” are contained in one css file and the switching is now as smooth as it should be.

I must second that the theme support in V7 is a real problem. I get the theme name as a URI- parameter and have no influence on that.

The “solution” of E. Denzer is not feasible in most cases. Themes are customer specific in my case. So I give the CSS and images and whatever to the customers marketing guys and they change on their own. So a strict separation.

Honestly, I did not understand the UIProvider.getTheme(). It adds IMHO an unnecessary degree of complexity.
Isn’t there a simple example for that in V7 ?

Currently the theme handling here is a showstopper on our path to V7.

Regards
Gerd

If you want to run the whole application with different themes based on URI parameters, UIProvider is the correct place to select the theme.

See e.g.
this tutorial
on how to use a UIProvider - you do not need to select different UIs but always return the same UI class and override getTheme(…) for the customization. In your web.xml, just replace the UI parameter with one for selecting the UIProvider.

The theme needs to be selected early so that it can be loaded early. Otherwise even the application loading indicator etc. would not use your theme, and/or late starting of the theme download would delay the startup of your application a little more (as it does in Vaadin 6). Reloading a page when changing themes is necessary anyway, and triggering a reload right when starting the application would lead to “flickering” and significantly degraded perceived performance.

In most applications - those where one doesn’t want to totally replace the theme but rather customize it - the solution by Emmanuel is simple and much more efficient than theme switching as it does not require reloads.

How about multy-tenant applications. I am desigig an app for ~12 tenants, they all want to have somehow different themes. Depending on the user (belonging to a differet tenant) a different theme should be selected. Can I select different themes for different (simultanuous) users?

In UIProvider you can use information that is available on the first request, so if e.g. your tenants are using different DNS names for the server or are logged in using JAAS before the first request for a Vaadin application, it should be easy to pick the theme based on that.

If the information is not available when starting the Vaadin UI, what exactly would you need?

yes you are right - tenant information should be available when starting the Vaadin UI. So setting the corresponding theme in UIProvider should work. No need to change the theme afterwards for multy-tenant purposes. Thanks.

Everyone involved in this discussion might be happy to learn that we added setTheme functionality to Vaadin 7.3.0.

See https://vaadin.com/wiki/-/wiki/Main/Changing+theme+on+the+fly for a very simple example on how this is used.

I want to use multiple themes, but we can use only one annoatation.

How to use multiple custom themes

Starting from Vaadin 7.3.0, you can use UI.setTheme to change the theme at any time. See
https://vaadin.com/wiki/-/wiki/Main/Changing+theme+on+the+fly
for a simple practical example.

Thank you.

But i have only 2 themes, I can use @Theme Annotation for one theme, can u just simply give the next option available for the using my secondTheme.

I am using setTheme(); But it is not working

I have no good ideas for why setTheme wouldn’t work in your case. Does the same theme work if you change the @Theme annotation?

You might also find some clues about what is wrong if by running the server with assertions enabled (by passing -ea as a JVM parameter) or by looking for errors in the Vaadin debug window that is opened if you add ?debug to the URL in the browser (you might also need to disable “production mode” through @VaadinServlet, @WebServlet or web.xml for the debug window to be available).

Vaadin uses default theme in bootstrap stage, as you set in @Theme annotation.
This theme name returns by
VaadinService.getConfiguredTheme
method.

For example, try to open
Valo Theme Test
, choose Dark theme and click browser refresh button. You can see, that Vaadin initially uses
Default
Valo theme and then sets
Dark
Valo theme.

My application uses several themes and i use them by overriding the method
getConfiguredTheme
as i wrote in

https://vaadin.com/forum#!/thread/10873527

Sorry, @Theme annotation is also not working and setTheme also not working, i think it is applied but some of the components doest not seem to have adapted the properties of the same.

please find the below code:


setTheme(“mobiledemo”);

if(request.getPathInfo() != null && (("/steward".equals(request.getPathInfo())) || ("/orders".equals(request.getPathInfo())) || ("/portal".equals(request.getPathInfo())))){
        if("/steward".equals(request.getPathInfo())){
            mainLayout.addComponent(new StewardLogin(UserType.STEWARD), "left: 0px; top: 0px; bottom: 30px; right: 0px");
        }else if("/orders".equals(request.getPathInfo())){
            mainLayout.addComponent(new StewardLogin(UserType.KITCHEN), "left: 0px; top: 0px; bottom: 30px; right: 0px");
        }else{
            [b]

setTheme(“indyneportaltheme”);
[/b]
setContent(new PortalMainPageUI());
}

Still it is not working.

Now i have removed the @theme Annotation.

Please suggest any thing to be removed or any feedback

Error occuring:

Sep 07, 2015 2:30:53 PM com.vaadin.server.VaadinServlet serveStaticResourcesInVAADIN
INFO: Requested resource
[/VAADIN/themes/reindeer/Images/logo_white.png] not found from filesystem or through class loader. Add widgetset and/or theme JAR to your classpath or add files to WebContent/VAADIN folder.

But in my project there is no any folder name reindeer.

However my path is /VAADIN/themes/mytheme/Images/logo_white.png