|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.click.extras.control.MenuFactory
public class MenuFactory
Provides a Menu factory for creating application menus from configuration files.
Menu factory provides a variety of getRootMenu() methods for loading the menus. The defaultgetRootMenu()
method creates menus
from the configuration file /WEB-INF/menu.xml, or the classpath
resource /menu.xml if WEB-INF/menu.xml was not resolved.
Below is an example menu.xml configuration file:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <menu> <menu label="Home" path="user/home.htm" roles="tomcat, role1"/> <menu label="User" path="user/home.htm" roles="tomcat, role1"> <menu label="User Page 1" path="user/user-1.htm" roles="tomcat, role1"/> <menu label="User Page 2" path="user/user-2.htm" roles="tomcat, role1"/> </menu> <menu label="Admin" path="admin/admin-1.htm" roles="role1"> <menu label="Admin Page 1" path="admin/admin-1.htm" roles="tomcat, role1"/> <menu label="Admin Page 2" path="admin/admin-2.htm" roles="tomcat, role1"/> </menu> </menu>You can also specify an alternative configuration file name to load your menus from. Just use one of the getRootMenu methods that accept a configuration file name, for example
getRootMenu(name, fileName)
.
public abstract class BorderPage extends Page { private Menu rootMenu; public BorderPage() { MenuFactory menuFactory = new MenuFactory(); rootMenu = menuFactory.getRootMenu(); addControl(rootMenu); } @Override public String getTemplate() { return "/border-template.htm"; } }
public abstract class BorderPage extends Page { // Note the transient keyword private transient Menu rootMenu; @Override public void onInit() { super.onInit(); MenuFactory menuFactory = new MenuFactory(); rootMenu = menuFactory.getRootMenu(); addControl(rootMenu); } @Override public void onDestroy() { if (rootMenu != null) { removeControl(rootMenu); } super.onDestroy(); } }
getRootMenu()
will automatically cache the
menus for improved performance (technically the menus are only cached when
Click is in production or profile mode).
If you want to manage Menu caching yourself, use one of the
getRootMenu
methods that accepts a boolean
controlling whether or not the menus are cached.
A common use case for caching menus yourself is when you need to customize
the menus based on the logged in user. For this scenario you would load the
Menus using getRootMenu(false)
, customize the
menus according to the user profile, and cache the menus in the HttpSession.
Menu
,
Serialized FormField Summary | |
---|---|
protected static Set<String> |
DEFAULT_ATTRIBUTES
The default Menu XML attributes loaded into menu properties. |
protected static String |
DEFAULT_CONFIG_FILE
The menu configuration filename: "menu.xml". |
static String |
DEFAULT_ROOT_MENU_NAME
The default root menu name: "rootMenu". |
protected static Map<String,Menu> |
MENU_CACHE
The menu cache. |
Constructor Summary | |
---|---|
MenuFactory()
|
Method Summary | |
---|---|
protected Menu |
buildMenu(Element menuElement,
AccessController accessController,
Class<? extends Menu> menuClass)
Build a new Menu from the given menu item XML Element and recurse through all the menu-items children. |
protected void |
cacheRootMenu(Menu menu)
Cache the given menu in the menu cache . |
protected Menu |
createMenu(Class<? extends Menu> menuClass)
Create a new menu instance of the given menu class. |
protected Map<String,Menu> |
getMenuCache()
Return the map containing menus cached by name. |
Menu |
getRootMenu()
Return cached root menu item defined in the WEB-INF/menu.xml or classpath menu.xml, creating menu items using the Menu class and the JEE RoleAccessController. |
Menu |
getRootMenu(AccessController accessController)
Return root menu item defined in the WEB-INF/menu.xml or classpath menu.xml, creating menu items using the Menu class and the provided AccessController. |
Menu |
getRootMenu(boolean cached)
Return root menu item defined in the WEB-INF/menu.xml or classpath menu.xml, creating menu items using the Menu class and the JEE RoleAccessController. |
Menu |
getRootMenu(Class<? extends Menu> menuClass)
Return root menu item defined in the WEB-INF/menu.xml or classpath menu.xml, creating menu items using the provided menu class and the JEE RoleAccessController. |
Menu |
getRootMenu(String name,
String fileName)
Return root menu item defined by the given name and fileName under WEB-INF or the classpath, creating menu items using the Menu class and the JEE RoleAccessController. |
Menu |
getRootMenu(String name,
String fileName,
AccessController accessController,
boolean cached,
Class<? extends Menu> menuClass)
Return root menu item defined by the given name and fileName under WEB-INF or the classpath, creating menu items using the provided menu class and AccessController. |
protected Menu |
loadFromMenuXml(String name,
String fileName,
AccessController accessController,
Class<? extends Menu> menuClass)
Return a copy of the Applications root Menu as defined by the configuration file. |
protected Menu |
retrieveRootMenu(String name)
Return the cached root menu from the menu cache . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String DEFAULT_ROOT_MENU_NAME
protected static final String DEFAULT_CONFIG_FILE
protected static final Set<String> DEFAULT_ATTRIBUTES
protected static final Map<String,Menu> MENU_CACHE
Constructor Detail |
---|
public MenuFactory()
Method Detail |
---|
public Menu getRootMenu()
RoleAccessController
public Menu getRootMenu(Class<? extends Menu> menuClass)
menuClass
- the menu class to create new Menu instances from
public Menu getRootMenu(AccessController accessController)
accessController
- the menu access controller
public Menu getRootMenu(boolean cached)
cached
- return the cached menu if in production or profile mode,
otherwise create and return a new root menu instance
public Menu getRootMenu(String name, String fileName)
name
- the name of the root menufileName
- the fileName defining the menu definitions
public Menu getRootMenu(String name, String fileName, AccessController accessController, boolean cached, Class<? extends Menu> menuClass)
public void onInit() { MenuFactory factory = new MenuFactory(); String menuName = "mymenu"; String fileName = "mymenu.xml"; AccessController accessController = new RoleAccessController(); boolean cached = true; factory.getRootMenu(menuName, fileName, accessController, cached, MyMenu.class); }
name
- the name of the root menufileName
- the fileName defining the menu definitionsaccessController
- the menu access controllercached
- return the cached menu if in production or profile mode,
otherwise create and return a new root menu instancemenuClass
- the menu class to create new Menu instances from
protected Menu buildMenu(Element menuElement, AccessController accessController, Class<? extends Menu> menuClass)
Menu
will be
created.
menuElement
- the menu item XML ElementaccessController
- the menu access controllermenuClass
- the menu class to instantiate
protected Menu createMenu(Class<? extends Menu> menuClass)
menuClass
- the menu class to instantiate
protected Menu loadFromMenuXml(String name, String fileName, AccessController accessController, Class<? extends Menu> menuClass)
name
- the name of the root menufileName
- the configuration fileName defining the menu definitionsaccessController
- the menu access controllermenuClass
- the menu class to instantiate
protected Map<String,Menu> getMenuCache()
protected Menu retrieveRootMenu(String name)
menu cache
.
name
- the name of the root menu to retrieve
protected void cacheRootMenu(Menu menu)
menu cache
.
menu
- the menu to store in the cache
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |