Return-Path: Delivered-To: apmail-click-commits-archive@www.apache.org Received: (qmail 97532 invoked from network); 26 Mar 2010 12:28:18 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 26 Mar 2010 12:28:18 -0000 Received: (qmail 59584 invoked by uid 500); 26 Mar 2010 12:28:18 -0000 Delivered-To: apmail-click-commits-archive@click.apache.org Received: (qmail 59565 invoked by uid 500); 26 Mar 2010 12:28:18 -0000 Mailing-List: contact commits-help@click.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: click-dev@click.apache.org Delivered-To: mailing list commits@click.apache.org Received: (qmail 59557 invoked by uid 99); 26 Mar 2010 12:28:18 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 26 Mar 2010 12:28:18 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 26 Mar 2010 12:28:15 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 207E32388A77; Fri, 26 Mar 2010 12:27:54 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r927800 - in /click/trunk/click: build/checkstyle-suppressions.xml extras/src/org/apache/click/extras/control/Menu.java extras/src/org/apache/click/extras/control/MenuFactory.java Date: Fri, 26 Mar 2010 12:27:54 -0000 To: commits@click.apache.org From: medgar@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100326122754.207E32388A77@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: medgar Date: Fri Mar 26 12:27:53 2010 New Revision: 927800 URL: http://svn.apache.org/viewvc?rev=927800&view=rev Log: MenuFactory and Menu updates Modified: click/trunk/click/build/checkstyle-suppressions.xml click/trunk/click/extras/src/org/apache/click/extras/control/Menu.java click/trunk/click/extras/src/org/apache/click/extras/control/MenuFactory.java Modified: click/trunk/click/build/checkstyle-suppressions.xml URL: http://svn.apache.org/viewvc/click/trunk/click/build/checkstyle-suppressions.xml?rev=927800&r1=927799&r2=927800&view=diff ============================================================================== --- click/trunk/click/build/checkstyle-suppressions.xml (original) +++ click/trunk/click/build/checkstyle-suppressions.xml Fri Mar 26 12:27:53 2010 @@ -124,7 +124,7 @@ + lines="118,121"/> children = new ArrayList(); + protected List children; /** * The menu path is to an external page flag, by default this value is false. @@ -355,9 +355,15 @@ public class Menu extends AbstractContro setLabel(labelAtr); } - setImageSrc(menuElement.getAttribute("imageSrc")); + String imageSrcAtr = menuElement.getAttribute("imageSrc"); + if (StringUtils.isNotBlank(imageSrcAtr)) { + setImageSrc(imageSrcAtr); + } - setPath(menuElement.getAttribute("path")); + String pathAtr = menuElement.getAttribute("path"); + if (StringUtils.isNotBlank(pathAtr)) { + setPath(pathAtr); + } String titleAtr = menuElement.getAttribute("title"); if (StringUtils.isNotBlank(titleAtr)) { @@ -499,6 +505,9 @@ public class Menu extends AbstractContro * @return the list of submenu items */ public List getChildren() { + if (children == null) { + children = new ArrayList(); + } return children; } @@ -629,7 +638,7 @@ public class Menu extends AbstractContro */ public List getRoles() { if (roles == null) { - roles = new ArrayList(); + roles = new ArrayList(); } return roles; } @@ -847,7 +856,7 @@ public class Menu extends AbstractContro * @return the HTML HEAD elements for the control */ @Override - public List getHeadElements() { + public List getHeadElements() { String id = getId(); if (id == null) { throw new IllegalStateException("Menu name is not set"); Modified: click/trunk/click/extras/src/org/apache/click/extras/control/MenuFactory.java URL: http://svn.apache.org/viewvc/click/trunk/click/extras/src/org/apache/click/extras/control/MenuFactory.java?rev=927800&r1=927799&r2=927800&view=diff ============================================================================== --- click/trunk/click/extras/src/org/apache/click/extras/control/MenuFactory.java (original) +++ click/trunk/click/extras/src/org/apache/click/extras/control/MenuFactory.java Fri Mar 26 12:27:53 2010 @@ -39,7 +39,67 @@ import org.w3c.dom.Node; import org.w3c.dom.NodeList; /** - * TODO documentation. + * Provides a Menu factory for creating an application root menu from the + * default configuration file. By default application menus are created from + * the configuration file /WEB-INF/menu.xml, or the classpath resource + * /menu.xml if the WEB-INF menu was not resolved. + * + *

MenuFactory Examples

+ * + * Below is an example of a MenuFactory being used to set the rootMenu on a + * border page. Typically a border page will define a page template which + * contain the surrounding page chrome including the header and the application + * menu. Application page classes will subclass the BorderPage an inherit + * the application rootMenu. + * + *
+ * public abstract class BorderPage extends Page {
+ *
+ *     @Bindable public Menu rootMenu;
+ *
+ *     public BorderPage() {
+ *         MenuFactory menuFactory = new MenuFactory();
+ *         rootMenu = menuFactory.getRootMenu();
+ *     }
+ *
+ *     @Override
+ *     public String getTemplate() {
+ *         return "/border-template.htm";
+ *     }
+ *
+ * } 
+ * + * Please note if you page is stateful and serialized you probably won't want + * your application menu being serialize to disk or across a cluster with you + * page as well. In these scenarios please follow the pattern below. + * + * + *
+ * public abstract class BorderPage extends Page {
+ *
+ *     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();
+ *     }
+ *
+ * } 
+ * + * @see Menu */ public class MenuFactory { @@ -77,17 +137,17 @@ public class MenuFactory { // Public Methods --------------------------------------------------------- /** - * Return root menu item defined in the WEB-INF/menu.xml or classpath + * 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 * RollAccessController. * * @see RoleAccessController * - * @return the root menu item defined in the WEB-INF/menu.xml file or menu.xml - * in the root classpath + * @return the cached root menu item defined in the WEB-INF/menu.xml file + * or menu.xml in the root classpath */ public Menu getRootMenu() { - return getRootMenu(Menu.class, new RoleAccessController()); + return getRootMenu(Menu.class, new RoleAccessController(), true); } /** @@ -96,11 +156,11 @@ public class MenuFactory { * RollAccessController. * * @param menuClass the menu class to create new Menu instances from - * @return the root menu item defined in the WEB-INF/menu.xml file or menu.xml - * in the root classpath + * @return the cached root menu item defined in the WEB-INF/menu.xml file + * or menu.xml in the root classpath */ public Menu getRootMenu(Class menuClass) { - return getRootMenu(new RoleAccessController()); + return getRootMenu(menuClass, new RoleAccessController(), true); } /** @@ -113,7 +173,7 @@ public class MenuFactory { * in the root classpath */ public Menu getRootMenu(AccessController accessController) { - return getRootMenu(Menu.class, accessController); + return getRootMenu(Menu.class, accessController, true); } /** @@ -123,16 +183,20 @@ public class MenuFactory { * * @param menuClass the menu class to create new Menu instances from * @param accessController the menu access controller + * @param cached return the cached menu if in production or profile mode, + * otherwise create and return a new root menu instance * @return the root menu item defined in the WEB-INF/menu.xml file or menu.xml * in the root classpath */ - public Menu getRootMenu(Class menuClass, AccessController accessController) { + public Menu getRootMenu(Class menuClass, + AccessController accessController, + boolean cached) { Validate.notNull(menuClass, "Null menuClass parameter"); Validate.notNull(accessController, "Null accessController parameter"); - // If menu is cached return it - if (CACHED_ROOT_MENU != null) { + // If after cached menu and already loaded then get cached menu + if (cached && CACHED_ROOT_MENU != null) { return CACHED_ROOT_MENU; } @@ -142,8 +206,9 @@ public class MenuFactory { ServletContext servletContext = Context.getThreadLocalContext().getServletContext(); ConfigService configService = ClickUtils.getConfigService(servletContext); - if (configService.isProductionMode() || configService.isProfileMode()) { - // Cache menu in production modes + // Cache the menu if requested, and application in production or profile mode + if (cached && (configService.isProductionMode() || configService.isProfileMode())) { + CACHED_ROOT_MENU = loadedMenu; } @@ -233,15 +298,25 @@ public class MenuFactory { Menu menu = menuClass.newInstance(); - menu.setName(menuElement.getAttribute("name")); - - menu.setAccessController(accessController); + String nameAtr = menuElement.getAttribute("name"); + if (StringUtils.isNotBlank(nameAtr)) { + menu.setName(nameAtr); + } - menu.setLabel(menuElement.getAttribute("label")); + String labelAtr = menuElement.getAttribute("label"); + if (StringUtils.isNotBlank(labelAtr)) { + menu.setLabel(labelAtr); + } - menu.setImageSrc(menuElement.getAttribute("imageSrc")); + String imageSrcAtr = menuElement.getAttribute("imageSrc"); + if (StringUtils.isNotBlank(imageSrcAtr)) { + menu.setImageSrc(imageSrcAtr); + } - menu.setPath(menuElement.getAttribute("path")); + String pathAtr = menuElement.getAttribute("path"); + if (StringUtils.isNotBlank(pathAtr)) { + menu.setPath(pathAtr); + } String titleAtr = menuElement.getAttribute("title"); if (StringUtils.isNotBlank(titleAtr)) { @@ -264,7 +339,7 @@ public class MenuFactory { } String pagesValue = menuElement.getAttribute("pages"); - if (!StringUtils.isBlank(pagesValue)) { + if (StringUtils.isNotBlank(pagesValue)) { StringTokenizer tokenizer = new StringTokenizer(pagesValue, ","); while (tokenizer.hasMoreTokens()) { String path = tokenizer.nextToken().trim(); @@ -274,7 +349,7 @@ public class MenuFactory { } String rolesValue = menuElement.getAttribute("roles"); - if (!StringUtils.isBlank(rolesValue)) { + if (StringUtils.isNotBlank(rolesValue)) { StringTokenizer tokenizer = new StringTokenizer(rolesValue, ","); while (tokenizer.hasMoreTokens()) { menu.getRoles().add(tokenizer.nextToken().trim()); @@ -297,7 +372,7 @@ public class MenuFactory { Node node = childElements.item(i); if (node instanceof Element) { Menu childMenu = buildMenu((Element) node, menuClass, accessController); - menu.getChildren().add(childMenu); + menu.add(childMenu); } }