public MenuItemFragment(MenuItem menuItem) { super("menuItemFragment", "MENU_ITEM_FRAGMENT", MenuPanel.this); setRenderBodyOnly(true); // add the menu's label (hyperlinked if a link is provided) if (menuItem.getLink() != null) { if (menuItem.getImage() != null) { if (menuItem.getLabel() != null) { add( new LinkImageTextFragment( menuItem.getLink(), menuItem.getImage(), menuItem.getLabel())); } else { add(new LinkImageFragment(menuItem.getLink(), menuItem.getImage())); } } else { add(new LinkFragment(menuItem.getLink(), menuItem.getLabel())); } } else { if (menuItem.getImage() != null) { if (menuItem.getLabel() == null) { add(new ImageFragment(menuItem.getImage())); } } else { add(new TextFragment(menuItem.getLabel())); } } WebMarkupContainer menuItemList = new WebMarkupContainer("menuItemList"); add(menuItemList); // hide the <ul> tag if there are no submenus menuItemList.setVisible(menuItem.getChildren().size() > 0); /* // add a down or right arrow icon if there are children if (menuItem.getChildren().size() > 0) { menuItem.getLabel().add(MENU_HAS_SUBMENU_APPENDER); } */ // add the submenus menuItemList.add(new SubMenuListView("menuItemLinks", menuItem.getChildren())); }
/** * Get current {@link MenuItem} based on the <code>request</code> URL * * @param request * @param jspContext * @param menu * @return current {@link MenuItem} or <code>null</code> if not match found. */ protected MenuItem getItemFromCurrentURL( HttpServletRequest request, ServletContext jspContext, Menu menu) { // If request comes from a internal server forward we'll need original // request URI // See Servlert 2.4 specification, section SRV.8.4.2 for more // information String path = (String) request.getAttribute("javax.servlet.forward.request_uri"); String contextPath; if (path == null) { // If isn't a forwarded request so we'll use the original URI path = request.getRequestURI(); contextPath = request.getContextPath(); } else { contextPath = (String) request.getAttribute("javax.servlet.forward.context_path"); } if (path.startsWith(contextPath)) { path = path.substring(contextPath.length()); } Enumeration<String> paramNamesEnum = request.getParameterNames(); TreeSet<String> paramNames = new TreeSet<String>(); // We create a new Map because request.getParameterMap() is a map of // String[]. // In java 1.5 application will throw a ClassCastException if you try // set // ParameterMap's value into a String variable. // In java 1.6 works fine. Map<String, String> paramValues = new HashMap<String, String>(request.getParameterMap().size()); String paramName; while (paramNamesEnum.hasMoreElements()) { paramName = paramNamesEnum.nextElement(); paramNames.add(paramName); paramValues.put(paramName, request.getParameter(paramName)); } class StackItem { Iterator<MenuItem> iterator; public StackItem(Iterator<MenuItem> childIterator) { this.iterator = childIterator; } } Stack<StackItem> stack = new Stack<StackItem>(); stack.push(new StackItem(menu.getChildren().iterator())); StackItem curStackItem; MenuItem curItem; while (!stack.isEmpty()) { curStackItem = stack.pop(); while (curStackItem.iterator.hasNext()) { curItem = curStackItem.iterator.next(); if (isSameDestination( request, jspContext, path, paramNames, paramValues, curItem.getUrl())) { return curItem; } if (curItem.hasChildren()) { stack.push(curStackItem); stack.push(new StackItem(curItem.getChildren().iterator())); break; } } } return null; }