1 19 20 package com.sslexplorer.navigation; 21 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import javax.servlet.http.HttpServletRequest ; 29 30 import org.apache.struts.action.ActionForward; 31 32 import com.sslexplorer.core.AvailableMenuItem; 33 import com.sslexplorer.core.CoreUtil; 34 import com.sslexplorer.core.MenuItem; 35 import com.sslexplorer.security.LogonControllerFactory; 36 import com.sslexplorer.security.SessionInfo; 37 38 public class MenuTree { 39 40 43 protected List menus; 44 protected HashMap menuMap; 45 protected String name; 46 47 public MenuTree(String name) { 48 this.name = name; 49 menus = new ArrayList (); 50 menuMap = new HashMap (); 51 } 52 53 58 public String getName() { 59 return name; 60 } 61 62 68 public void addMenuItem(String parentId, MenuItem menuItem) { 69 MenuItem parent = parentId == null ? null : (MenuItem) menuMap.get(parentId); 70 if (parent != null) { 71 menuItem.setParent(parent); 72 parent.addChild(menuItem); 73 } else { 74 menus.add(menuItem); 75 } 76 menuMap.put(menuItem.getId(), menuItem); 77 } 78 79 80 86 public void removeMenuItem(String parentId, String itemId) { 87 MenuItem parent = parentId == null ? null : (MenuItem) menuMap.get(parentId); 88 if (parent != null) { 89 parent.removeChild(parent.getChild(itemId)); 90 } 91 menuMap.remove(itemId); 92 } 93 94 95 99 public MenuItem getMenuItem(String id) { 100 return (MenuItem) menuMap.get(id); 101 } 102 103 104 115 public List rebuildMenus(MenuItem menuItem, int checkNavigationContext, SessionInfo info, HttpServletRequest request, String referer) { 116 List availableMenus = new ArrayList (); 117 for (Iterator i = menuItem == null ? menus.iterator() : menuItem.availableChildren(checkNavigationContext, info, request).iterator(); i.hasNext();) { 118 MenuItem it = (MenuItem) i.next(); 119 if (it.isAvailable(checkNavigationContext, info, request)) { 120 AvailableMenuItem nm = createAvailableMenuItem(it, null, request, referer, checkNavigationContext, info); 121 if (it.isLeaf() || (!it.isLeaf() && !nm.isEmpty())) { 122 availableMenus.add(nm); 123 } 124 } 125 } 126 Collections.sort(availableMenus); 127 return availableMenus; 128 } 129 130 142 public AvailableMenuItem createAvailableMenuItem(MenuItem item, AvailableMenuItem parent, HttpServletRequest request, String referer, int checkNavigationContext, SessionInfo info) { 143 return new AvailableMenuItem(item, parent, request, referer, checkNavigationContext, info); 144 } 145 146 156 public List rebuildMenus(int checkNavigationContext, SessionInfo info, HttpServletRequest request, String referer) { 157 return rebuildMenus(null, checkNavigationContext, info, request, referer); 158 } 159 160 167 public List rebuildMenus(HttpServletRequest request) { 168 return rebuildMenus(null, request); 169 170 } 171 172 180 public List rebuildMenus(MenuItem menuItem, HttpServletRequest request) { 181 SessionInfo info = LogonControllerFactory.getInstance().getSessionInfo(request); 182 return rebuildMenus(menuItem, info == null ?0 : 183 info.getNavigationContext(), info, request, 184 CoreUtil.getReferer(request)); 185 } 186 187 public ActionForward getFirstAvailableActionForward(List availableMenuItems) { 188 for(Iterator i = availableMenuItems.iterator(); i.hasNext(); ) { 189 AvailableMenuItem it = getFirstAvailableMenuLeaf((AvailableMenuItem)i.next()); 190 if(it != null) { 191 return new ActionForward(it.getPath(), true); 192 } 193 } 194 return null; 195 } 196 197 AvailableMenuItem getFirstAvailableMenuLeaf(AvailableMenuItem item) { 198 if(item.getMenuItem().isLeaf()) { 199 return item; 200 } 201 else for(Iterator i = item.iterator(); i.hasNext(); ) { 202 AvailableMenuItem it = getFirstAvailableMenuLeaf((AvailableMenuItem)i.next()); 203 if(it != null) { 204 return it; 205 } 206 } 207 return null; 208 } 209 } | Popular Tags |