PrimeFaces Theme Development
上QQ阅读APP看书,第一时间看更新

Extending the ThemeService Bean

In order to get the ThemeSwitcher to work, we need to provide a list of theme names that ThemeSwitcher uses to offer the user a choice of themes. We will do this in the ThemeService Bean as an extension to the services it provides.

Note

As the Bean is application scoped, there is only one instance of it during the lifetime of the application. Thus, we are saving valuable resources by doing so.

We will add a new property to the Bean in a manner that is similar to how we added the theme property to the CurrentTheme Bean. The completely filled out Add Property dialog box is shown in the following screenshot:

Extending the ThemeService Bean

The themes property is an array because it will never be modified at runtime. Hence, it is a read-only property and hence the final keyword.

Place the cursor after private final String[] themes = but before ;. Then, add the following code to initialize the array. The added code is highlighted for you:

    private final String[] themes = {"afterdark",
 "afternoon",
 "afterwork",
 "aristo",
 "black-tie",
 "blitzer",
 "bluesky",
 "bootstrap",
 "casablanca",
 "cupertino",
 "cruze",
 "dark-hive",
 "delta",
 "dot-luv",
 "eggplant",
 "excite-bike",
 "flick",
 "glass-x",
 "home",
 "hot-sneaks",
 "humanity",
 "le-frog",
 "midnight",
 "mint-choc",
 "overcast",
 "pepper-grinder",
 "redmond",
 "rocket",
 "sam",
 "smoothness",
 "south-street",
 "start",
 "sunny",
 "swanky-purse",
 "trontastic",
 "ui-darkness",
 "ui-lightness",
 "vader"};

Don't forget to save the changes.

For more information about PrimeFaces themes, visit http://primefaces.org/themes We are now going to create a Facelets Template and add elements to it, which are what we want to see in each page that we create using it. As it is common in web applications, we want the navigation elements to appear in each page along with the possible status information and, for this project, a ThemeSwitcher.