4 Replies Latest reply on Mar 10, 2008 6:00 PM by C Smith

    Selecting theme based on URL

    C Smith Newbie

      Is there a way to select the theme based on the URL?


      Thanks,


      Colin

        • 1. Re: Selecting theme based on URL
          Keith Naas Novice

          You could very easily write a component that picks the theme based on the URL.  Have you tried using Seam Events to observe an event that could be useful?


          There is a big list of OOTB events in 5.1.3. Contextual events

          • 2. Re: Selecting theme based on URL
            Uros Majeric Newbie

            you have a few posibilities.
            One of them is:


            inject request parameter:


            @RequestParameter
            private String theme;



            and then via Theme.instance outject it:



            ThemeSelector.instance().setTheme(theme);



            of course you have to have defined theme with this name.


            Uros

            • 3. Re: Selecting theme based on URL
              Austin Kotlus Newbie

              I don't see why not.  Although I have never tried to do this, here's one approach:


              In pages.xml


              This will call urlThemeSelector.select prior to going to any page in your application.


              <page view-id="*" action="#{urlThemeSelector.select}" />
              



              URL Theme Selector


              Just create a simple seam component that has logic to determine what theme gets used based on the URL...


              String url = facesContext.getExternalContext().getRequestServletPath();
              //determine the theme based on the URL
              
              //use SEAM's ThemeSelector to select that URL programatically
              //refer to the seam docs and javadocs
              



              Good luck!  If you get this all working, you should post back to let everyone know how.


              -ak

              • 4. Re: Selecting theme based on URL
                C Smith Newbie

                Thanks for all the help.  Here's the solution...


                Updated pages.xml with:


                <page view-id="*" action="#{URLThemeSelector.select}"/>



                Created the following bean:


                import org.apache.commons.lang.ArrayUtils;
                import org.apache.commons.lang.StringUtils;
                import org.apache.log4j.Logger;
                import org.apache.log4j.LogManager;
                import org.jboss.seam.annotations.Name;
                import org.jboss.seam.annotations.web.RequestParameter;
                import org.jboss.seam.theme.ThemeSelector;
                
                @Name("URLThemeSelector")
                public class URLThemeSelector {
                  private static final Logger logger = LogManager.getLogger(URLThemeSelector.class);
                  
                  @RequestParameter
                  private String theme;
                  
                  public void select() {
                    if (logger.isDebugEnabled()) {
                      logger.debug("select()");
                    }
                    ThemeSelector mgr = ThemeSelector.instance();
                    String currentTheme = mgr.getTheme();
                    if (StringUtils.isNotEmpty(theme) && !StringUtils.equals(currentTheme, theme)) { 
                      String themes[] = mgr.getAvailableThemes();
                      if (ArrayUtils.indexOf(themes, theme) > ArrayUtils.INDEX_NOT_FOUND) {
                        if (logger.isInfoEnabled()) {
                          logger.info("Setting theme: " + theme);
                        }
                        mgr.selectTheme(theme);
                      } else {
                        logger.warn("Invalid theme from URL: " + theme);
                      }
                    }
                  }
                }